我正在尝试在 Java 中使用类注释,但我有一个奇怪的行为,即返回getTypesAnnotatedWith
一个返回的类。getAnnotation
null
是否有某些原因getTypesAnnotatedWith
返回没有注释的类?
例如,在我的项目中,以下代码(正确)返回了我注释的类之一:
new Reflections("my.nice.package")
.getTypesAnnotatedWith(MyTag.class)
.iterator().next();
但是,如果我调用getAnnotation
它,结果是null
:
new Reflections("my.nice.package")
.getTypesAnnotatedWith(MyTag.class)
.iterator().next()
.getAnnotation(MyTag.class));
这是注释:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyTag {
public String value();
}
而且,为了完整起见,这就是我注释类的方式:
@MyTag("FooBar")
class MyClass {
// ...
}