0

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.

Mansur
  • 1,661
  • 3
  • 17
  • 41
  • This has some useful ideas: https://stackoverflow.com/questions/22126851/how-to-extend-java-annotation – racraman Jul 29 '19 at 06:20
  • No, I know that, I kind of, need a "super type" for my annotations to group them together. The answer on the top has something like this `Spring allows you to group other Spring's annotation in your own custom annotations`, tho I couldn't find anything regarding to it. – Mansur Jul 29 '19 at 06:31

0 Answers0