From 33deba4948c8589d51e36dd616be915c96fc81cd Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Fri, 5 Jun 2020 15:51:28 -0700 Subject: [PATCH 1/4] Use chars rather than strings Update `SystemEnvironmentPropertyMapper` to use single chars rather than strings whenever possible. See gh-21523 --- .../properties/source/SystemEnvironmentPropertyMapper.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java index eb50e0d2c96..19775ab8178 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java @@ -58,7 +58,7 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper { StringBuilder result = new StringBuilder(); for (int i = 0; i < numberOfElements; i++) { if (result.length() > 0) { - result.append("_"); + result.append('_'); } result.append(name.getElement(i, Form.UNIFORM).toUpperCase(Locale.ENGLISH)); } @@ -69,7 +69,7 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper { StringBuilder result = new StringBuilder(); for (int i = 0; i < name.getNumberOfElements(); i++) { if (result.length() > 0) { - result.append("_"); + result.append('_'); } result.append(convertLegacyNameElement(name.getElement(i, Form.ORIGINAL))); } @@ -118,7 +118,7 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper { } StringBuilder legacyCompatibleName = new StringBuilder(); for (int i = 0; i < name.getNumberOfElements(); i++) { - legacyCompatibleName.append((i != 0) ? "." : ""); + legacyCompatibleName.append((i != 0) ? '.' : ""); legacyCompatibleName.append(name.getElement(i, Form.DASHED).replace('-', '.')); } return ConfigurationPropertyName.isValid(legacyCompatibleName) From f8d6d9a4b0dac75c6733afa96c4f92b62db1f948 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Fri, 5 Jun 2020 15:58:13 -0700 Subject: [PATCH 2/4] Call append only when necessary See gh-21523 --- .../properties/source/SystemEnvironmentPropertyMapper.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java index 19775ab8178..c6632c45453 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java @@ -118,7 +118,9 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper { } StringBuilder legacyCompatibleName = new StringBuilder(); for (int i = 0; i < name.getNumberOfElements(); i++) { - legacyCompatibleName.append((i != 0) ? '.' : ""); + if (i != 0) { + legacyCompatibleName.append('.'); + } legacyCompatibleName.append(name.getElement(i, Form.DASHED).replace('-', '.')); } return ConfigurationPropertyName.isValid(legacyCompatibleName) From 0378de7b308a60449f62762bcea6d307dcc90e5b Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Fri, 5 Jun 2020 16:03:03 -0700 Subject: [PATCH 3/4] Optimize SystemEnvironmentPropertyMapper See gh-21523 --- .../source/SystemEnvironmentPropertyMapper.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java index c6632c45453..5b21e5f30d6 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/source/SystemEnvironmentPropertyMapper.java @@ -116,6 +116,16 @@ 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; + } + } + + private StringBuilder buildLegacyCompatibleName(ConfigurationPropertyName name) { StringBuilder legacyCompatibleName = new StringBuilder(); for (int i = 0; i < name.getNumberOfElements(); i++) { if (i != 0) { @@ -123,8 +133,7 @@ final class SystemEnvironmentPropertyMapper implements PropertyMapper { } legacyCompatibleName.append(name.getElement(i, Form.DASHED).replace('-', '.')); } - return ConfigurationPropertyName.isValid(legacyCompatibleName) - && ConfigurationPropertyName.of(legacyCompatibleName).isAncestorOf(candidate); + return legacyCompatibleName; } boolean hasDashedEntries(ConfigurationPropertyName name) { From 47c1928189b77b0a0687325242ffbd3e7b7f7698 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 5 Jun 2020 16:10:36 -0700 Subject: [PATCH 4/4] Polish 'Optimize SystemEnvironmentPropertyMapper' Introduce a new `ConfigurationPropertyName.ofIfValid` method to save us needing to throw and catch an exception unnecessarily. See gh-21523 --- .../source/ConfigurationPropertyName.java | 12 ++++++++++++ .../source/SystemEnvironmentPropertyMapper.java | 13 ++++--------- .../source/ConfigurationPropertyNameTests.java | 12 ++++++++++++ 3 files changed, 28 insertions(+), 9 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 ec2bdbca570..2c8bd8b64e4 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 @@ -510,6 +510,18 @@ public final class ConfigurationPropertyName implements Comparable ConfigurationPropertyName.adapt(null, '.'))