views:

37

answers:

1

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

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

When declaring a List, I only want to allow the inclusion of annotations but everything else would result in a compilation error:

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

If the above is even possible, is it then possible to restrict it to types of annotations? By that I mean only allow annotations that are grouped somehow. For example, if I have the following annotations:

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

And I only want to allow annotations that live in the water. Therefore adding Lion.class to the List would result in a compilation error due to his land loving ways.

+6  A: 

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

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

About the second part: I guess no, because annotation interfaces cannot extend each other.

doublep
Thanks, after some more playing I found the some solution to the first part of the problem. And yes, I didn't think the second part was possible either due to interfaces not allow sub-typing. Thanks for the response!
digiarnie