简体   繁体   中英

How to build a list of classes annotated with my custom annotation?

I want to get a complete list 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.

classindex是一个编译时注释扫描库,使用注释处理器实现。

You could try my library FastClasspathScanner :

List<String> classNames = new FastClassPathScanner("com.mypackage")
    .scan()
    .getNamesOfClassesWithAnnotation(Custom.class);

你应该看看Scannotation

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:

  1. 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).

  2. Compile the code and process the result with javap . That works on the bytecode level[*]. It gives you accurate results but the output of javap isn't really meant for automatic processing.

  3. 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.

  4. 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.

在源代码的 IDE 中运行查找,如果您尝试在已编译的类上执行此操作,您将向它们添加一个方法来支持这一点,即使您反编译了这些类,我认为注释或注释不会出现。

The easiest way would be to use an IDE as Jesus suggested .

But you could also

Beware: All of these are tricky. The AspectJ solution should be the simplest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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