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;
}
精彩评论