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,17 +974,13 @@ public class MethodUtils {
final Class<?> mcls = method.getDeclaringClass();
final List<Class<?>> classes = getAllSuperclassesAndInterfaces(mcls);
for (final Class<?> acls : classes) {
Method equivalentMethod;
try {
equivalentMethod = (ignoreAccess ? acls.getDeclaredMethod(method.getName(), method.getParameterTypes())
: acls.getMethod(method.getName(), method.getParameterTypes()));
} catch (final NoSuchMethodException e) {
// if not found, just keep searching
continue;
}
annotation = equivalentMethod.getAnnotation(annotationCls);
if (annotation != null) {
break;
Method equivalentMethod = (ignoreAccess ? MethodUtils.getMatchingMethod(acls, method.getName(), method.getParameterTypes())
: MethodUtils.getMatchingAccessibleMethod(acls, method.getName(), method.getParameterTypes()));
if (equivalentMethod != null) {
annotation = equivalentMethod.getAnnotation(annotationCls);
if (annotation != null) {
break;
}
}
}
}

View File

@ -799,6 +799,15 @@ public class MethodUtilsTest {
Annotated.class, true, true));
assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("publicAnnotatedMethod"),
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
@ -827,6 +836,15 @@ public class MethodUtilsTest {
Annotated.class, true, false));
assertNotNull(MethodUtils.getAnnotation(PublicChild.class.getMethod("publicAnnotatedMethod"),
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

View File

@ -25,4 +25,10 @@ public class GenericParent<T> implements GenericConsumer<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) {
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) {
}
}