Skip to content

Commit a679f3e

Browse files
committed
Polishing
(cherry picked from commit be5f2a8)
1 parent bd5b374 commit a679f3e

File tree

4 files changed

+64
-84
lines changed

4 files changed

+64
-84
lines changed

spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.junit.Before;
2525
import org.junit.Test;
26+
2627
import org.springframework.aop.scope.ScopedObject;
2728
import org.springframework.aop.scope.ScopedProxyUtils;
2829
import org.springframework.beans.factory.FactoryBean;
@@ -86,8 +87,7 @@ public void enhancementIsPresentBecauseSingletonSemanticsAreRespected() {
8687
*/
8788
@Test
8889
public void alreadyLoadedConfigurationClasses() {
89-
beanFactory.registerBeanDefinition("unloadedConfig", new RootBeanDefinition(UnloadedConfig.class.getName(),
90-
null, null));
90+
beanFactory.registerBeanDefinition("unloadedConfig", new RootBeanDefinition(UnloadedConfig.class.getName()));
9191
beanFactory.registerBeanDefinition("loadedConfig", new RootBeanDefinition(LoadedConfig.class));
9292
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
9393
pp.postProcessBeanFactory(beanFactory);
@@ -126,56 +126,56 @@ public void postProcessorWorksWithComposedConfigurationUsingAsm() {
126126
@Test
127127
public void postProcessorWorksWithComposedConfigurationWithAttributeOverridesUsingReflection() {
128128
RootBeanDefinition beanDefinition = new RootBeanDefinition(
129-
ComposedConfigurationWithAttributeOverridesClass.class);
129+
ComposedConfigurationWithAttributeOverridesClass.class);
130130
assertSupportForComposedAnnotation(beanDefinition);
131131
}
132132

133133
@Test
134134
public void postProcessorWorksWithComposedConfigurationWithAttributeOverridesUsingAsm() {
135135
RootBeanDefinition beanDefinition = new RootBeanDefinition(
136-
ComposedConfigurationWithAttributeOverridesClass.class.getName());
136+
ComposedConfigurationWithAttributeOverridesClass.class.getName());
137137
assertSupportForComposedAnnotation(beanDefinition);
138138
}
139139

140140
@Test
141141
public void postProcessorWorksWithComposedComposedConfigurationWithAttributeOverridesUsingReflection() {
142142
RootBeanDefinition beanDefinition = new RootBeanDefinition(
143-
ComposedComposedConfigurationWithAttributeOverridesClass.class);
143+
ComposedComposedConfigurationWithAttributeOverridesClass.class);
144144
assertSupportForComposedAnnotation(beanDefinition);
145145
}
146146

147147
@Test
148148
public void postProcessorWorksWithComposedComposedConfigurationWithAttributeOverridesUsingAsm() {
149149
RootBeanDefinition beanDefinition = new RootBeanDefinition(
150-
ComposedComposedConfigurationWithAttributeOverridesClass.class.getName());
150+
ComposedComposedConfigurationWithAttributeOverridesClass.class.getName());
151151
assertSupportForComposedAnnotation(beanDefinition);
152152
}
153153

154154
@Test
155155
public void postProcessorWorksWithMetaComponentScanConfigurationWithAttributeOverridesUsingReflection() {
156156
RootBeanDefinition beanDefinition = new RootBeanDefinition(
157-
MetaComponentScanConfigurationWithAttributeOverridesClass.class);
157+
MetaComponentScanConfigurationWithAttributeOverridesClass.class);
158158
assertSupportForComposedAnnotation(beanDefinition);
159159
}
160160

161161
@Test
162162
public void postProcessorWorksWithMetaComponentScanConfigurationWithAttributeOverridesUsingAsm() {
163163
RootBeanDefinition beanDefinition = new RootBeanDefinition(
164-
MetaComponentScanConfigurationWithAttributeOverridesClass.class.getName());
164+
MetaComponentScanConfigurationWithAttributeOverridesClass.class.getName());
165165
assertSupportForComposedAnnotation(beanDefinition);
166166
}
167167

168168
@Test
169169
public void postProcessorWorksWithMetaComponentScanConfigurationWithAttributeOverridesSubclassUsingReflection() {
170170
RootBeanDefinition beanDefinition = new RootBeanDefinition(
171-
SubMetaComponentScanConfigurationWithAttributeOverridesClass.class);
171+
SubMetaComponentScanConfigurationWithAttributeOverridesClass.class);
172172
assertSupportForComposedAnnotation(beanDefinition);
173173
}
174174

