2
我試圖找到用自定義註釋標註的所有字段,但它似乎沒有檢測到它。相同的代碼對標準註釋工作正常,比如@Deprecated。Java:獲取私有成員的註釋
最少的代碼來重現:
public class MyClass {
public @interface MyAnnotation {}
@MyAnnotation Object someObject;
@MyAnnotation @Deprecated Object someDeprecatedObject;
@Deprecated Object aThirdObject;
public static void main(String[] args) {
Class<?> cls = MyClass.class;
for (Field field : cls.getDeclaredFields()) {
System.out.print(field.getName());
for (Annotation a : field.getDeclaredAnnotations())
System.out.print(" " + a);
System.out.println();
}
}
}
輸出:
someObject
someDeprecatedObject @java.lang.Deprecated()
aThirdObject @java.lang.Deprecated()
@Deprecated來了,但@MyAnnotation不!幫幫我!
完美。將在9分鐘內接受。謝謝! – slezica 2012-02-22 19:35:17