在使用Java reflections library时,我可以正确地找到用注释修饰的类:
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(CommandName.class);但是当我尝试查看每个类上有多少注释时,我总是看到零:
for(Class c : annotated)
{
int numAnnotations = c.getAnnotations().length;
}为什么c.getAnnotations()会返回长度为0的数组?实际上,在调试器中,c的所有字段(除了name)都是null。
发布于 2012-11-14 05:54:45
除非你用@Retention(RetentionPolicy.RUNTIME)标记注解,否则注解不会保留到运行时。这就解决了这个问题。奇怪的是,这个类被包含在集合中。我会认为要么全有要么什么都没有。
https://stackoverflow.com/questions/13368460
复制相似问题