创建我的注释获取与注释,字段列表通过使用反射
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