I have 2 annotations (@Anno1
, @Anno2
) which have the same methods. To processing them, I need to create 2 methods with different signatures (one accepting Anno1.class
and the other Anno2.class
) which do exactly the same things. However, since annotations in Java cannot implement or extend another interface, annotation, class, I cannot put a "super-class" as a method parameter.
Let's say annotation - Anno1, and Anno2 are something like this.
@Retention(...)
@Target(...)
public @interface Anno1 {
String value();
int code();
}
@Retention(...)
@Target(...)
public @interface Anno2 {
String value();
int code();
}
Now the method I wrote looks like this.
public void doHandleA1 (Anno1 anno1) {
// body is identical to doHandleA2, but does it with anno1
}
public void doHandleA2 (Anno1 anno1) {
// body is identical to doHandleA1, but does it with anno2
}
Also, I know, I can use instanceof
to determine the actual annotation, but I don't like that solution, it is not flexible. Because, I said 2 as an example, there may be 10.
I know Reflection is another option but I am looking for a solution which doesn't involve reflection.