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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…