version 0.9.4: support @Inherited for meta-annotations, optional parallel scanning, upgraded 3rdp dependencies, using commons-vfs
A Java runtime metadata analysis, in the spirit of Scannotations
Reflections scans your classpath, indexes the metadata, allows you to query it on runtime and may save and collect that information for many modules within your project.
Using Reflections you can query your metadata such as:
- get all subtypes of some type
- get all types/methods/fields annotated with some annotation, w/o annotation parameters matching
How to use?
a typical use of Reflections would be:
Reflections reflections = new Reflections("my.package.prefix"); //replace my.package.prefix with your package prefix, of course
Set<Class<? extends SomeClassOrInterface>> subTypes = reflections.getSubTypesOf(SomeClassOrInterface.class);
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);
Set<Class<?>> annotated1 = reflections.getTypesAnnotatedWith(
new SomeAnnotation() {public String value() {return "1";}
public Class<? extends Annotation> annotationType() {return SomeAnnotation.class;}});basically, to use Reflections for scanning and querying, instantiate it with a Configuration, for example
new Reflections(
new AbstractConfiguration() {
{
setFilter(new FilterBuilder().include("your project's common package prefix here..."));
setUrls(ClasspathHelper.getUrlsForCurrentClasspath());
setScanners(new SubTypesScanner(),
new ClassAnnotationsScanner().filterBy(myClassAnnotationsFilter));
));
}
});than use the convenient methods to query the metadata, such as getSubTypesOf, getTypesAnnotatedWith, getMethodsAnnotatedWith
You can use other scanners defined in Reflections as well, such as: SubTypesScanner, ClassAnnotationsScanner, FieldAnnotationsScanner, MethodAnnotationsScanner, ConvertersScanner. Than use the equivalent query method in the Reflections object.
Check out the tests here - with more examples on it's functionality
another usage of Reflections is to collect pre saved metadata xml, using collect() and collect(prefix, Predicate).
in order to save a Reflections instance metadata use save() method
Reflections Maven plugin
There is the ReflectionsMojo Maven plugin available for your project. With simple configuration you can save all scanned metadata into xml files upon compiling. Later on, when your project is bootstrapping you can let Reflections collect all those resources and re-create that metadata for you, making it available at runtime without re-scanning the classpath - thus reducing the bootstrapping time.
Use this maven configuration in your pom file:
<build>
<plugins>
<plugin>
<groupId>org.reflections</groupId>
<artifactId>reflections-maven</artifactId>
<version>0.9.4 or whatever the version might be</version>
<executions>
<execution>
<goals>
<goal>reflections</goal>
</goals>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>Than use Reflections to collect() methods as mentioned before
By default, the Reflections Maven plugin saves the marshalled xml file of scanned metadata into ${project.build.outputDirectory}/META-INF/reflections/${project.artifactId}-reflections.xml
This way, you can dynamically collect all post compiled resources from your project's modules, without knowing of each other.
Check out in the wikis for the ReflectionsMojo documentation
Extending Reflections
You can easily extend Reflections by :
- create your specialized scan class, should extend AbstractScanner
- provide a query method within that scan class
- next, use that scanner instance to first configure Reflections and than to query
Cheers