• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Finding annotated classes

 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, just getting started with annotations. Yeah, yeah, always late to the party...

Seriously, is there a way to find all classes that have been annotated with a specific annotation? For example, is there a way to find which classes have been marked with @Bear or some such?
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.javalobby.org/java/forums/t17381
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is very similar to the oft-asked question about finding all the classes that implement MyPluginInterface. The simple answer is no, there's no simple answer. The more complex answer is that if you control class loading, then you can know about all the loaded/loadable classes, and individually check them for the annotation you're interested in. You can do this via reflection (which will load every class you look at, perhaps not what you want) or via one of the libraries like BCEL that parse class files and "reflect" them without actually creating a Class object.

So use, for example, a URLClassLoader. Then you'll know exactly what the class path is, and you can use the java.util.jar apis to find classes in jars, and the File api to find classes not in jars, and then inspect them.

I would not be at all surprised if there exists a third party library that does all this for you automagically.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose it depends whether you want to do this at compile time or runtime, and whether it's enought to do this while using your own development tools, or you need to more general platform-independent solution. Using IntelliJ IDEA, for example, you can just right-click on an annotation and search for uages in the project. I assume something similar is possible in Eclipse as well. You may also be interested in the jdk's annotation processing tool. Or you can use reflection. With all these, you need to somehow specify the input source files or class files to be scanned (though in the case of your IDE, you've already done that). I'm not sure which of these approaches might be closest to what you're looking for.
 
reply
    Bookmark Topic Watch Topic
  • New Topic