Allow annotations only to list

Is it possible to restrict only annotations in a List? So, let's say I have two annotations:

(1) Foo.class
(2) Bar.class

When declaring a list, I want to enable the inclusion of annotations, but everything else will lead to a compilation error:

List<Something> list = new ArrayList<Something>();
list.add(Foo.class);
list.add(Bar.class);
list.add(String.class); // bad

If possible, is it possible to limit it to the types of annotations? By this, I mean only allowing annotations that are somehow grouped. For example, if I have the following annotations:

(1) Shark.class
(2) GoldFish.class
(3) Lion.class

And I just want to allow annotations that live in water. Therefore, adding Lion.class to the List will result in a compilation error due to its earthly love paths.

+3
source share
1 answer

I'm not sure I understood your question correctly, but I think you need this:

List <Class <? extends Annotation>>  x
    = new ArrayList <Class <? extends Annotation>> ();

: , , .

+6

Source: https://habr.com/ru/post/1752732/


All Articles