dimanche 23 juillet 2017

how to read runtime annotations on a field within a class

Imagine an annotation called "MyAnn" with runtime retention, a class MyClass, and an abstract class called MyData. MyClass has a field of type MyData, annotated with MyAnn. Within the instance of MyData, I need to see if the annotation MyAnn is present and retrieve its information?

Note - in Java8 I know we can directly annotate the inner class at construction - and that works - but I need this working based on the field annotation.

Thanks for any help!

public MyClass extends MySuperClass() {
   @MyAnn(value = "something")
   protected MyData mydata;

   public void doSomething() {
      mydata = new MyData() {};
      mydata.initialize();
   }
}

public abstract MyData() {
   String myValue = null;

   public void initialize() {
      if (***I am annotated with MyAnn) {
         myValue = (***value from annotation MyAnn);         
      }
   }
}

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnn {
    String myvalue;
}





Aucun commentaire:

Enregistrer un commentaire