简体   繁体   English

如何获取类及其超类的注释列表

[英]How to get the list of annotations of a class and its superclass

I'm writing a method supposed to retrieve all annotations of a specific method declaring class and its superclasses.我正在编写一个方法,应该检索声明类及其超类的特定方法的所有注释。

By using the method getAnnotations() on the declaring class, the resulting table contains only the declaring class annotations and the superclass annotations are ignored.通过在声明类上使用getAnnotations()方法,结果表仅包含声明类注释,而忽略超类注释。 If I remove the annotations of the declaring class, then the superclass annotation are present.如果我删除声明类的注释,则存在超类注释。

What am I missing here?我在这里缺少什么?

The simplified method retrieving the annotations :检索注释的简化方法:

public void check(Method invokedMethod) {
    for (Annotation annotation : invokedMethod.getDeclaringClass().getAnnotations()) {
        // Do something ...
    }
}

(All annotations I'm trying the get have the @Inherited annotation) (我正在尝试获取的所有注释都有@Inherited注释)

In case you need to process several annotations of the same type, the standard approach is does not work, because annotations are stored in a Map with annotation types as the key.如果您需要处理多个相同类型的注释,标准方法是行不通的,因为注释存储在以注释类型为键的Map (See more here ). 在此处查看更多信息)。 Here is how I would work around this problem (just go through all super classes manually):这是我将如何解决这个问题(只需手动浏览所有超类):

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;

public class AnnotationReflectionTest {
    public static void main(String[] args) throws Exception {
        check(Class2.class.getMethod("num", new Class[0]));
    }

    public static void check(Method invokedMethod) {
        Class<?> type = invokedMethod.getDeclaringClass();
        while (type != null) {
            for (Annotation annotation : type.getDeclaredAnnotations()) {
                System.out.println(annotation.toString());
            }
            type = type.getSuperclass();
        }
    }
}

@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Annot1 {
    int num();
}

@Annot1(num = 5)
class Class1 {
    public int num() {
        return 1;
    }
}

@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Annot2 {
    String text();
}

@Annot2(text = "ttt")
class Class2 extends Class1 {
    public int num() {
        return super.num() + 1;
    }
}

What version of Java and what OS do you use?您使用什么版本的 Java 和什么操作系统?

I had to write a simple method with我不得不写一个简单的方法

private <A extends Annotation> A getAnnotationFromType(Class<?> classType, final Class<A> annotationClass) {
    
    while ( !classType.getName().equals(Object.class.getName()) ) {
        
        if ( classType.isAnnotationPresent(annotationClass)) {
            return classType.getAnnotation(annotationClass);
        }
        classType = classType.getSuperclass();
    }
    return null;
    
}
    

This might be obvious to most people, but if you came here looking for fields of a class and its superclasses , you can use这对大多数人来说可能是显而易见的,但是如果您来这里寻找类及其超类的字段,则可以使用

myClass.getFields()

to get all fields, also of superclasses, instead of获取所有字段,也包括超类,而不是

myClass.getDeclaredFields()

which only returns the fields of the class itself.它只返回类本身的字段。 Similarly for methods and constructors.方法和构造函数也是如此。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM