开发者

Access annotated fields

I made a custom Annotation for my project which will be used only with fields, that is

@MyAnnotation int myVariable

I have another class which will be in charge of performing some actions according to the variables values.The project has an undetermined number of classes with annotations included. How can I access them using my annotations proces开发者_如何学Pythonsor in order to access the values?

I can check the annotated variables going though each class, but not modifying the value since is not an object.

any suggestion on how to do it?

Thanks in advance!! :)


int getMyVariable(Foo foo) throws IllegalArgumentException, IllegalAccessException{
 for(Field f:foo.getClass().getDeclaredFields()){
  /**
   * Ensure the RetentionPolicy of 'MyAnnotation' is RUNTIME.
   */
   if(f.isAnnotationPresent(MyAnnotation.class)){
   return f.getInt(foo);
  } 
 }
 return -1;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