Polishing

This commit is contained in:
Juergen Hoeller
2014-09-17 19:10:33 +02:00
parent 3a1f7b6d14
commit e819999c08
15 changed files with 172 additions and 147 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@@ -46,13 +46,13 @@ class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor {
private final Method writeMethod;
private final Class<?> propertyEditorClass;
private volatile Set<Method> ambiguousWriteMethods;
private MethodParameter writeMethodParameter;
private Class<?> propertyType;
private MethodParameter writeMethodParameter;
private final Class<?> propertyEditorClass;
public GenericTypeAwarePropertyDescriptor(Class<?> beanClass, String propertyName,
@@ -60,8 +60,11 @@ class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor {
throws IntrospectionException {
super(propertyName, null, null);
if (beanClass == null) {
throw new IntrospectionException("Bean class must not be null");
}
this.beanClass = beanClass;
this.propertyEditorClass = propertyEditorClass;
Method readMethodToUse = BridgeMethodResolver.findBridgedMethod(readMethod);
Method writeMethodToUse = BridgeMethodResolver.findBridgedMethod(writeMethod);
@@ -93,8 +96,11 @@ class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor {
this.ambiguousWriteMethods = ambiguousCandidates;
}
}
this.propertyEditorClass = propertyEditorClass;
}
public Class<?> getBeanClass() {
return this.beanClass;
}
@@ -120,9 +126,15 @@ class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor {
return this.writeMethod;
}
@Override
public Class<?> getPropertyEditorClass() {
return this.propertyEditorClass;
public synchronized MethodParameter getWriteMethodParameter() {
if (this.writeMethod == null) {
return null;
}
if (this.writeMethodParameter == null) {
this.writeMethodParameter = new MethodParameter(this.writeMethod, 0);
GenericTypeResolver.resolveParameterType(this.writeMethodParameter, this.beanClass);
}
return this.writeMethodParameter;
}
@Override
@@ -144,15 +156,9 @@ class GenericTypeAwarePropertyDescriptor extends PropertyDescriptor {
return this.propertyType;
}
public synchronized MethodParameter getWriteMethodParameter() {
if (this.writeMethod == null) {
return null;
}
if (this.writeMethodParameter == null) {
this.writeMethodParameter = new MethodParameter(this.writeMethod, 0);
GenericTypeResolver.resolveParameterType(this.writeMethodParameter, this.beanClass);
}
return this.writeMethodParameter;
@Override
public Class<?> getPropertyEditorClass() {
return this.propertyEditorClass;
}
}

View File

@@ -238,7 +238,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
if (requiredConstructor != null) {
throw new BeanCreationException(beanName,
"Invalid autowire-marked constructor: " + candidate +
". Found another constructor with 'required' Autowired annotation: " +
". Found constructor with 'required' Autowired annotation already: " +
requiredConstructor);
}
if (candidate.getParameterTypes().length == 0) {
@@ -250,7 +250,7 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
if (!candidates.isEmpty()) {
throw new BeanCreationException(beanName,
"Invalid autowire-marked constructors: " + candidates +
". Found another constructor with 'required' Autowired annotation: " +
". Found constructor with 'required' Autowired annotation: " +
candidate);
}
requiredConstructor = candidate;
@@ -484,14 +484,14 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
value = resolvedCachedArgument(beanName, this.cachedFieldValue);
}
else {
DependencyDescriptor descriptor = new DependencyDescriptor(field, this.required);
DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
Set<String> autowiredBeanNames = new LinkedHashSet<String>(1);
TypeConverter typeConverter = beanFactory.getTypeConverter();
value = beanFactory.resolveDependency(descriptor, beanName, autowiredBeanNames, typeConverter);
value = beanFactory.resolveDependency(desc, beanName, autowiredBeanNames, typeConverter);
synchronized (this) {
if (!this.cached) {
if (value != null || this.required) {
this.cachedFieldValue = descriptor;
this.cachedFieldValue = desc;
registerDependentBeans(beanName, autowiredBeanNames);
if (autowiredBeanNames.size() == 1) {
String autowiredBeanName = autowiredBeanNames.iterator().next();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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,14 +16,6 @@
package org.springframework.beans.factory.annotation;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.Serializable;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
@@ -33,6 +25,7 @@ import java.util.List;
import java.util.Map;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.FactoryBean;
@@ -49,16 +42,15 @@ import org.springframework.tests.sample.beans.NestedTestBean;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.util.SerializationTestUtils;
import static org.junit.Assert.*;
/**
* Unit tests for {@link AutowiredAnnotationBeanPostProcessor}.
*
* @author Juergen Hoeller
* @author Mark Fisher
* @author Sam Brannen
* @author Chris Beams
*/
public final class AutowiredAnnotationBeanPostProcessorTests {
public class AutowiredAnnotationBeanPostProcessorTests {
@Test
public void testIncompleteBeanDefinition() {
@@ -963,7 +955,6 @@ public final class AutowiredAnnotationBeanPostProcessorTests {
private TestBean testBean2;
@Autowired
public void setTestBean2(TestBean testBean2) {
if (this.testBean2 != null) {