74

我有一个父类Parent和一个子类Child,定义如下:

class Parent {
    @MyAnnotation("hello")
    void foo() {
        // implementation irrelevant
    }
}
class Child extends Parent {
    @Override
    foo() {
        // implementation irrelevant
    }
}

如果我获得Method参考Child::foochildFoo.getAnnotation(MyAnnotation.class)会给我@MyAnnotation吗?还是会null

我对注解如何或是否与 Java 继承一起工作更感兴趣。

4

4 回答 4

87

从http://www.eclipse.org/aspectj/doc/released/adk15notebook/annotations.html#annotation-inheritance逐字复制:

注释继承

了解与注释继承相关的规则很重要,因为这些规则与基于注释存在或不存在的连接点匹配有关。

默认情况下,注释不会被继承。给定以下程序

        @MyAnnotation
        class Super {
          @Oneway public void foo() {}
        }

        class Sub extends Super {
          public void foo() {}
        }

然后Sub没有MyAnnotation注释,Sub.foo()也不是@Oneway方法,尽管它覆盖了Super.foo()which is。

如果注解类型具有元注解@Inherited,则类上该类型的注解将导致该注解被子类继承。所以,在上面的例子中,如果MyAnnotation类型有@Inherited属性,那么Sub就会有MyAnnotation注解。

@Inherited当用于注释类型以外的任何内容时,注释不会被继承。实现一个或多个接口的类型永远不会从它实现的接口继承任何注释。

于 2012-04-10T03:10:21.837 回答
12

您已经找到了答案:JDK 中没有方法注释继承的规定。

但是爬上超类链来寻找带注释的方法也很容易实现:

/**
 * Climbs the super-class chain to find the first method with the given signature which is
 * annotated with the given annotation.
 *
 * @return A method of the requested signature, applicable to all instances of the given
 *         class, and annotated with the required annotation
 * @throws NoSuchMethodException If no method was found that matches this description
 */
public Method getAnnotatedMethod(Class<? extends Annotation> annotation,
                                 Class c, String methodName, Class... parameterTypes)
        throws NoSuchMethodException {

    Method method = c.getMethod(methodName, parameterTypes);
    if (method.isAnnotationPresent(annotation)) {
        return method;
    }

    return getAnnotatedMethod(annotation, c.getSuperclass(), methodName, parameterTypes);
}
于 2012-05-20T13:20:44.013 回答
9

使用 Spring Core 你可以解决

AnnotationUtils.java

于 2015-05-08T13:07:00.160 回答
2

虽然所问问题的答案是 JavaMethod.getAnnotation()不考虑被覆盖的方法,但有时找到这些注释很有用。这是我目前正在使用的 Saintali 答案的更完整版本:

public static <A extends Annotation> A getInheritedAnnotation(
    Class<A> annotationClass, AnnotatedElement element)
{
    A annotation = element.getAnnotation(annotationClass);
    if (annotation == null && element instanceof Method)
        annotation = getOverriddenAnnotation(annotationClass, (Method) element);
    return annotation;
}

private static <A extends Annotation> A getOverriddenAnnotation(
    Class<A> annotationClass, Method method)
{
    final Class<?> methodClass = method.getDeclaringClass();
    final String name = method.getName();
    final Class<?>[] params = method.getParameterTypes();

    // prioritize all superclasses over all interfaces
    final Class<?> superclass = methodClass.getSuperclass();
    if (superclass != null)
    {
        final A annotation =
            getOverriddenAnnotationFrom(annotationClass, superclass, name, params);
        if (annotation != null)
            return annotation;
    }

    // depth-first search over interface hierarchy
    for (final Class<?> intf : methodClass.getInterfaces())
    {
        final A annotation =
            getOverriddenAnnotationFrom(annotationClass, intf, name, params);
        if (annotation != null)
            return annotation;
    }

    return null;
}

private static <A extends Annotation> A getOverriddenAnnotationFrom(
    Class<A> annotationClass, Class<?> searchClass, String name, Class<?>[] params)
{
    try
    {
        final Method method = searchClass.getMethod(name, params);
        final A annotation = method.getAnnotation(annotationClass);
        if (annotation != null)
            return annotation;
        return getOverriddenAnnotation(annotationClass, method);
    }
    catch (final NoSuchMethodException e)
    {
        return null;
    }
}
于 2013-06-24T17:08:49.483 回答