0

我有一个班级测试:

public class Test 
{
    @Anno1(Anno1Param = "bla1")
    @Anno2(Anno2Param = "bla1")
    public static final String TEST1 = "test1";

    @Anno1(Anno1Param = "bla2")
    @Anno2(Anno2Param = "bla2")
    @Anno3(Anno3Param = "bla2")
    public static final String TEST2 = "test2";

    @Anno3(Anno3Param = "bla3")
    public static final String TEST3 = "test3";

    @Anno1(Anno1Param = "bla4")
    public static final String TEST4 = "test4";
}

现在,给定字段值,我想检索与之关联的注释。我现在唯一能做到的方法是:

 for (Field field : Test.class.getFields()) {
     if(field.get(this).equals("test4")) {
         Annotation a = field.getAnnotation(Anno1.class);
         ......
     }
 }

java反射中有没有办法我可以做类似的事情:

getAnnotationByFieldValue("test4")

这样给定字段值“test4”,而不遍历测试类中的所有字段并直接检索注释引用?

4

0 回答 0