Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scan the classpath for classes with custom annotation [duplicate]

Tags:

java

guice

For example if I have annotation @MyOwnAnnotation and have these classes in my classpath, so that I could scan classpath possibly with some kind of filter (example. scan only packages starting with my.own.app.*) and get list of all classes with annotation @MyOwnAnnotation? I'm using guice as injection framework and I don't use Spring.

like image 345
newbie Avatar asked Sep 06 '11 08:09

newbie


1 Answers

Yes, check out the Scannotation library.

Also, see the following blog post that documents use of Scannotation.

Basic example:

URL[] urls = ClasspathUrlFinder.findClassPaths(); // scan java.class.path
AnnotationDB db = new AnnotationDB();
db.scanArchives(urls);
Set<String> entityClasses =
    db.getAnnotationIndex().get(MyOwnAnnotation.class.getName());

Your annotations will need to have 'runtime' retention so that they are available in the .class file at runtime.

like image 63
johnstok Avatar answered Oct 02 '22 17:10

johnstok