From c8f37a95c9b92c92e55d64f8ff47d546c985658e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 8 May 2013 20:55:20 -0700 Subject: [PATCH] [bs-18] Unhack direct field access Remove reflection hacks from ConfigurationPropertiesBindingConfiguration following updates to Spring 4.0 --- ...urationPropertiesBindingConfiguration.java | 28 ++++++------------- 1 file changed, 8 insertions(+), 20 deletions(-) diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ConfigurationPropertiesBindingConfiguration.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ConfigurationPropertiesBindingConfiguration.java index 7d8058f328..f458014aab 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ConfigurationPropertiesBindingConfiguration.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/annotation/ConfigurationPropertiesBindingConfiguration.java @@ -16,8 +16,6 @@ package org.springframework.bootstrap.context.annotation; -import java.lang.reflect.Field; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ConfigurableApplicationContext; @@ -30,7 +28,6 @@ import org.springframework.core.env.Environment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySources; -import org.springframework.util.ReflectionUtils; import org.springframework.validation.Validator; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; @@ -78,15 +75,14 @@ public class ConfigurationPropertiesBindingConfiguration { if (this.configurer != null) { propertySources = extractPropertySources(this.configurer); + } else if (this.environment instanceof ConfigurableEnvironment) { + propertySources = flattenPropertySources(((ConfigurableEnvironment) this.environment) + .getPropertySources()); } else { - if (this.environment instanceof ConfigurableEnvironment) { - propertySources = flattenPropertySources(((ConfigurableEnvironment) this.environment) - .getPropertySources()); - } else { - // empty, so not very useful, but fulfils the contract - propertySources = new MutablePropertySources(); - } + // empty, so not very useful, but fulfils the contract + propertySources = new MutablePropertySources(); } + PropertySourcesBindingPostProcessor processor = new PropertySourcesBindingPostProcessor(); processor.setValidator(this.validator); processor.setConversionService(this.conversionService); @@ -117,7 +113,7 @@ public class ConfigurationPropertiesBindingConfiguration { */ private void flattenPropertySources(PropertySource propertySource, MutablePropertySources result) { - Object source = getField(propertySource, "source"); + Object source = propertySource.getSource(); if (source instanceof ConfigurableEnvironment) { ConfigurableEnvironment environment = (ConfigurableEnvironment) source; for (PropertySource childSource : environment.getPropertySources()) { @@ -140,17 +136,9 @@ public class ConfigurationPropertiesBindingConfiguration { */ private PropertySources extractPropertySources( PropertySourcesPlaceholderConfigurer configurer) { - PropertySources propertySources = (PropertySources) getField(configurer, - "propertySources"); + PropertySources propertySources = configurer.getAppliedPropertySources(); // Flatten the sources into a single list so they can be iterated return flattenPropertySources(propertySources); } - private Object getField(Object target, String name) { - // Hack, hack, hackety, hack... - Field field = ReflectionUtils.findField(target.getClass(), name); - ReflectionUtils.makeAccessible(field); - return ReflectionUtils.getField(field, target); - } - }