From a92bd422369460efbda7fb433d80de601d584604 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 16 Jul 2023 12:55:25 +0200 Subject: [PATCH] Stop using Constants utility in PropertyPlaceholderConfigurer See gh-30851 --- .../config/PropertyPlaceholderConfigurer.java | 22 +++++++++++++---- .../PropertyPlaceholderConfigurerTests.java | 24 +++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.java b/spring-beans/src/main/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.java index 130f13e310..0fba4f79c2 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +16,14 @@ package org.springframework.beans.factory.config; +import java.util.Map; import java.util.Properties; import org.springframework.beans.BeansException; -import org.springframework.core.Constants; import org.springframework.core.SpringProperties; import org.springframework.core.env.AbstractEnvironment; import org.springframework.lang.Nullable; +import org.springframework.util.Assert; import org.springframework.util.PropertyPlaceholderHelper; import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver; import org.springframework.util.StringValueResolver; @@ -45,6 +46,7 @@ import org.springframework.util.StringValueResolver; * * @author Juergen Hoeller * @author Chris Beams + * @author Sam Brannen * @since 02.10.2003 * @see #setSystemPropertiesModeName * @see PlaceholderConfigurerSupport @@ -72,7 +74,16 @@ public class PropertyPlaceholderConfigurer extends PlaceholderConfigurerSupport public static final int SYSTEM_PROPERTIES_MODE_OVERRIDE = 2; - private static final Constants constants = new Constants(PropertyPlaceholderConfigurer.class); + /** + * Map of constant names to constant values for the system properties mode + * constants defined in this class. + */ + private static final Map constants = Map.of( + "SYSTEM_PROPERTIES_MODE_NEVER", SYSTEM_PROPERTIES_MODE_NEVER, + "SYSTEM_PROPERTIES_MODE_FALLBACK", SYSTEM_PROPERTIES_MODE_FALLBACK, + "SYSTEM_PROPERTIES_MODE_OVERRIDE", SYSTEM_PROPERTIES_MODE_OVERRIDE + ); + private int systemPropertiesMode = SYSTEM_PROPERTIES_MODE_FALLBACK; @@ -87,7 +98,10 @@ public class PropertyPlaceholderConfigurer extends PlaceholderConfigurerSupport * @see #setSystemPropertiesMode */ public void setSystemPropertiesModeName(String constantName) throws IllegalArgumentException { - this.systemPropertiesMode = constants.asNumber(constantName).intValue(); + Assert.hasText(constantName, "'constantName' must not be null or blank"); + Integer mode = constants.get(constantName); + Assert.notNull(mode, "Only system properties mode constants allowed"); + this.systemPropertiesMode = mode; } /** diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java index 8be82a310c..c86bcff6ea 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/config/PropertyPlaceholderConfigurerTests.java @@ -16,7 +16,10 @@ package org.springframework.beans.factory.config; +import java.lang.reflect.Field; +import java.util.Arrays; import java.util.Properties; +import java.util.stream.Stream; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -27,8 +30,10 @@ import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.testfixture.beans.TestBean; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; +import org.springframework.util.ReflectionUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNoException; import static org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition; import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; import static org.springframework.beans.factory.support.BeanDefinitionReaderUtils.registerWithGeneratedName; @@ -235,4 +240,23 @@ class PropertyPlaceholderConfigurerTests { System.clearProperty("my.name"); } + /** + * This test effectively verifies that the internal 'constants' map is properly + * configured for all SYSTEM_PROPERTIES_MODE_ constants defined in + * {@link PropertyPlaceholderConfigurer}. + */ + @Test + @SuppressWarnings("deprecation") + void setSystemPropertiesModeNameToAllSupportedValues() { + streamSystemPropertiesModeConstants() + .map(Field::getName) + .forEach(name -> assertThatNoException().as(name).isThrownBy(() -> ppc.setSystemPropertiesModeName(name))); + } + + private static Stream streamSystemPropertiesModeConstants() { + return Arrays.stream(PropertyPlaceholderConfigurer.class.getFields()) + .filter(ReflectionUtils::isPublicStaticFinal) + .filter(field -> field.getName().startsWith("SYSTEM_PROPERTIES_MODE_")); + } + }