創建我的註釋獲取與註釋,字段列表通過使用反射
public @interface MyAnnotation {
}
我把它放在場在我的測試對象
public class TestObject {
@MyAnnotation
final private Outlook outlook;
@MyAnnotation
final private Temperature temperature;
...
}
現在,我想所有的字段列表MyAnnotation
。
for(Field field : TestObject.class.getDeclaredFields())
{
if (field.isAnnotationPresent(MyAnnotation.class))
{
//do action
}
}
但好像我的塊做永遠不會執行的動作和字段沒有註釋如下面的代碼返回0
TestObject.class.getDeclaredField("outlook").getAnnotations().length;
是任何人都可以幫助我,告訴我,我在做什麼錯誤?
1)爲了更好地幫助越早,張貼[SSCCE](http://sscce.org/)。 2)請在句子開頭添加大寫字母。還要使用大寫字母I和專有名稱(如Java),以及縮寫和首字母縮略詞(如JEE或WAR)。這使人們更容易理解和幫助。 –
[How to get annotations of a member variable?](http://stackoverflow.com/questions/4453159/how-to-get-annotations-of-a-member-variable) – fglez