org.reflections
Class Reflections

java.lang.Object
  extended by org.reflections.ReflectionUtils
      extended by org.reflections.Reflections

public class Reflections
extends ReflectionUtils

Reflections one-stop-shop object

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:

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 SomeType>> subTypes = reflections.getSubTypesOf(SomeType.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 ConfigurationBuilder()
              .filterInputsBy(new FilterBuilder().include("your project's common package prefix here..."))
              .setUrls(ClasspathHelper.getUrlsForCurrentClasspath())
              .setScanners(new SubTypesScanner(), new TypeAnnotationsScanner().filterResultsBy(myClassAnnotationsFilter)));
 
and than use the convenient methods to query the metadata, such as getSubTypesOf(java.lang.Class), getTypesAnnotatedWith(java.lang.Class), getMethodsAnnotatedWith(java.lang.Class), getFieldsAnnotatedWith(java.lang.Class), getResources(com.google.common.base.Predicate)
use getStore() to access and query the store directly

in order to save a metadata use save(String) or save(String, org.reflections.serializers.Serializer) for example with XmlSerializer or JavaCodeSerializer

in order to collect pre saved metadata and avoid re-scanning, use collect(String, com.google.common.base.Predicate)

For Javadoc, source code, and more information about Reflections Library, see http://code.google.com/p/reflections/


Field Summary
protected  Configuration configuration
           
protected  Store store
           
 
Constructor Summary
protected Reflections()
           
  Reflections(Configuration configuration)
          constructs a Reflections instance and scan according to given Configuration
  Reflections(java.lang.String prefix, Scanner... scanners)
          a convenient constructor for scanning within a package prefix
 
Method Summary
static Reflections collect()
          collect saved Reflection xml resources and merge it into a Reflections instance
 Reflections collect(java.io.File file)
          merges saved Reflections resources from the given file, using the serializer configured in this instance's Configuration useful if you know the serialized resource location and prefer not to look it up the classpath
 Reflections collect(java.io.InputStream inputStream)
          merges saved Reflections resources from the given input stream, using the serializer configured in this instance's Configuration useful if you know the serialized resource location and prefer not to look it up the classpath
 Reflections collect(java.lang.String packagePrefix, com.google.common.base.Predicate<java.lang.String> resourceNameFilter)
          collect saved Reflections resources from all urls that contains the given packagePrefix and matches the given resourceNameFilter and de-serializes them using the serializer configured in the configuration
 Reflections collect(java.lang.String packagePrefix, com.google.common.base.Predicate<java.lang.String> resourceNameFilter, Serializer serializer)
          collect saved Reflections resources from all urls that contains the given packagePrefix and matches the given resourceNameFilter and de-serializes them using the serializer configured in the configuration
 java.util.Set<java.lang.reflect.Method> getConverters(java.lang.Class<?> from, java.lang.Class<?> to)
          get 'converter' methods that could effectively convert from type 'from' to type 'to'
 java.util.Set<java.lang.reflect.Field> getFieldsAnnotatedWith(java.lang.annotation.Annotation annotation)
          get all methods annotated with a given annotation, including annotation member values matching

depends on FieldAnnotationsScanner configured, otherwise an empty set is returned

 java.util.Set<java.lang.reflect.Field> getFieldsAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
          get all fields annotated with a given annotation

depends on FieldAnnotationsScanner configured, otherwise an empty set is returned

 java.util.Set<java.lang.reflect.Method> getMethodsAnnotatedWith(java.lang.annotation.Annotation annotation)
          get all methods annotated with a given annotation, including annotation member values matching

depends on MethodAnnotationsScanner configured, otherwise an empty set is returned

 java.util.Set<java.lang.reflect.Method> getMethodsAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
          get all methods annotated with a given annotation

depends on MethodAnnotationsScanner configured, otherwise an empty set is returned

 java.util.Set<java.lang.String> getResources(java.util.regex.Pattern pattern)
          get resources relative paths where simple name (key) matches given regular expression
 java.util.Set<java.lang.String> getResources(com.google.common.base.Predicate<java.lang.String> namePredicate)
          get resources relative paths where simple name (key) matches given namePredicate
 Store getStore()
          returns the store used for storing and querying the metadata
