如何获得用特定注释注释的对象(非类)的所有字段和属性,而又不遍历的所有字段或属性描述符?

我的目标是避免对每个甚至没有注释的字段或属性(例如getClass()或类的不是实例的字段或成员变量的任何字段)进行不必要的迭代。

还是迭代是唯一的方法?还有其他更好的方法吗?

最佳答案

您可以使用reflections package为您完成所有工作。项目说明:



例子:

 Reflections reflections = new Reflections("my.project.prefix");

 Set<Class<? extends SomeType>> subTypes =
           reflections.getSubTypesOf(SomeType.class);

 Set<Class<?>> annotated =
           reflections.getTypesAnnotatedWith(SomeAnnotation.class);

 Set<Class<?>> annotated1 =
           reflections.getTypesAnnotatedWith(new SomeAnnotation() {
                public String value() { return "1"; }
                public Class<? extends Annotation> annotationType() {
                    return SomeAnnotation.class;
                }
            });

关于java - 如何获得使用特定注释注释的对象的所有字段和属性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13875029/

10-11 22:28
查看更多