Java 反射。getTypesAnnotatedWith(…)找到类,但getAnnotations()。长度为0

Java 反射。getTypesAnnotatedWith(…)找到类,但getAnnotations()。长度为0,java,reflection,reflections,Java,Reflection,Reflections,使用Java时,我可以正确地找到用注释修饰的类: Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(CommandName.class); 为什么c.getAnnotations()返回长度为0的数组?事实上,在调试器中,c的所有字段(除了name)都是nullAck,除非用@Retention(RetentionPolicy.runtime)标记注释,否则注释不会保留到运行时。这把它修好了。但奇怪的

使用Java时,我可以正确地找到用注释修饰的类:

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

为什么
c.getAnnotations()
返回长度为0的数组?事实上,在调试器中,
c
的所有字段(除了
name
)都是
null
Ack,除非用@Retention(RetentionPolicy.runtime)标记注释,否则注释不会保留到运行时。这把它修好了。但奇怪的是,这个班竟然被包括在这一组中。我认为要么全是,要么全是。

关于你的最后一句话,我认为
实例中的大部分元数据都是惰性加载的。在这种假设下,不应该第一次调用getAnnotations()强制至少填充annotations数组吗?是的,这肯定会发生。我只是说调试器可能有误导性+我想回答你的问题。
for(Class c : annotated)
{
     int numAnnotations = c.getAnnotations().length;
}