revised static annotation check

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@1576 50f2f4bb-b051-0410-bef5-90022cba6387
This commit is contained in:
Juergen Hoeller 2009-07-21 14:12:16 +00:00
parent 6cef98e561
commit 53cfa5d4fa
11 changed files with 56 additions and 34 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -241,7 +241,7 @@ public interface BeanFactory {
* @see #getBean * @see #getBean
* @see #isTypeMatch * @see #isTypeMatch
*/ */
Class getType(String name) throws NoSuchBeanDefinitionException; Class<?> getType(String name) throws NoSuchBeanDefinitionException;
/** /**
* Return the aliases for the given bean name, if any. * Return the aliases for the given bean name, if any.

View File

@ -24,6 +24,8 @@ import java.util.Set;
import org.springframework.beans.SimpleTypeConverter; import org.springframework.beans.SimpleTypeConverter;
import org.springframework.beans.TypeConverter; import org.springframework.beans.TypeConverter;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.BeanDefinitionHolder; import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.config.DependencyDescriptor; import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.beans.factory.support.AutowireCandidateQualifier; import org.springframework.beans.factory.support.AutowireCandidateQualifier;
@ -47,12 +49,14 @@ import org.springframework.util.ObjectUtils;
* @see Qualifier * @see Qualifier
* @see Value * @see Value
*/ */
public class QualifierAnnotationAutowireCandidateResolver implements AutowireCandidateResolver { public class QualifierAnnotationAutowireCandidateResolver implements AutowireCandidateResolver, BeanFactoryAware {
private final Set<Class<? extends Annotation>> qualifierTypes; private final Set<Class<? extends Annotation>> qualifierTypes;
private Class<? extends Annotation> valueAnnotationType = Value.class; private Class<? extends Annotation> valueAnnotationType = Value.class;
private BeanFactory beanFactory;
/** /**
* Create a new QualifierAnnotationAutowireCandidateResolver * Create a new QualifierAnnotationAutowireCandidateResolver
@ -112,11 +116,15 @@ public class QualifierAnnotationAutowireCandidateResolver implements AutowireCan
this.valueAnnotationType = valueAnnotationType; this.valueAnnotationType = valueAnnotationType;
} }
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
/** /**
* Determine whether the provided bean definition is an autowire candidate. * Determine whether the provided bean definition is an autowire candidate.
* <p>To be considered a candidate the bean's <em>autowire-candidate</em> * <p>To be considered a candidate the bean's <em>autowire-candidate</em>
* attribute must not have been set to 'false'. Also if an annotation on * attribute must not have been set to 'false'. Also, if an annotation on
* the field or parameter to be autowired is recognized by this bean factory * the field or parameter to be autowired is recognized by this bean factory
* as a <em>qualifier</em>, the bean must 'match' against the annotation as * as a <em>qualifier</em>, the bean must 'match' against the annotation as
* well as any attributes it may contain. The bean definition must contain * well as any attributes it may contain. The bean definition must contain
@ -195,10 +203,18 @@ public class QualifierAnnotationAutowireCandidateResolver implements AutowireCan
if (bd.getResolvedFactoryMethod() != null) { if (bd.getResolvedFactoryMethod() != null) {
targetAnnotation = bd.getResolvedFactoryMethod().getAnnotation(type); targetAnnotation = bd.getResolvedFactoryMethod().getAnnotation(type);
} }
if (targetAnnotation == null && bd.hasBeanClass()) { if (targetAnnotation == null) {
// look for matching annotation on the target class // look for matching annotation on the target class
Class<?> beanClass = bd.getBeanClass(); Class<?> beanType = null;
targetAnnotation = beanClass.getAnnotation(type); if (this.beanFactory != null) {
beanType = this.beanFactory.getType(bdHolder.getBeanName());
}
else if (bd.hasBeanClass()) {
beanType = bd.getBeanClass();
}
if (beanType != null) {
targetAnnotation = ClassUtils.getUserClass(beanType).getAnnotation(type);
}
} }
if (targetAnnotation != null && targetAnnotation.equals(annotation)) { if (targetAnnotation != null && targetAnnotation.equals(annotation)) {
return true; return true;

View File

@ -473,7 +473,7 @@ public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport imp
} }
} }
public Class getType(String name) throws NoSuchBeanDefinitionException { public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
String beanName = transformedBeanName(name); String beanName = transformedBeanName(name);
// Check manually registered singletons. // Check manually registered singletons.

View File

@ -181,7 +181,7 @@ class BeanDefinitionValueResolver {
TypedStringValue typedStringValue = (TypedStringValue) value; TypedStringValue typedStringValue = (TypedStringValue) value;
Object valueObject = evaluate(typedStringValue.getValue()); Object valueObject = evaluate(typedStringValue.getValue());
try { try {
Class resolvedTargetType = resolveTargetType(typedStringValue); Class<?> resolvedTargetType = resolveTargetType(typedStringValue);
if (resolvedTargetType != null) { if (resolvedTargetType != null) {
return this.typeConverter.convertIfNecessary(valueObject, resolvedTargetType); return this.typeConverter.convertIfNecessary(valueObject, resolvedTargetType);
} }
@ -222,7 +222,7 @@ class BeanDefinitionValueResolver {
* @throws ClassNotFoundException if the specified type cannot be resolved * @throws ClassNotFoundException if the specified type cannot be resolved
* @see TypedStringValue#resolveTargetType * @see TypedStringValue#resolveTargetType
*/ */
protected Class resolveTargetType(TypedStringValue value) throws ClassNotFoundException { protected Class<?> resolveTargetType(TypedStringValue value) throws ClassNotFoundException {
if (value.hasTargetType()) { if (value.hasTargetType()) {
return value.getTargetType(); return value.getTargetType();
} }

View File

@ -37,6 +37,7 @@ import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanCurrentlyInCreationException; import org.springframework.beans.factory.BeanCurrentlyInCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.CannotLoadBeanClassException; import org.springframework.beans.factory.CannotLoadBeanClassException;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
@ -180,6 +181,9 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
*/ */
public void setAutowireCandidateResolver(AutowireCandidateResolver autowireCandidateResolver) { public void setAutowireCandidateResolver(AutowireCandidateResolver autowireCandidateResolver) {
Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null"); Assert.notNull(autowireCandidateResolver, "AutowireCandidateResolver must not be null");
if (autowireCandidateResolver instanceof BeanFactoryAware) {
((BeanFactoryAware) autowireCandidateResolver).setBeanFactory(this);
}
this.autowireCandidateResolver = autowireCandidateResolver; this.autowireCandidateResolver = autowireCandidateResolver;
} }
@ -414,16 +418,19 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
beanName = BeanFactoryUtils.transformedBeanName(beanName); beanName = BeanFactoryUtils.transformedBeanName(beanName);
} }
if (!containsBeanDefinition(beanName)) { if (containsBeanDefinition(beanName)) {
if (containsSingleton(beanName)) { return isAutowireCandidate(beanName, getMergedLocalBeanDefinition(beanName), descriptor);
}
else if (containsSingleton(beanName)) {
return isAutowireCandidate(beanName, new RootBeanDefinition(getType(beanName)), descriptor); return isAutowireCandidate(beanName, new RootBeanDefinition(getType(beanName)), descriptor);
} }
else if (getParentBeanFactory() instanceof ConfigurableListableBeanFactory) { else if (getParentBeanFactory() instanceof ConfigurableListableBeanFactory) {
// No bean definition found in this factory -> delegate to parent. // No bean definition found in this factory -> delegate to parent.
return ((ConfigurableListableBeanFactory) getParentBeanFactory()).isAutowireCandidate(beanName, descriptor); return ((ConfigurableListableBeanFactory) getParentBeanFactory()).isAutowireCandidate(beanName, descriptor);
} }
else {
return true;
} }
return isAutowireCandidate(beanName, getMergedLocalBeanDefinition(beanName), descriptor);
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2007 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -58,7 +58,7 @@ public class SimpleInstantiationStrategy implements InstantiationStrategy {
throw new BeanInstantiationException(clazz, "No default constructor found", ex); throw new BeanInstantiationException(clazz, "No default constructor found", ex);
} }
} }
return BeanUtils.instantiateClass(constructorToUse, null); return BeanUtils.instantiateClass(constructorToUse);
} }
else { else {
// Must generate CGLIB subclass. // Must generate CGLIB subclass.

View File

@ -143,7 +143,7 @@ public class StaticListableBeanFactory implements ListableBeanFactory {
return (targetType == null || (type != null && targetType.isAssignableFrom(type))); return (targetType == null || (type != null && targetType.isAssignableFrom(type)));
} }
public Class getType(String name) throws NoSuchBeanDefinitionException { public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
String beanName = BeanFactoryUtils.transformedBeanName(name); String beanName = BeanFactoryUtils.transformedBeanName(name);
Object bean = this.beans.get(beanName); Object bean = this.beans.get(beanName);

View File

@ -898,7 +898,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
return getBeanFactory().isTypeMatch(name, targetType); return getBeanFactory().isTypeMatch(name, targetType);
} }
public Class getType(String name) throws NoSuchBeanDefinitionException { public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
return getBeanFactory().getType(name); return getBeanFactory().getType(name);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2008 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -21,7 +21,6 @@ import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import javax.naming.NameNotFoundException; import javax.naming.NameNotFoundException;
import javax.naming.NamingException; import javax.naming.NamingException;
@ -153,7 +152,7 @@ public class SimpleJndiBeanFactory extends JndiLocatorSupport implements BeanFac
return (targetType == null || (type != null && targetType.isAssignableFrom(type))); return (targetType == null || (type != null && targetType.isAssignableFrom(type)));
} }
public Class getType(String name) throws NoSuchBeanDefinitionException { public Class<?> getType(String name) throws NoSuchBeanDefinitionException {
try { try {
return doGetType(name); return doGetType(name);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2006 the original author or authors. * Copyright 2002-2009 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,14 +16,14 @@
package org.springframework.aop.config; package org.springframework.aop.config;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import static org.easymock.EasyMock.*;
import org.junit.After; import org.junit.After;
import static org.junit.Assert.*;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactory;
/** /**
@ -107,7 +107,7 @@ public final class MethodLocatingFactoryBeanTests {
@Test @Test
public void testSunnyDayPath() throws Exception { public void testSunnyDayPath() throws Exception {
expect(beanFactory.getType(BEAN_NAME)).andReturn(String.class); expect((Class) beanFactory.getType(BEAN_NAME)).andReturn(String.class);
replay(beanFactory); replay(beanFactory);
factory.setTargetBeanName(BEAN_NAME); factory.setTargetBeanName(BEAN_NAME);
@ -117,12 +117,12 @@ public final class MethodLocatingFactoryBeanTests {
assertNotNull(result); assertNotNull(result);
assertTrue(result instanceof Method); assertTrue(result instanceof Method);
Method method = (Method) result; Method method = (Method) result;
assertEquals("Bingo", method.invoke("Bingo", new Object[]{})); assertEquals("Bingo", method.invoke("Bingo"));
} }
@Test(expected=IllegalArgumentException.class) @Test(expected=IllegalArgumentException.class)
public void testWhereMethodCannotBeResolved() { public void testWhereMethodCannotBeResolved() {
expect(beanFactory.getType(BEAN_NAME)).andReturn(String.class); expect((Class) beanFactory.getType(BEAN_NAME)).andReturn(String.class);
replay(beanFactory); replay(beanFactory);
factory.setTargetBeanName(BEAN_NAME); factory.setTargetBeanName(BEAN_NAME);

View File

@ -368,7 +368,7 @@ public abstract class ClassUtils {
* @param clazz the class to check * @param clazz the class to check
* @return the user-defined class * @return the user-defined class
*/ */
public static Class getUserClass(Class clazz) { public static Class<?> getUserClass(Class clazz) {
return (clazz != null && clazz.getName().contains(CGLIB_CLASS_SEPARATOR) ? return (clazz != null && clazz.getName().contains(CGLIB_CLASS_SEPARATOR) ?
clazz.getSuperclass() : clazz); clazz.getSuperclass() : clazz);
} }