Browse Source

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
pull/15799/head
Andy Wilkinson 7 years ago
parent
commit
39e2aaa41c
  1. 13
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java

13
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) { if (elementValue == null) {
return this; return this;
} }
Elements additionalElements = elementsOf(elementValue); Elements additionalElements = probablySingleElementOf(elementValue);
return new ConfigurationPropertyName(this.elements.append(additionalElements)); return new ConfigurationPropertyName(this.elements.append(additionalElements));
} }
@ -424,11 +424,16 @@ public final class ConfigurationPropertyName
return (elements != null) ? new ConfigurationPropertyName(elements) : null; return (elements != null) ? new ConfigurationPropertyName(elements) : null;
} }
private static Elements elementsOf(CharSequence name) { private static Elements probablySingleElementOf(CharSequence name) {
return elementsOf(name, false); return elementsOf(name, false, 1);
} }
private static Elements elementsOf(CharSequence name, boolean returnNullIfInvalid) { 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) { if (name == null) {
Assert.isTrue(returnNullIfInvalid, "Name must not be null"); Assert.isTrue(returnNullIfInvalid, "Name must not be null");
return null; return null;
@ -443,7 +448,7 @@ public final class ConfigurationPropertyName
throw new InvalidConfigurationPropertyNameException(name, throw new InvalidConfigurationPropertyNameException(name,
Collections.singletonList('.')); Collections.singletonList('.'));
} }
Elements elements = new ElementsParser(name, '.').parse(); Elements elements = new ElementsParser(name, '.', parserCapacity).parse();
for (int i = 0; i < elements.getSize(); i++) { for (int i = 0; i < elements.getSize(); i++) {
if (elements.getType(i) == ElementType.NON_UNIFORM) { if (elements.getType(i) == ElementType.NON_UNIFORM) {
if (returnNullIfInvalid) { if (returnNullIfInvalid) {

Loading…
Cancel
Save