ClassIndex

ClassIndex is much quicker alternative to every run-time annotation scanning library like Reflections or Scannotations.

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:

<dependency>
    <groupid>org.atteo.classindex</groupid>
    <artifactid>classindex</artifactid>
    <version>3.1</version>
</dependency>

There are two annotations which trigger the indexing:

To access the index at run-time use static methods of ClassIndex class.

Example:

@IndexAnnotated
public @interface Entity {
}

@Entity
public class Car {
}

...

for (Class<?> klass : ClassIndex.getAnnotated(Entity.class)) {
    System.out.println(klass.getName());
}

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.

Links:


Go Back