From 39e2aaa41c447b31ad934edb18bd95ec81be2b86 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 23 Jan 2019 14:41:39 +0000 Subject: [PATCH] Size the ElementsParser based on expected number of elements Previously, the ElementsParser would be created using its default capacity of 6 even when parsing a String that is expected to produce a single element. This commit updates ConfigurationPropertyName to create an ElementsParser with a capacity of 1 when parsing a String that should contain only a single element. See gh-15760 --- .../source/ConfigurationPropertyName.java | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java index d1af209d520..0af5f76fdc5 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java @@ -195,7 +195,7 @@ public final class ConfigurationPropertyName if (elementValue == null) { return this; } - Elements additionalElements = elementsOf(elementValue); + Elements additionalElements = probablySingleElementOf(elementValue); return new ConfigurationPropertyName(this.elements.append(additionalElements)); } @@ -424,11 +424,16 @@ public final class ConfigurationPropertyName return (elements != null) ? new ConfigurationPropertyName(elements) : null; } - private static Elements elementsOf(CharSequence name) { - return elementsOf(name, false); + private static Elements probablySingleElementOf(CharSequence name) { + return elementsOf(name, false, 1); } private static Elements elementsOf(CharSequence name, boolean returnNullIfInvalid) { + return elementsOf(name, returnNullIfInvalid, ElementsParser.DEFAULT_CAPACITY); + } + + private static Elements elementsOf(CharSequence name, boolean returnNullIfInvalid, + int parserCapacity) { if (name == null) { Assert.isTrue(returnNullIfInvalid, "Name must not be null"); return null; @@ -443,7 +448,7 @@ public final class ConfigurationPropertyName throw new InvalidConfigurationPropertyNameException(name, Collections.singletonList('.')); } - Elements elements = new ElementsParser(name, '.').parse(); + Elements elements = new ElementsParser(name, '.', parserCapacity).parse(); for (int i = 0; i < elements.getSize(); i++) { if (elements.getType(i) == ElementType.NON_UNIFORM) { if (returnNullIfInvalid) {