<T> java.util.Set<java.lang.Class<? extends T>>
getSubTypesOf(java.lang.Class<T> type)
          gets all sub types in hierarchy of a given type

depends on SubTypesScanner configured, otherwise an empty set is returned

 java.util.Set<java.lang.Class<?>> getTypesAnnotatedWith(java.lang.annotation.Annotation annotation)
          get types annotated with a given annotation, both classes and annotations, including annotation member values matching
 java.util.Set<java.lang.Class<?>> getTypesAnnotatedWith(java.lang.annotation.Annotation annotation, boolean honorInherited)
          get types annotated with a given annotation, both classes and annotations, including annotation member values matching
 java.util.Set<java.lang.Class<?>> getTypesAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
          get types annotated with a given annotation, both classes and annotations
 java.util.Set<java.lang.Class<?>> getTypesAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, boolean honorInherited)
          get types annotated with a given annotation, both classes and annotations
 Reflections merge(Reflections reflections)
          merges a Reflections instance metadata into this instance
 java.io.File save(java.lang.String filename)
          serialize to a given directory and filename
 java.io.File save(java.lang.String filename, Serializer serializer)
          serialize to a given directory and filename using given serializer
protected  void scan()
           
 
Methods inherited from class org.reflections.ReflectionUtils
areAnnotationMembersMatcing, getAllSuperTypes, getAllSuperTypesAnnotatedWith, getMatchingAnnotations
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

configuration

protected transient Configuration configuration

store

protected Store store
Constructor Detail

Reflections

public Reflections(Configuration configuration)
constructs a Reflections instance and scan according to given Configuration

it is prefered to use ConfigurationBuilder


Reflections

public Reflections(java.lang.String prefix,
                   Scanner... scanners)
a convenient constructor for scanning within a package prefix

if no scanners supplied, TypeAnnotationsScanner and SubTypesScanner are used by default


Reflections

protected Reflections()
Method Detail

scan

protected void scan()

collect

public static Reflections collect()
collect saved Reflection xml resources and merge it into a Reflections instance

by default, resources are collected from all urls that contains the package META-INF/reflections and includes files matching the pattern .*-reflections.xml


collect

public Reflections collect(java.lang.String packagePrefix,
                           com.google.common.base.Predicate<java.lang.String> resourceNameFilter)
collect saved Reflections resources from all urls that contains the given packagePrefix and matches the given resourceNameFilter and de-serializes them using the serializer configured in the configuration

it is preferred to use a designated resource prefix (for example META-INF/reflections but not just META-INF), so that relevant urls could be found much faster


collect

public Reflections collect(java.lang.String packagePrefix,
                           com.google.common.base.Predicate<java.lang.String> resourceNameFilter,
                           Serializer serializer)
collect saved Reflections resources from all urls that contains the given packagePrefix and matches the given resourceNameFilter and de-serializes them using the serializer configured in the configuration

it is preferred to use a designated resource prefix (for example META-INF/reflections but not just META-INF), so that relevant urls could be found much faster


collect

public Reflections collect(java.io.InputStream inputStream)
merges saved Reflections resources from the given input stream, using the serializer configured in this instance's Configuration useful if you know the serialized resource location and prefer not to look it up the classpath


collect

public Reflections collect(java.io.File file)
merges saved Reflections resources from the given file, using the serializer configured in this instance's Configuration useful if you know the serialized resource location and prefer not to look it up the classpath


merge

public Reflections merge(Reflections reflections)
merges a Reflections instance metadata into this instance


getSubTypesOf

public <T> java.util.Set<java.lang.Class<? extends T>> getSubTypesOf(java.lang.Class<T> type)
gets all sub types in hierarchy of a given type

depends on SubTypesScanner configured, otherwise an empty set is returned


getTypesAnnotatedWith

