Annotations: restrict reference to classes with a annotation

与世无争的帅哥 提交于 2019-12-11 03:56:08

问题


I think this is not possible, but maybe I'm wrong. So I ask you, if it is possible. ;-)

If I define a annotation that accept only class references which extends some interface or class that is possible:

 Class<? extends ServiceProviderIF> serviceIFProvider();

At this annotation I only can add some class which extends ServiceProviderIF. My question: is such a definition also possible for another annotation? That means with pseudocode something like this

 Class<? contains AnnotationXYZ> classReferences();

AnnotationXYZ is another annotation definition. And the class which should be added is only accepted by containing this Annotation.

TIA and best regards, Oli


回答1:


It's impossible. Annotation can be set on any type (class, interface), but Java method can't return a multi-type containing types from different hierarchies. In other words, you should place all your classes that have annotation AnnotationXYZ in the same hierarchy under some interface or class Blabla, then use type <? extends Blabla> wildcard.




回答2:


No, it's not possible. Only types are accepted in generic types. And annotations don't define types.



来源:https://stackoverflow.com/questions/11011783/annotations-restrict-reference-to-classes-with-a-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!