dimanche 26 juillet 2020

injecting a variable using Java annotation

I have created an annotation and I need to inject a variable (like in SLF4j log variable) using JAVA. Below is my code so far ...

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public  @interface ClassLevelAnno {
    public String testVariable = "abc";
}

And I am trying to use this annotation in the following class

package test;

@ClassLevelAnno
public class UsingTestAnno {
// I need to use testVariable defined in ClassLevelAnno class directly like follow.
    public void testMethod() {
        System.out.println(testVariable);
    }
}

As in the above class, I need to print the variable directly. Why this class level annotation didn't inherit its fields.





Aucun commentaire:

Enregistrer un commentaire