public java.util.Set<java.lang.Class<?>> getTypesAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
get types annotated with a given annotation, both classes and annotations

Inherited is honored

Note that this (@Inherited) meta-annotation type has no effect if the annotated type is used for anything other than a class. Also, this meta-annotation causes annotations to be inherited only from superclasses; annotations on implemented interfaces have no effect.

depends on TypeAnnotationsScanner and SubTypesScanner configured, otherwise an empty set is returned


getTypesAnnotatedWith

public java.util.Set<java.lang.Class<?>> getTypesAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotation,
                                                               boolean honorInherited)
get types annotated with a given annotation, both classes and annotations

Inherited is honored according to given honorInherited

Note that this (@Inherited) meta-annotation type has no effect if the annotated type is used for anything other than a class. Also, this meta-annotation causes annotations to be inherited only from superclasses; annotations on implemented interfaces have no effect.

depends on TypeAnnotationsScanner and SubTypesScanner configured, otherwise an empty set is returned


getTypesAnnotatedWith

public java.util.Set<java.lang.Class<?>> getTypesAnnotatedWith(java.lang.annotation.Annotation annotation)
get types annotated with a given annotation, both classes and annotations, including annotation member values matching

Inherited is honored

depends on TypeAnnotationsScanner configured, otherwise an empty set is returned


getTypesAnnotatedWith

public java.util.Set<java.lang.Class<?>> getTypesAnnotatedWith(java.lang.annotation.Annotation annotation,
                                                               boolean honorInherited)
get types annotated with a given annotation, both classes and annotations, including annotation member values matching

Inherited is honored according to given honorInherited

depends on TypeAnnotationsScanner configured, otherwise an empty set is returned


getMethodsAnnotatedWith

public java.util.Set<java.lang.reflect.Method> getMethodsAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
get all methods annotated with a given annotation

depends on MethodAnnotationsScanner configured, otherwise an empty set is returned


getMethodsAnnotatedWith

public java.util.Set<java.lang.reflect.Method> getMethodsAnnotatedWith(java.lang.annotation.Annotation annotation)
get all methods annotated with a given annotation, including annotation member values matching

depends on MethodAnnotationsScanner configured, otherwise an empty set is returned


getFieldsAnnotatedWith

public java.util.Set<java.lang.reflect.Field> getFieldsAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
get all fields annotated with a given annotation

depends on FieldAnnotationsScanner configured, otherwise an empty set is returned


getFieldsAnnotatedWith

public java.util.Set<java.lang.reflect.Field> getFieldsAnnotatedWith(java.lang.annotation.Annotation annotation)
get all methods annotated with a given annotation, including annotation member values matching

depends on FieldAnnotationsScanner configured, otherwise an empty set is returned


getConverters

public java.util.Set<java.lang.reflect.Method> getConverters(java.lang.Class<?> from,
                                                             java.lang.Class<?> to)
get 'converter' methods that could effectively convert from type 'from' to type 'to'

depends on ConvertersScanner configured, otherwise an empty set is returned

Parameters:
from - - the type to convert from
to - - the required return type

getResources

public java.util.Set<java.lang.String> getResources(com.google.common.base.Predicate<java.lang.String> namePredicate)
get resources relative paths where simple name (key) matches given namePredicate


getResources

public java.util.Set<java.lang.String> getResources(java.util.regex.Pattern pattern)
get resources relative paths where simple name (key) matches given regular expression
Set xmls = reflections.getResources(".\*\.xml");


getStore

public Store getStore()
returns the store used for storing and querying the metadata


save

public java.io.File save(java.lang.String filename)
serialize to a given directory and filename

* it is prefered to specify a designated directory (for example META-INF/reflections), so that it could be found later much faster using the load method

see the documentation for the save method on the configured Serializer


save

public java.io.File save(java.lang.String filename,
                         Serializer serializer)
serialize to a given directory and filename using given serializer

* it is prefered to specify a designated directory (for example META-INF/reflections), so that it could be found later much faster using the load method



Copyright © 2010. All Rights Reserved.