|
Project Information
Links
|
version 0.9.7.RC1 available in the Maven Central repository - Repo1 and in the Downloads. see also UseCases, javadoc, JavaCodeSerializer, ReflectionUtils, ClasspathHelper, Licence A Java runtime metadata analysis, in the spirit of ScannotationsReflections 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:
How to use?add Reflections to your project. for maven projects just add this dependency: <dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.7.RC1</version>
</dependency>otherwise add the relevant jars to your projects, this might help a typical use of Reflections would be: Reflections reflections = new Reflections("my.project.prefix");
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 first instantiate it with one of the constructors: new Reflections(new ConfigurationBuilder()
.filterInputsBy(new FilterBuilder().include(FilterBuilder.prefix("my.project.prefix")))
.setUrls(ClasspathHelper.forPackage("my.project.prefix"))
.setScanners(new SubTypesScanner(),
new TypeAnnotationsScanner().filterResultsBy(filter),
new ResourcesScanner()));than use the convenient methods to query the metadata, such as:
You can use other scanners defined in Reflections as well, such as: SubTypesScanner, TypeAnnotationsScanner, MethodAnnotationsScanner, FieldAnnotationsScanner, ResourcrsScanner, or write a specialized scanner for your needs browse the javadoc for more info also, browse the tests directory to see some more examples ReflectionUtilsReflections also contains some convenient java reflection helper methods for getting types/methods/fields matching some predicates, generally in the form of getAllXXX(type/s, withYYY) for example, getting all getter methods of some classes: Set<Method> getters =
Reflections.getAllMethods(someClasses,
Predicates.and(withModifier(Modifier.PUBLIC), withPrefix("get"), withParametersCount(0));see more in the ReflectionUtils javadoc UseCasesReflections can also:
see the UseCases wiki page Reflections Maven pluginThere 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>the latest version...</version>
<executions>
<execution>
<goals>
<goal>reflections</goal>
</goals>
<phase>process-classes</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>than, on runtime: Reflections reflections =
isProduction() ? Reflections.collect() : new Reflections("your.package.here");Check out in the ReflectionsMojo wiki page Extending ReflectionsYou can easily extend Reflections by :
patches and extension are welcomed! Cheers |
