ClassIndex is an annotation processor which at compile-time generates an index of classes implementing given interface, classes annotated by given annotation or placed in a common package.
Java 6 and above automatically discovers the processor when put on the classpath and executes it during compilation. Download classindex jar file (also requires Guava) or simply put the following dependency in Maven POM file:
There are two annotations which trigger the indexing:
@IndexSubclasses when placed on interface makes an index of all classes implementing the interface, when placed on class makes an index of its subclasses and finally when placed in package-info.java it creates an index of all classes in that package.
@IndexAnnotated when placed on an annotation makes an index of all classes marked with that annotation.
To access the index at run-time use static methods of ClassIndex class.
For subclasses of the given class the index file name and format is compatible with what ServiceLoader expects. Keep in mind that ServiceLoader also requires for the classes to have zero-argument default constructor.
For classes inside given package the index file is named “jaxb.index”, it is located inside the package folder and it’s format is compatible with what JAXBContext.newInstance(String) expects.