diff --git a/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java b/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java index 303bd3da68b..0302a3129ff 100644 --- a/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java +++ b/org.springframework.beans/src/main/java/org/springframework/beans/factory/annotation/QualifierAnnotationAutowireCandidateResolver.java @@ -157,7 +157,7 @@ public class QualifierAnnotationAutowireCandidateResolver implements AutowireCan if (methodParam != null) { Method method = methodParam.getMethod(); if (method == null || void.class.equals(method.getReturnType())) { - match = checkQualifiers(bdHolder, methodParam.getAnnotations()); + match = checkQualifiers(bdHolder, methodParam.getMethodAnnotations()); } } } @@ -274,7 +274,7 @@ public class QualifierAnnotationAutowireCandidateResolver implements AutowireCan if (value == null) { MethodParameter methodParam = descriptor.getMethodParameter(); if (methodParam != null) { - value = findValue(methodParam.getAnnotations()); + value = findValue(methodParam.getMethodAnnotations()); } } return value; diff --git a/org.springframework.core/src/main/java/org/springframework/core/MethodParameter.java b/org.springframework.core/src/main/java/org/springframework/core/MethodParameter.java index 8f8cfe35673..2ae4361230f 100644 --- a/org.springframework.core/src/main/java/org/springframework/core/MethodParameter.java +++ b/org.springframework.core/src/main/java/org/springframework/core/MethodParameter.java @@ -31,10 +31,6 @@ import org.springframework.util.Assert; * a Method or Constructor plus a parameter index and a nested type index for * a declared generic type. Useful as a specification object to pass along. * - *

Used by {@link GenericCollectionTypeResolver}, - * {@link org.springframework.beans.BeanWrapperImpl} and - * {@link org.springframework.beans.factory.support.AbstractBeanFactory}. - * * @author Juergen Hoeller * @author Rob Harrop * @author Andy Clement @@ -212,22 +208,49 @@ public class MethodParameter { /** * Return the annotations associated with the target method/constructor itself. */ - public Annotation[] getAnnotations() { + public Annotation[] getMethodAnnotations() { return (this.method != null ? this.method.getAnnotations() : this.constructor.getAnnotations()); } + /** + * Return the method/constructor annotation of the given type, if available. + * @param annotationType the annotation type to look for + * @return the annotation object, or null if not found + */ + @SuppressWarnings("unchecked") + public T getMethodAnnotation(Class annotationType) { + return (this.method != null ? this.method.getAnnotation(annotationType) : + (T) this.constructor.getAnnotation(annotationType)); + } + /** * Return the annotations associated with the specific method/constructor parameter. */ public Annotation[] getParameterAnnotations() { if (this.parameterAnnotations == null) { - Annotation[][] annotationArray = (this.method != null) ? - this.method.getParameterAnnotations() : this.constructor.getParameterAnnotations(); + Annotation[][] annotationArray = (this.method != null ? + this.method.getParameterAnnotations() : this.constructor.getParameterAnnotations()); this.parameterAnnotations = annotationArray[this.parameterIndex]; } return this.parameterAnnotations; } + /** + * Return the parameter annotation of the given type, if available. + * @param annotationType the annotation type to look for + * @return the annotation object, or null if not found + */ + @SuppressWarnings("unchecked") + public T getParameterAnnotation(Class annotationType) { + Annotation[] anns = getParameterAnnotations(); + for (Annotation ann : anns) { + if (annotationType.isInstance(ann)) { + return (T) ann; + } + } + return null; + } + /** * Initialize parameter name discovery for this method parameter. *

This method does not actually try to retrieve the parameter name at