[SPR-3635] [SPR-5039] [SPR-5813] JMX annotation inheritance fixed
This commit is contained in:
@@ -101,6 +101,9 @@ public abstract class AnnotationUtils {
|
||||
public static <A extends Annotation> A findAnnotation(Method method, Class<A> annotationType) {
|
||||
A annotation = getAnnotation(method, annotationType);
|
||||
Class<?> cl = method.getDeclaringClass();
|
||||
if(annotation == null) {
|
||||
annotation = searchForAnnotationOnInterfaces(method, annotationType, cl.getInterfaces());
|
||||
}
|
||||
while (annotation == null) {
|
||||
cl = cl.getSuperclass();
|
||||
if (cl == null || cl == Object.class) {
|
||||
@@ -109,6 +112,9 @@ public abstract class AnnotationUtils {
|
||||
try {
|
||||
Method equivalentMethod = cl.getDeclaredMethod(method.getName(), method.getParameterTypes());
|
||||
annotation = getAnnotation(equivalentMethod, annotationType);
|
||||
if(annotation == null) {
|
||||
annotation = searchForAnnotationOnInterfaces(method, annotationType, cl.getInterfaces());
|
||||
}
|
||||
}
|
||||
catch (NoSuchMethodException ex) {
|
||||
// We're done...
|
||||
@@ -117,6 +123,24 @@ public abstract class AnnotationUtils {
|
||||
return annotation;
|
||||
}
|
||||
|
||||
private static <A extends Annotation> A searchForAnnotationOnInterfaces(Method method, Class<A> annotationType, Class[] ifaces) {
|
||||
A annotation = null;
|
||||
for (Class<?> iface : ifaces) {
|
||||
Method equivalentMethod = null;
|
||||
try {
|
||||
equivalentMethod = iface.getDeclaredMethod(method.getName(), method.getParameterTypes());
|
||||
annotation = getAnnotation(equivalentMethod, annotationType);
|
||||
}
|
||||
catch (NoSuchMethodException e) {
|
||||
// skip this interface - it doesn't have the method
|
||||
}
|
||||
if (annotation != null) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return annotation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a single {@link Annotation} of <code>annotationType</code> from the
|
||||
* supplied {@link Class}, traversing its interfaces and super classes
|
||||
|
||||
@@ -188,6 +188,22 @@ public class AnnotationUtilsTests {
|
||||
assertEquals(Ordered.LOWEST_PRECEDENCE, AnnotationUtils.getDefaultValue(Order.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAnnotationFromInterface() throws Exception {
|
||||
|
||||
Method method = ImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
|
||||
Order order = findAnnotation(method, Order.class);
|
||||
assertNotNull(order);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFindAnnotationFromInterfaceOnSuper() throws Exception {
|
||||
|
||||
Method method = SubOfImplementsInterfaceWithAnnotatedMethod.class.getMethod("foo");
|
||||
Order order = findAnnotation(method, Order.class);
|
||||
assertNotNull(order);
|
||||
}
|
||||
|
||||
public static interface AnnotatedInterface {
|
||||
|
||||
@Order(0)
|
||||
@@ -286,6 +302,25 @@ public class AnnotationUtilsTests {
|
||||
public static class SubNonInheritedAnnotationClass extends NonInheritedAnnotationClass {
|
||||
}
|
||||
|
||||
|
||||
public static interface InterfaceWithAnnotatedMethod {
|
||||
|
||||
@Order
|
||||
void foo();
|
||||
}
|
||||
|
||||
public static class ImplementsInterfaceWithAnnotatedMethod implements InterfaceWithAnnotatedMethod {
|
||||
|
||||
public void foo() {
|
||||
}
|
||||
}
|
||||
|
||||
public static class SubOfImplementsInterfaceWithAnnotatedMethod extends ImplementsInterfaceWithAnnotatedMethod {
|
||||
|
||||
public void foo() {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
|
||||
Reference in New Issue
Block a user