175175
@Test
176176
public void postProcessorWorksWithMetaComponentScanConfigurationWithAttributeOverridesSubclassUsingAsm() {
177177
RootBeanDefinition beanDefinition = new RootBeanDefinition(
178-
SubMetaComponentScanConfigurationWithAttributeOverridesClass.class.getName());
178+
SubMetaComponentScanConfigurationWithAttributeOverridesClass.class.getName());
179179
assertSupportForComposedAnnotation(beanDefinition);
180180
}
181181

@@ -208,8 +208,8 @@ public void postProcessorDoesNotOverrideRegularBeanDefinitions() {
208208
public void postProcessorDoesNotOverrideRegularBeanDefinitionsEvenWithScopedProxy() {
209209
RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
210210
rbd.setResource(new DescriptiveResource("XML or something"));
211-
BeanDefinitionHolder proxied = ScopedProxyUtils.createScopedProxy(new BeanDefinitionHolder(rbd, "bar"),
212-
beanFactory, true);
211+
BeanDefinitionHolder proxied = ScopedProxyUtils.createScopedProxy(
212+
new BeanDefinitionHolder(rbd, "bar"), beanFactory, true);
213213
beanFactory.registerBeanDefinition("bar", proxied.getBeanDefinition());
214214
beanFactory.registerBeanDefinition("config", new RootBeanDefinition(SingletonBeanConfig.class));
215215
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
@@ -296,8 +296,7 @@ public void genericsBasedInjectionWithScopedProxy() {
296296
RootBeanDefinition bd = new RootBeanDefinition(RepositoryInjectionBean.class);
297297
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
298298
beanFactory.registerBeanDefinition("annotatedBean", bd);
299-
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(
300-
ScopedProxyRepositoryConfiguration.class));
299+
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(ScopedProxyRepositoryConfiguration.class));
301300
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
302301
pp.postProcessBeanFactory(beanFactory);
303302
beanFactory.freezeConfiguration();
@@ -332,8 +331,7 @@ public void genericsBasedInjectionWithFactoryBean() {
332331
RootBeanDefinition bd = new RootBeanDefinition(RepositoryFactoryBeanInjectionBean.class);
333332
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
334333
beanFactory.registerBeanDefinition("annotatedBean", bd);
335-
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(
336-
RepositoryFactoryBeanConfiguration.class));
334+
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(RepositoryFactoryBeanConfiguration.class));
337335
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
338336
pp.postProcessBeanFactory(beanFactory);
339337
beanFactory.preInstantiateSingletons();
@@ -374,8 +372,7 @@ public void genericsBasedInjectionWithWildcardWithExtendsMatch() {
374372

375373
@Test
376374
public void genericsBasedInjectionWithWildcardWithGenericExtendsMatch() {
377-
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(
378-
WildcardWithGenericExtendsConfiguration.class));
375+
beanFactory.registerBeanDefinition("configClass", new RootBeanDefinition(WildcardWithGenericExtendsConfiguration.class));
379376
ConfigurationClassPostProcessor pp = new ConfigurationClassPostProcessor();
380377
pp.postProcessBeanFactory(beanFactory);
381378

@@ -574,7 +571,6 @@ public static class ScopedProxyRepositoryConfiguration {
574571
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
575572
public Repository<String> stringRepo() {
576573
return new Repository<String>() {
577-
578574
@Override
579575
public String toString() {
580576
return "Repository<String>";
@@ -586,7 +582,6 @@ public String toString() {
586582
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)
587583
public Repository<Integer> integerRepo() {
588584
return new Repository<Integer>() {
589-
590585
@Override
591586
public String toString() {
592587
return "Repository<Integer>";

spring-core/src/main/java/org/springframework/core/env/EnumerablePropertySource.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,7 @@
1818

1919
import org.apache.commons.logging.Log;
2020
import org.apache.commons.logging.LogFactory;
21+
2122
import org.springframework.util.Assert;
2223

2324
/**
@@ -53,22 +54,23 @@ public EnumerablePropertySource(String name, T source) {
5354
super(name, source);
5455
}
5556

57+
5658
/**
57-
* Return the names of all properties contained by the {@linkplain #getSource()
58-
* source} object (never {@code null}).
59+
* Return the names of all properties contained by the
60+
* {@linkplain #getSource() source} object (never {@code null}).
5961
*/
6062
public abstract String[] getPropertyNames();
6163

6264
/**
6365
* Return whether this {@code PropertySource} contains a property with the given name.
64-
* <p>This implementation checks for the presence of the given name within
65-
* the {@link #getPropertyNames()} array.
66-
* @param name the property to find
66+
* <p>This implementation checks for the presence of the given name within the
67+
* {@link #getPropertyNames()} array.
68+
* @param name the name of the property to find
6769
*/
6870
@Override
6971
public boolean containsProperty(String name) {
70-
Assert.notNull(name, "property name must not be null");
71-
for (String candidate : this.getPropertyNames()) {
72+
Assert.notNull(name, "Property name must not be null");
73+
for (String candidate : getPropertyNames()) {
7274
if (candidate.equals(name)) {
7375
if (logger.isDebugEnabled()) {
7476
logger.debug(String.format("PropertySource [%s] contains '%s'", getName(), name));

spring-core/src/main/java/org/springframework/core/env/PropertySource.java

Lines changed: 37 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,9 @@
1818

1919
import org.apache.commons.logging.Log;
2020
import org.apache.commons.logging.LogFactory;
21+
2122
import org.springframework.util.Assert;
23+
import org.springframework.util.ObjectUtils;
2224

2325
/**
2426
* Abstract base class representing a source of name/value property pairs. The underlying
@@ -55,12 +57,13 @@
5557
*/
5658
public abstract class PropertySource<T> {
5759

58-
protected final Log logger = LogFactory.getLog(this.getClass());
60+
protected final Log logger = LogFactory.getLog(getClass());
5961

6062
protected final String name;
6163

6264
protected final T source;
6365

66+
6467
/**
6568
* Create a new {@code PropertySource} with the given name and source object.
6669
*/
@@ -74,15 +77,15 @@ public PropertySource(String name, T source) {
7477
/**
7578
* Create a new {@code PropertySource} with the given name and with a new {@code Object}
7679
* instance as the underlying source.
77-
* <p>Often useful in testing scenarios when creating
78-
* anonymous implementations that never query an actual source, but rather return
79-
* hard-coded values.
80+
* <p>Often useful in testing scenarios when creating anonymous implementations that
81+
* never query an actual source but rather return hard-coded values.
8082
*/
8183
@SuppressWarnings("unchecked")
8284
public PropertySource(String name) {
8385
this(name, (T) new Object());
8486
}
8587

88+
8689
/**
8790
* Return the name of this {@code PropertySource}
8891
*/
@@ -94,91 +97,75 @@ public String getName() {
9497
* Return the underlying source object for this {@code PropertySource}.
9598
*/
9699
public T getSource() {
97-
return source;
100+
return this.source;
98101
}
99102

100103
/**
101104
* Return whether this {@code PropertySource} contains the given name.
102-
* <p>This implementation simply checks for a null return value
103-
* from {@link #getProperty(String)}. Subclasses may wish to
104-
* implement a more efficient algorithm if possible.
105+
* <p>This implementation simply checks for a {@code null} return value
106+
* from {@link #getProperty(String)}. Subclasses may wish to implement
107+
* a more efficient algorithm if possible.
105108
* @param name the property name to find
106109
*/
107110
public boolean containsProperty(String name) {
108-
return this.getProperty(name) != null;
111+
return (getProperty(name) != null);
109112
}
110113

111114
/**
112-
* Return the value associated with the given name, {@code null} if not found.
115+
* Return the value associated with the given name,
116+
* or {@code null} if not found.
113117
* @param name the property to find
114118
* @see PropertyResolver#getRequiredProperty(String)
115119
*/
116120
public abstract Object getProperty(String name);
117121

118-
/**
119-
* Return a hashcode derived from the {@code name} property of this {@code PropertySource}
120-
* object.
121-
*/
122-
@Override
123-
public int hashCode() {
124-
final int prime = 31;
125-
int result = 1;
126-
result = prime * result + ((this.name == null) ? 0 : this.name.hashCode());
127-
return result;
128-
}
129122

130123
/**
131124
* This {@code PropertySource} object is equal to the given object if:
132125
* <ul>
133-
* <li>they are the same instance
134-
* <li>the {@code name} properties for both objects are equal
126+
* <li>they are the same instance
127+
* <li>the {@code name} properties for both objects are equal
135128
* </ul>
136-
*
137-
* <P>No properties other than {@code name} are evaluated.
129+
* <p>No properties other than {@code name} are evaluated.
138130
*/
139131
@Override
140132
public boolean equals(Object obj) {
141-
if (this == obj)
142-
return true;
143-
if (obj == null)
144-
return false;
145-
if (!(obj instanceof PropertySource))
146-
return false;
147-
PropertySource<?> other = (PropertySource<?>) obj;
148-
if (this.name == null) {
149-
if (other.name != null)
150-
return false;
151-
} else if (!this.name.equals(other.name))
152-
return false;
153-
return true;
133+
return (this == obj || (obj instanceof PropertySource &&
134+
ObjectUtils.nullSafeEquals(this.name, ((PropertySource<?>) obj).name)));
135+
}
136+
137+
/**
138+
* Return a hash code derived from the {@code name} property
139+
* of this {@code PropertySource} object.
140+
*/
141+
@Override
142+
public int hashCode() {
143+
return ObjectUtils.nullSafeHashCode(this.name);
154144
}
155145

156146
/**
157147
* Produce concise output (type and name) if the current log level does not include
158-
* debug. If debug is enabled, produce verbose output including hashcode of the
148+
* debug. If debug is enabled, produce verbose output including the hash code of the
159149
* PropertySource instance and every name/value property pair.
160-
*
161-
* This variable verbosity is useful as a property source such as system properties
150+
* <p>This variable verbosity is useful as a property source such as system properties
162151
* or environment variables may contain an arbitrary number of property pairs,
163152
* potentially leading to difficult to read exception and log messages.
164-
*
165153
* @see Log#isDebugEnabled()
166154
*/
167155
@Override
168156
public String toString() {
169157
if (logger.isDebugEnabled()) {
170158
return String.format("%s@%s [name='%s', properties=%s]",
171-
this.getClass().getSimpleName(), System.identityHashCode(this), this.name, this.source);
159+
getClass().getSimpleName(), System.identityHashCode(this), this.name, this.source);
160+
}
161+
else {
162+
return String.format("%s [name='%s']", getClass().getSimpleName(), this.name);
172163
}
173-
174-
return String.format("%s [name='%s']",
175-
this.getClass().getSimpleName(), this.name);
176164
}
177165

178166

179167
/**
180168
* Return a {@code PropertySource} implementation intended for collection comparison purposes only.
181-
*
182169
* <p>Primarily for internal use, but given a collection of {@code PropertySource} objects, may be
183170
* used as follows:
184171
* <pre class="code">
@@ -189,11 +176,9 @@ public String toString() {
189176
* assert sources.contains(PropertySource.named("sourceB"));
190177
* assert !sources.contains(PropertySource.named("sourceC"));
191178
* }</pre>
192-
*
193179
* The returned {@code PropertySource} will throw {@code UnsupportedOperationException}
194180
* if any methods other than {@code equals(Object)}, {@code hashCode()}, and {@code toString()}
195181
* are called.
196-
*
197182
* @param name the name of the comparison {@code PropertySource} to be created and returned.
198183
*/
199184
public static PropertySource<?> named(String name) {
@@ -209,7 +194,6 @@ public static PropertySource<?> named(String name) {
209194
* {@code ApplicationContext}. In such cases, a stub should be used to hold the
210195
* intended default position/order of the property source, then be replaced
211196
* during context refresh.
212-
*
213197
* @see org.springframework.context.support.AbstractApplicationContext#initPropertySources()
214198
* @see org.springframework.web.context.support.StandardServletEnvironment
215199
* @see org.springframework.web.context.support.ServletContextPropertySource
@@ -221,7 +205,7 @@ public StubPropertySource(String name) {
221205
}
222206

223207
/**
224-
* Always return {@code null}.
208+
* Always returns {@code null}.
225209
*/
226210
@Override
227211
public String getProperty(String name) {
@@ -236,8 +220,7 @@ public String getProperty(String name) {
236220
static class ComparisonPropertySource extends StubPropertySource {
237221

238222
private static final String USAGE_ERROR =
239-
"ComparisonPropertySource instances are for collection comparison " +
240-
"use only";
223+
"ComparisonPropertySource instances are for use with collection comparison only";
241224

242225
public ComparisonPropertySource(String name) {
243226
super(name);

0 commit comments

Comments
 (0)