Repository: apache/dubbo
File: dubbo-common/src/main/java/org/apache/dubbo/common/config/EnvironmentConfiguration.java (Java)

Code before:
 * Configuration from system environment
 */
public class EnvironmentConfiguration implements Configuration {

    private final Map<String, String> envMap;

    @Override
    public Object getInternalProperty(String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        String value = envMap.get(key);
        if (value != null) {
            return value;
        }
        for (String candidateKey : generateCandidateEnvironmentKeys(key)) {
            value = envMap.get(candidateKey);
            if (value != null) {
                return value;
            }
        }

        String osStyleKey = StringUtils.toOSStyleKey(key);
        value = envMap.get(osStyleKey);
        return value;
    }

    private Set<String> generateCandidateEnvironmentKeys(String originalKey) {
        Set<String> candidates = new LinkedHashSet<>();

        // Dots and hyphens to underscores, uppercase
        String normalizedKey = originalKey
                .replace(CommonConstants.DOT_SEPARATOR, CommonConstants.UNDERLINE_SEPARATOR)
                .replace(CommonConstants.PROPERTIES_CHAR_SEPARATOR, CommonConstants.UNDERLINE_SEPARATOR);
        candidates.add(normalizedKey.toUpperCase(Locale.ROOT));

        // Dots to underscores, hyphens removed, uppercase (Spring Boot style)
        String springLikeNoHyphens = originalKey
                .replace(CommonConstants.DOT_SEPARATOR, CommonConstants.UNDERLINE_SEPARATOR)
                .replace(CommonConstants.PROPERTIES_CHAR_SEPARATOR, "")
                .toUpperCase(Locale.ROOT);
        candidates.add(springLikeNoHyphens);

        // Dots to underscores, hyphens preserved, uppercase
        String dotsToUnderscoresUpper = originalKey
                .replace(CommonConstants.DOT_SEPARATOR, CommonConstants.UNDERLINE_SEPARATOR)
                .toUpperCase(Locale.ROOT);
        candidates.add(dotsToUnderscoresUpper);

        // Dots and hyphens to underscores, lowercase
        candidates.add(normalizedKey);

Review comment (performance):
Although it’s not critical, could we optimize this piece of code? There’s no need to precompute all the keys — we can generate one, test it, and add it to the tested records. Also, the replacement of DOT_SEPARATOR doesn’t need to be performed multiple times.

Code after:
 * Configuration from system environment
 */
public class EnvironmentConfiguration implements Configuration {

    @Override
    public Object getInternalProperty(String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        String value = getenv().get(key);
        if (value != null) {
            return value;
        }
        for (String candidateKey : generateCandidateEnvironmentKeys(key)) {
            value = getenv().get(candidateKey);
            if (value != null) {
                return value;
            }
        }

        String osStyleKey = StringUtils.toOSStyleKey(key);
        value = getenv().get(osStyleKey);
        return value;
    }

    private Set<String> generateCandidateEnvironmentKeys(String originalKey) {
        Set<String> candidates = new LinkedHashSet<>();

        String dotsToUnderscores =
                originalKey.replace(CommonConstants.DOT_SEPARATOR, CommonConstants.UNDERLINE_SEPARATOR);
        String normalizedKey = dotsToUnderscores.replace(
                CommonConstants.PROPERTIES_CHAR_SEPARATOR, CommonConstants.UNDERLINE_SEPARATOR);

        candidates.add(normalizedKey.toUpperCase(Locale.ROOT));

        String springLikeNoHyphens = dotsToUnderscores
                .replace(CommonConstants.PROPERTIES_CHAR_SEPARATOR, "")
                .toUpperCase(Locale.ROOT);
        candidates.add(springLikeNoHyphens);

        String dotsToUnderscoresUpper = dotsToUnderscores.toUpperCase(Locale.ROOT);
        candidates.add(dotsToUnderscoresUpper);

        candidates.add(normalizedKey);

        return candidates;
    }

    public Map<String, String> getProperties() {
        return getenv();
    }