Browse Source

Polish 'Optimize SystemEnvironmentPropertyMapper'

Introduce a new `ConfigurationPropertyName.ofIfValid` method to
save us needing to throw and catch an exception unnecessarily.

See gh-21523
pull/21793/head
Phillip Webb 6 years ago
parent
commit
47c1928189
  1. 12
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java
  2. 13
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java
  3. 12
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java

12
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/ConfigurationPropertyName.java

@ -510,6 +510,18 @@ public final class ConfigurationPropertyName implements Comparable<Configuration @@ -510,6 +510,18 @@ public final class ConfigurationPropertyName implements Comparable<Configuration
return of(name, false);
}
/**
* Return a {@link ConfigurationPropertyName} for the specified string or {@code null}
* if the name is not valid.
* @param name the source name
* @return a {@link ConfigurationPropertyName} instance
* @throws InvalidConfigurationPropertyNameException if the name is not valid
* @since 2.3.1
*/
public static ConfigurationPropertyName ofIfValid(CharSequence name) {
return of(name, true);
}
/**
* Return a {@link ConfigurationPropertyName} for the specified string.
* @param name the source name

13
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java

@ -116,16 +116,11 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper { @@ -116,16 +116,11 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
if (!hasDashedEntries(name)) {
return false;
}
StringBuilder legacyCompatibleName = buildLegacyCompatibleName(name);
try {
return ConfigurationPropertyName.of(legacyCompatibleName).isAncestorOf(candidate);
}
catch (Exception ex) {
return false;
}
ConfigurationPropertyName legacyCompatibleName = buildLegacyCompatibleName(name);
return legacyCompatibleName != null && legacyCompatibleName.isAncestorOf(candidate);
}
private StringBuilder buildLegacyCompatibleName(ConfigurationPropertyName name) {
private ConfigurationPropertyName buildLegacyCompatibleName(ConfigurationPropertyName name) {
StringBuilder legacyCompatibleName = new StringBuilder();
for (int i = 0; i < name.getNumberOfElements(); i++) {
if (i != 0) {
@ -133,7 +128,7 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper { @@ -133,7 +128,7 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper {
}
legacyCompatibleName.append(name.getElement(i, Form.DASHED).replace('-', '.'));
}
return legacyCompatibleName;
return ConfigurationPropertyName.ofIfValid(legacyCompatibleName);
}
boolean hasDashedEntries(ConfigurationPropertyName name) {

12
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/source/ConfigurationPropertyNameTests.java

@ -210,6 +210,18 @@ class ConfigurationPropertyNameTests { @@ -210,6 +210,18 @@ class ConfigurationPropertyNameTests {
assertThat(name.append("foo").toString()).isEqualTo("foo");
}
@Test
void ofIfValidWhenNameIsValidReturnsName() {
ConfigurationPropertyName name = ConfigurationPropertyName.ofIfValid("spring.bo-ot");
assertThat(name).hasToString("spring.bo-ot");
}
@Test
void ofIfValidWhenNameIsNotValidReturnsNull() {
ConfigurationPropertyName name = ConfigurationPropertyName.ofIfValid("spring.bo!oot");
assertThat(name).isNull();
}
@Test
void adaptWhenNameIsNullShouldThrowException() {
assertThatIllegalArgumentException().isThrownBy(() -> ConfigurationPropertyName.adapt(null, '.'))

Loading…
Cancel
Save