我使用自定义类型使用注释。我无法像任何其他常规注释一样从对象中读取它们:
public class TestingAnnotations {
public static void main(final String[] args) {
final @CustomAnnotation TypeAnnotated b = new @CustomAnnotation TypeAnnotated();
System.out.println(b.getClass().getAnnotation(CustomAnnotation.class)); //<-- prints null :(
}
}
@Target({ElementType.TYPE_USE, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@interface CustomAnnotation {
}
class TypeAnnotated {
}
那么,如何检查b实例是否已注释?
由于
答案 0 :(得分:0)
你实际上并没有注释这个类......一个类看起来像是注释的:
@CustomAnnotation
class TypeAnnotated {
}
之后你会得到注释:
TypeAnnotated b = new TypeAnnotated();
System.out.println(b.getClass().getAnnotation(CustomAnnotation.class));