我正在尝试使用反射和注解。但是每当我向字段(或类或方法)添加注解,并使用反射查看该字段时,我发现其annotations
字段为空。
例如,以下代码:
public class Test {
public static void main(String[] args) throws NoSuchFieldException, SecurityException {
System.out.println(Test.class.getField("bl").getAnnotations().length);
}
@anno
public int bl;
public @interface anno {}
}
打印0。
顺便说一下,Java通常不会忽略注释,例如当我使用@Deprecated
注释时 - Eclipse会识别它并告诉我该类已过时。
我正在使用Eclipse Indigo和Java SE Development Kit 7更新2版。谢谢!