Skip to content

Commit db1cb13

Browse files
committed
Polish
Issue: SPR-6870
1 parent f55a4a1 commit db1cb13

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121
import java.io.ObjectInputStream;
2222
import java.io.ObjectStreamException;
2323
import java.io.Serializable;
24+
2425
import java.lang.annotation.Annotation;
2526
import java.lang.ref.Reference;
2627
import java.lang.ref.WeakReference;
27-
import java.lang.reflect.ParameterizedType;
28-
import java.lang.reflect.Type;
28+
2929
import java.security.AccessController;
3030
import java.security.PrivilegedAction;
31+
3132
import java.util.ArrayList;
3233
import java.util.Arrays;
3334
import java.util.Collection;
@@ -38,6 +39,7 @@
3839
import java.util.Map;
3940
import java.util.Set;
4041
import java.util.concurrent.ConcurrentHashMap;
42+
4143
import javax.inject.Provider;
4244

4345
import org.springframework.beans.BeansException;
@@ -98,7 +100,7 @@
98100
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory
99101
implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable {
100102

101-
private static Class javaxInjectProviderClass = null;
103+
private static Class<?> javaxInjectProviderClass = null;
102104

103105
static {
104106
ClassLoader cl = DefaultListableBeanFactory.class.getClassLoader();
@@ -128,7 +130,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
128130
private AutowireCandidateResolver autowireCandidateResolver = new SimpleAutowireCandidateResolver();
129131

130132
/** Map from dependency type to corresponding autowired value */
131-
private final Map<Class, Object> resolvableDependencies = new HashMap<Class, Object>();
133+
private final Map<Class<?>, Object> resolvableDependencies = new HashMap<Class<?>, Object>();
132134

133135
/** Map of bean definition objects, keyed by bean name */
134136
private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<String, BeanDefinition>();
@@ -294,11 +296,11 @@ public String[] getBeanDefinitionNames() {
294296
}
295297
}
296298

297-
public String[] getBeanNamesForType(Class type) {
299+
public String[] getBeanNamesForType(Class<?> type) {
298300
return getBeanNamesForType(type, true, true);
299301
}
300302

301-
public String[] getBeanNamesForType(Class type, boolean includeNonSingletons, boolean allowEagerInit) {
303+
public String[] getBeanNamesForType(Class<?> type, boolean includeNonSingletons, boolean allowEagerInit) {
302304
List<String> result = new ArrayList<String>();
303305

304306
// Check all bean definitions.
@@ -441,7 +443,7 @@ public Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> an
441443
*/
442444
public <A extends Annotation> A findAnnotationOnBean(String beanName, Class<A> annotationType) {
443445
A ann = null;
444-
Class beanType = getType(beanName);
446+
Class<?> beanType = getType(beanName);
445447
if (beanType != null) {
446448
ann = AnnotationUtils.findAnnotation(beanType, annotationType);
447449
}
@@ -564,18 +566,18 @@ public void preInstantiateSingletons() throws BeansException {
564566
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
565567
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
566568
if (isFactoryBean(beanName)) {
567-
final FactoryBean factory = (FactoryBean) getBean(FACTORY_BEAN_PREFIX + beanName);
569+
final FactoryBean<?> factory = (FactoryBean<?>) getBean(FACTORY_BEAN_PREFIX + beanName);
568570
boolean isEagerInit;
569571
if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
570572
isEagerInit = AccessController.doPrivileged(new PrivilegedAction<Boolean>() {
571573
public Boolean run() {
572-
return ((SmartFactoryBean) factory).isEagerInit();
574+
return ((SmartFactoryBean<?>) factory).isEagerInit();
573575
}
574576
}, getAccessControlContext());
575577
}
576578
else {
577579
isEagerInit = (factory instanceof SmartFactoryBean &&
578-
((SmartFactoryBean) factory).isEagerInit());
580+
((SmartFactoryBean<?>) factory).isEagerInit());
579581
}
580582
if (isEagerInit) {
581583
getBean(beanName);
@@ -723,7 +725,7 @@ protected Object doResolveDependency(DependencyDescriptor descriptor, Class<?> t
723725
}
724726

725727
if (type.isArray()) {
726-
Class componentType = type.getComponentType();
728+
Class<?> componentType = type.getComponentType();
727729
Map<String, Object> matchingBeans = findAutowireCandidates(beanName, componentType, descriptor);
728730
if (matchingBeans.isEmpty()) {
729731
if (descriptor.isRequired()) {
@@ -738,7 +740,7 @@ protected Object doResolveDependency(DependencyDescriptor descriptor, Class<?> t
738740
return converter.convertIfNecessary(matchingBeans.values(), type);
739741
}
740742
else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
741-
Class elementType = descriptor.getCollectionType();
743+
Class<?> elementType = descriptor.getCollectionType();
742744
if (elementType == null) {
743745
if (descriptor.isRequired()) {
744746
throw new FatalBeanException("No element type declared for collection [" + type.getName() + "]");
@@ -759,15 +761,15 @@ else if (Collection.class.isAssignableFrom(type) && type.isInterface()) {
759761
return converter.convertIfNecessary(matchingBeans.values(), type);
760762
}
761763
else if (Map.class.isAssignableFrom(type) && type.isInterface()) {
762-
Class keyType = descriptor.getMapKeyType();
764+
Class<?> keyType = descriptor.getMapKeyType();
763765
if (keyType == null || !String.class.isAssignableFrom(keyType)) {
764766
if (descriptor.isRequired()) {
765767
throw new FatalBeanException("Key type [" + keyType + "] of map [" + type.getName() +
766768
"] must be assignable to [java.lang.String]");
767769
}
768770
return null;
769771
}
770-
Class valueType = descriptor.getMapValueType();
772+
Class<?> valueType = descriptor.getMapValueType();
771773
if (valueType == null) {
772774
if (descriptor.isRequired()) {
773775
throw new FatalBeanException("No value type declared for map [" + type.getName() + "]");
@@ -828,12 +830,12 @@ else if (Map.class.isAssignableFrom(type) && type.isInterface()) {
828830
* @see #autowireConstructor
829831
*/
830832
protected Map<String, Object> findAutowireCandidates(
831-
String beanName, Class requiredType, DependencyDescriptor descriptor) {
833+
String beanName, Class<?> requiredType, DependencyDescriptor descriptor) {
832834

833835
String[] candidateNames = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
834836
this, requiredType, true, descriptor.isEager());
835837
Map<String, Object> result = new LinkedHashMap<String, Object>(candidateNames.length);
836-
for (Class autowiringType : this.resolvableDependencies.keySet()) {
838+
for (Class<?> autowiringType : this.resolvableDependencies.keySet()) {
837839
if (autowiringType.isAssignableFrom(requiredType)) {
838840
Object autowiringValue = this.resolvableDependencies.get(autowiringType);
839841
autowiringValue = AutowireUtils.resolveAutowiringValue(autowiringValue, requiredType);
@@ -918,7 +920,7 @@ protected boolean matchesBeanName(String beanName, String candidateName) {
918920
* Raise a NoSuchBeanDefinitionException for an unresolvable dependency.
919921
*/
920922
private void raiseNoSuchBeanDefinitionException(
921-
Class type, String dependencyDescription, DependencyDescriptor descriptor)
923+
Class<?> type, String dependencyDescription, DependencyDescriptor descriptor)
922924
throws NoSuchBeanDefinitionException {
923925

924926
throw new NoSuchBeanDefinitionException(type, dependencyDescription,
@@ -967,6 +969,7 @@ protected Object writeReplace() throws ObjectStreamException {
967969
* Minimal id reference to the factory.
968970
* Resolved to the actual factory instance on deserialization.
969971
*/
972+
@SuppressWarnings("serial")
970973
private static class SerializedBeanFactoryReference implements Serializable {
971974

972975
private final String id;
@@ -976,7 +979,7 @@ public SerializedBeanFactoryReference(String id) {
976979
}
977980

978981
private Object readResolve() {
979-
Reference ref = serializableFactories.get(this.id);
982+
Reference<?> ref = serializableFactories.get(this.id);
980983
if (ref == null) {
981984
throw new IllegalStateException(
982985
"Cannot deserialize BeanFactory with id " + this.id + ": no factory registered for this id");
@@ -994,7 +997,8 @@ private Object readResolve() {
994997
/**
995998
* Serializable ObjectFactory for lazy resolution of a dependency.
996999
*/
997-
private class DependencyObjectFactory implements ObjectFactory, Serializable {
1000+
@SuppressWarnings("serial")
1001+
private class DependencyObjectFactory implements ObjectFactory<Object>, Serializable {
9981002

9991003
private final DependencyDescriptor descriptor;
10001004

@@ -1015,7 +1019,8 @@ public Object getObject() throws BeansException {
10151019
/**
10161020
* Serializable ObjectFactory for lazy resolution of a dependency.
10171021
*/
1018-
private class DependencyProvider extends DependencyObjectFactory implements Provider {
1022+
@SuppressWarnings("serial")
1023+
private class DependencyProvider extends DependencyObjectFactory implements Provider<Object> {
10191024

10201025
public DependencyProvider(DependencyDescriptor descriptor, String beanName) {
10211026
super(descriptor, beanName);

spring-beans/src/test/java/org/springframework/beans/factory/DefaultListableBeanFactoryTests.java

Lines changed: 13 additions & 15 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-2012 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.
@@ -16,26 +16,18 @@
1616

1717
package org.springframework.beans.factory;
1818

19-
import static org.hamcrest.CoreMatchers.is;
20-
import static org.junit.Assert.assertEquals;
21-
import static org.junit.Assert.assertFalse;
22-
import static org.junit.Assert.assertNotNull;
23-
import static org.junit.Assert.assertNotSame;
24-
import static org.junit.Assert.assertNull;
25-
import static org.junit.Assert.assertSame;
26-
import static org.junit.Assert.assertThat;
27-
import static org.junit.Assert.assertTrue;
28-
import static org.junit.Assert.fail;
29-
import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition;
30-
3119
import java.lang.reflect.Field;
20+
3221
import java.net.MalformedURLException;
22+
3323
import java.security.AccessControlContext;
3424
import java.security.AccessController;
3525
import java.security.Principal;
3626
import java.security.PrivilegedAction;
27+
3728
import java.text.NumberFormat;
3829
import java.text.ParseException;
30+
3931
import java.util.Arrays;
4032
import java.util.Iterator;
4133
import java.util.List;
@@ -48,8 +40,10 @@
4840

4941
import org.apache.commons.logging.Log;
5042
import org.apache.commons.logging.LogFactory;
43+
5144
import org.junit.Ignore;
5245
import org.junit.Test;
46+
5347
import org.springframework.beans.BeansException;
5448
import org.springframework.beans.MutablePropertyValues;
5549
import org.springframework.beans.NotWritablePropertyException;
@@ -92,6 +86,10 @@
9286
import test.beans.NestedTestBean;
9387
import test.beans.TestBean;
9488

89+
import static org.hamcrest.CoreMatchers.*;
90+
91+
import static org.junit.Assert.*;
92+
9593
/**
9694
* Tests properties population and autowire behavior.
9795
*
@@ -2159,8 +2157,8 @@ public Object run() {
21592157
@Test
21602158
public void testContainsBeanReturnsTrueEvenForAbstractBeanDefinition() {
21612159
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
2162-
bf.registerBeanDefinition("abs",
2163-
rootBeanDefinition(TestBean.class).setAbstract(true).getBeanDefinition());
2160+
bf.registerBeanDefinition("abs", BeanDefinitionBuilder
2161+
.rootBeanDefinition(TestBean.class).setAbstract(true).getBeanDefinition());
21642162
assertThat(bf.containsBean("abs"), is(true));
21652163
assertThat(bf.containsBean("bogus"), is(false));
21662164
}

0 commit comments

Comments
 (0)