vendredi 7 octobre 2016

Reflect Annotation of an Instance of my own Class

I have several Classes A1,A2,A3 which extend the abstract class myA. These classes have x fields of class B. The fields of class B are annotated with the annotation Test.(Test is available at runtime) How can i get the annotation Test and its value from inside a method of class B.

public class A1 extends myA{

    @Test("all")
    private B b1;

    @Test("none")
    private B b2;

    @Test("none")
    private B b3;

    //....

    public void interact(){
        b2.doSomethingBasedOnMyAnnotation();
    }

}
public class A2 extends myA{

    @Test("none")
    private B b;

    //....
}

public class B{

     public void doSomethingBasedOnMyAnnotation(){
        // How to reach the Annotation from here ?
     }

}

@Retention(value = RetentionPolicy.RUNTIME)
@Target(value = ElementType.FIELD)
public @interface Test{
    String value() default "all";
}





Aucun commentaire:

Enregistrer un commentaire