LANG-1518 - fix searchSupers for generic classes (#494)

* fix searchSupers for generic classes

* fix checkstyle
This commit is contained in:
Michele Preti 2020-02-22 14:28:33 +01:00 committed by GitHub
parent eb8d069089
commit f4c2ed4995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 11 deletions

View File

@ -974,20 +974,16 @@ public static <A extends Annotation> A getAnnotation(final Method method, final
final Class<?> mcls = method.getDeclaringClass(); final Class<?> mcls = method.getDeclaringClass();
final List<Class<?>> classes = getAllSuperclassesAndInterfaces(mcls); final List<Class<?>> classes = getAllSuperclassesAndInterfaces(mcls);
for (final Class<?> acls : classes) { for (final Class<?> acls : classes) {
Method equivalentMethod; Method equivalentMethod = (ignoreAccess ? MethodUtils.getMatchingMethod(acls, method.getName(), method.getParameterTypes())
try { : MethodUtils.getMatchingAccessibleMethod(acls, method.getName(), method.getParameterTypes()));
equivalentMethod = (ignoreAccess ? acls.getDeclaredMethod(method.getName(), method.getParameterTypes()) if (equivalentMethod != null) {
: acls.getMethod(method.getName(), method.getParameterTypes()));
} catch (final NoSuchMethodException e) {
// if not found, just keep searching
continue;
}
annotation = equivalentMethod.getAnnotation(annotationCls); annotation = equivalentMethod.getAnnotation(annotationCls);
if (annotation != null) { if (annotation != null) {
break; break;
} }
} }
} }
}
return annotation; return annotation;
} }

View File

@ -799,6 +799,15 @@ public void testGetAnnotationSearchSupersAndIgnoreAccess() throws NoSuchMethodEx
Annotated.class, true, true)); Annotated.class, true, true));
assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("publicAnnotatedMethod"), assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("publicAnnotatedMethod"),
Annotated.class, true, true)); Annotated.class, true, true));
assertNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("parentNotAnnotatedMethod", String.class),
Annotated.class, true, true));
assertNotNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("parentProtectedAnnotatedMethod", String.class),
Annotated.class, true, true));
assertNotNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getDeclaredMethod("privateAnnotatedMethod", String.class),
Annotated.class, true, true));
assertNotNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("publicAnnotatedMethod", String.class),
Annotated.class, true, true));
} }
@Test @Test
@ -827,6 +836,15 @@ public void testGetAnnotationSearchSupersButNotIgnoreAccess() throws NoSuchMetho
Annotated.class, true, false)); Annotated.class, true, false));
assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("publicAnnotatedMethod"), assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("publicAnnotatedMethod"),
Annotated.class, true, false)); Annotated.class, true, false));
assertNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("parentNotAnnotatedMethod", String.class),
Annotated.class, true, false));
assertNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("parentProtectedAnnotatedMethod", String.class),
Annotated.class, true, false));
assertNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getDeclaredMethod("privateAnnotatedMethod", String.class),
Annotated.class, true, false));
assertNotNull(MethodUtils.getAnnotation(StringParameterizedChild.class.getMethod("publicAnnotatedMethod", String.class),
Annotated.class, true, false));
} }
@Test @Test

View File

@ -25,4 +25,10 @@ public class GenericParent<T> implements GenericConsumer<T> {
public void consume(final T t) { public void consume(final T t) {
} }
@Annotated
protected void parentProtectedAnnotatedMethod(final T t) {
}
public void parentNotAnnotatedMethod(final T t) {
}
} }

View File

@ -24,4 +24,19 @@ public class StringParameterizedChild extends GenericParent<String> {
public void consume(final String t) { public void consume(final String t) {
super.consume(t); super.consume(t);
} }
@Override
public void parentProtectedAnnotatedMethod(final String t) {
}
public void parentNotAnnotatedMethod(final String t) {
}
@Annotated
private void privateAnnotatedMethod(final String t) {
}
@Annotated
public void publicAnnotatedMethod(final String t) {
}
} }