简体   繁体   English

我如何获取某个类的所有可用注释的列表

[英]How can i get a list of all available annotations of some class

Is there a way I can show user a list of all available annotations related to some class , for example all annotations available with in hibernate , or under javax.persistence. 有没有一种方法可以向用户显示与某个类相关的所有可用注释的列表,例如hibernate中或javax.persistence下可用的所有注释。

Like , to be more specific, When i write @ in Eclipse and hit CtlSpace. 就像,更具体地说,当我在Eclipse中编写@并点击CtlSpace时。 I get this list http://screencast.com/t/tLAVFdi46OiB 我得到此列表http://screencast.com/t/tLAVFdi46OiB

I want to show this whole list somewhere in my application's Interface. 我想在应用程序界面的某个位置显示整个列表。

there must be some where this list is coming from , From where i can fetch this list . 这个清单一定有一些来源,我可以从那里取得这个清单。

Any idea 任何想法

thanks 谢谢

All annotations implement the Annotation interface. 所有注释均实现Annotation接口。 Therefore, you can try something like this to find all annotations in the org.hibernate package (see Reflections javadocs ): 因此,您可以尝试这样的方法来查找org.hibernate包中的所有注释(请参阅Reflections javadocs ):

Reflections reflections = new Reflections("org.hibernate");
Set<Class<? extends Annotation>> annotations  = reflections.getSubTypesOf(Annotation.class);

You'll need the Reflections library, which relies on Guava and Javassist . 您将需要Reflections库,该库依赖于GuavaJavassist If you're using IntelliJ and gradle (which I suggest) you can include this in your gradle file: 如果您使用的是IntelliJ和gradle(我建议这样做),则可以在gradle文件中添加它:

compile 'org.reflections:reflections-maven:0.9.9-RC2'
compile 'com.google.guava:guava:18.0'
compile 'javassist:javassist:3.12.1.GA'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM