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 parameters 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 TargetHelper {
@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.second); // should be `second`
}
}
Or is it even possible to do it without TargetHelper
interface?
Let's say I have this TargetProcessor
called before SomeClassBuilder
which sole purpose is to fill-in the target : Target
annotated with @TargetAnnotation
and assign the field/attributes from @TargetAnnotaton
to Target
.
public class TargetProcessor {
public void parse() {
// look into `@TargetAnnotation`
// map `@TargetAnnotation` properties to `Target`
}
}