lundi 16 octobre 2017

Updating field attributes by annotation

I'm reviewing how reflection works or possible work. I have this SomeClassBuilder wherein it has an attribute target : Target with declared annotation TargetAnnotation.

Thing is, is it possible to override/update the values/properties of Target wherein upon invoke of someMethod() would return the paramters on the annotation?

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TargetAnnotation {
    String first();
    String second();
    // other attributes
}

public class Target {
    String first;
    String second;
    // some other attributes unique only to `Target`
}

public interface TargetHelper {
    void setTarget(Target target)''
}

public class SomeClassBuilder implements TaretHelper {

    @TargetAnnotation(first = "first", second = "second")
    private Target target;

    @Override public void setTarget(Target target) { this.target = target }

    public void someMethod() {
        System.out.println(target.first); // should be `first`
        System.out.println(target.first); // should be `second`
    }

}





Aucun commentaire:

Enregistrer un commentaire