Find annotations on implemented generic interface methods as well

Issue: SPR-16060

(cherry picked from commit 23d4862)
This commit is contained in:
Juergen Hoeller
2018-07-18 19:44:30 +02:00
parent 4341838a21
commit b72594d799
2 changed files with 45 additions and 9 deletions

View File

@@ -39,6 +39,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.BridgeMethodResolver;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -588,8 +589,7 @@ public abstract class AnnotationUtils {
Set<Method> annotatedMethods = getAnnotatedMethodsInBaseType(ifc);
if (!annotatedMethods.isEmpty()) {
for (Method annotatedMethod : annotatedMethods) {
if (annotatedMethod.getName().equals(method.getName()) &&
Arrays.equals(annotatedMethod.getParameterTypes(), method.getParameterTypes())) {
if (isOverride(method, annotatedMethod)) {
A annotation = getAnnotation(annotatedMethod, annotationType);
if (annotation != null) {
return annotation;
@@ -647,6 +647,23 @@ public abstract class AnnotationUtils {
return true;
}
private static boolean isOverride(Method method, Method candidate) {
if (!candidate.getName().equals(method.getName()) ||
candidate.getParameterCount() != method.getParameterCount()) {
return false;
}
Class<?>[] paramTypes = method.getParameterTypes();
if (Arrays.equals(candidate.getParameterTypes(), paramTypes)) {
return true;
}
for (int i = 0; i < paramTypes.length; i++) {
if (paramTypes[i] != ResolvableType.forMethodParameter(candidate, i, method.getDeclaringClass()).resolve()) {
return false;
}
}
return true;
}
/**
* Find a single {@link Annotation} of {@code annotationType} on the
* supplied {@link Class}, traversing its interfaces, annotations, and
@@ -1833,8 +1850,8 @@ public abstract class AnnotationUtils {
/**
* Determine if the supplied method is an "annotationType" method.
* @return {@code true} if the method is an "annotationType" method
* @see Annotation#annotationType()
* @since 4.2
* @see Annotation#annotationType()
*/
static boolean isAnnotationTypeMethod(@Nullable Method method) {
return (method != null && method.getName().equals("annotationType") && method.getParameterCount() == 0);
@@ -2047,7 +2064,7 @@ public abstract class AnnotationUtils {
* @see #getAttributeAliasNames
* @see #getAttributeOverrideName
*/
private static class AliasDescriptor {
private static final class AliasDescriptor {
private final Method sourceAttribute;