How to build a list of classes annotated with my custom annotation?
I want to get a complete list 开发者_Python百科of classes in the application which are annotated with @Custom
annotation. What is the best mechanism for this operation?
ps. For example, how JAX-RS implementations find all classes that are annotated with @Path
? I would like to use the same mechanism.
Usually this is done using the process called classpath scanning. In general class loaders do not allow for scanning through all the classes on the classpath. But usually the only used class loader is UrlClassLoader
from which we can retrieve the list of directories and jar files (see getURLs) and open them one by one to list available classes.
This approach is implemented by libraries like Scannotation and Reflections.
Another approach is to use Java Pluggable Annotation Processing API to write annotation processor which will collect all annotated classes at compile time and build the index file for runtime use.
The above mechanism is implemented in ClassIndex library.
Using classpath scanning is usually two orders of magnitude slower than compile-time indexing. See this benchmark.
I know this is an old question, but I ran across it in my own search for classpath scanning and found another good answer, so I'm adding it here.
Google Guava has a ClassPath object that provides "best effort" classpath scanning (which is all any classpath scanning utility offers, really). Since Guava is a widely-adopted, carefully-maintained utility library, this is a great option for projects that either (a) are already using Guava, or (b) need a stable library they can rely on for classpath scanning.
You should take a look at Scannotation.
classindex is a compile-time annotation scanning library, implemented using an annotation processor.
You could try my library FastClasspathScanner:
List<String> classNames = new FastClassPathScanner("com.mypackage")
.scan()
.getNamesOfClassesWithAnnotation(Custom.class);
As you probably know by now, Java has no way to enumerate all packages or the classes in each package. So you have to do it the "hard" way. Options:
Use a tool like
grep
to search for the annotation. Advantage: Fast, simple. Drawbacks: Might return false positives (like classes where the annotation is commented out).Compile the code and process the result with
javap
. That works on the bytecode level[*]. It gives you accurate results but the output ofjavap
isn't really meant for automatic processing.Use a bytecode library like ASM. Not for the faint of heart but it allows you to access other data as well (like implemented interfaces, fields, etc) in relation to the annotation.
Last option: Eclipse uses its own Java compiler which can build an AST from Java code (even if the code doesn't compile). Advantage over ASM: You get a model for Java code for free. Makes certain operations more simple. No so easy to set up, though. See my blog for an example.
[*]: The annotation must have Retention.RUNTIME
for this to work.
Run a find in your IDE on the source, If you trying to do this on compiled classes you would have add a method to them to support this, even if you decompiled the classes I do not think annotations or comments would show up.
The easiest way would be to use an IDE as Jesus suggested.
But you could also
- write an annotation processor that logs all occurrences of the annotation
- write an AspectJ aspect and declare a warning for this annotation (I know, warning is probably not what you want, but there is no INFO in AspectJ)
- use ASM to check the byte code for the annotation
- use a Source Parser to check the source code for the annotation
Beware: All of these are tricky. The AspectJ solution should be the simplest.
精彩评论