0

I'm trying to make a method that returns fields that are annotated with a specific annotation.

I have one annotation (TestAnn) that I want to be a superclass for the other two (AutoSave and AutoLoad).

TestAnn:

public @interface TestAnn { boolean value() default true; } 

AutoSave and AutoLoad (their code are the same):

@TestAnn public @interface AutoSave { boolean value() default true; } 

Method to get fields:

private List<Field> getFields(Mode mode){ Class<? extends TestAnn> an; if (mode == Mode.SAVE){ an = AutoSave.class; } else if (mode == Mode.LOAD){ an = AutoLoad.class; } else{ throw new RuntimeException(); } return null; } 

So I tried to annotate AutoSave and AutoLoad with @TestAnn in hope that it is how inheritance works for annotations. But when I try to assign a specific annotation class to wildcard <? extends TestAnn>, the compiler says I can't do that.

So is there some sort of inheritance for annotations? If you provide code for my specific case it would be very helpful. Thanks!

7
  • What are you trying to do in getFields exactly? Commented Oct 19, 2022 at 20:46
  • @Sweeper Get all the fields with the appropriate (AutoLoad or AutoSave) annotation on them. Commented Oct 19, 2022 at 20:56
  • Annotations are not themselves polymorphic. They cannot extend other annotations, nor any other interface for that matter. The dupe targets are pretty old, but the situation has not changed. Commented Oct 19, 2022 at 20:58
  • @JohnBollinger So how do I get all the class fields with dynamically defined annotation? Commented Oct 19, 2022 at 21:01
  • What does that have to do with polymorphic annotations? Commented Oct 19, 2022 at 21:07

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.