views:

49

answers:

3

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 processor 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!! :)

+1  A: 

I think you may find this article useful.

Paweł Dyda
+1  A: 
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;
}
卢声远 Shengyuan Lu
A: 

If I got it correctly, you want to access all classes that have this annotation. There is the scannotation project, which scans for annotations. It might be needed, though, to make a class-level annotation that will make the class eligible for detection.

Bozho