2

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.

Like , to be more specific, When i write @ in Eclipse and hit CtlSpace. I get this list 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

junaidp
  • 10,801
  • 29
  • 89
  • 137
  • You have to scan *every* annotation in the classpath (so all .class files), and remember all the annotations that are applicable to a class. Eclipse has done that and cached the result for performance. You have to do it yourself for your purposes. – Andreas Dec 04 '15 at 03:59
  • any idea how we do that , "scan every annotation in the classpath " – junaidp Dec 04 '15 at 04:34

1 Answers1

0

All annotations implement the Annotation interface. Therefore, you can try something like this to find all annotations in the org.hibernate package (see 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. If you're using IntelliJ and gradle (which I suggest) you can include this in your gradle file:

compile 'org.reflections:reflections-maven:0.9.9-RC2'
compile 'com.google.guava:guava:18.0'
compile 'javassist:javassist:3.12.1.GA'
bcorso
  • 45,608
  • 10
  • 63
  • 75
  • thanks , will this work for all annotations available in hibernate for example. how will i achieve that with your example. what will be the package in that scenario. – junaidp Dec 04 '15 at 04:20
  • should work. not sure about the package, org.hibernate? – bcorso Dec 04 '15 at 04:27
  • yeah , org.hibernate was the package , i tried that and org.hibernate.annotations as well. but getting error : java.lang.NoClassDefFoundError: com/google/common/base/Predicate, – junaidp Dec 04 '15 at 04:34
  • I think that might be a separate issue. You might want to start a new SO question for that. – bcorso Dec 04 '15 at 05:32
  • Its resolved, some jar was missing , And your solution works for me . Thanks ! – junaidp Dec 04 '15 at 05:32