|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.jboss.errai.reflections.ReflectionUtils
org.jboss.errai.reflections.Reflections
public class Reflections
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.forClassLoader()) .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<? extends java.lang.annotation.Annotation>)
, getMethodsAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation>)
, getFieldsAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation>)
, getResources(com.google.common.base.Predicate)
getStore()
to access and query the store directly
in order to save a metadata use save(String)
or save(String,
org.jboss.errai.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)
* be aware that when using the constructor new Reflections("my.package"), only urls with prefix 'my.package' will be scanned,
and any transitive classes in other urls will not be scanned (for example if my.package.SomeClass extends other.package.OtherClass,
than the later will not be scanned). in that case specify the urls manually using ConfigurationBuilder
For Javadoc, source code, and more information about Reflections Library, see http://code.google.com/p/reflections/
Field Summary | |
---|---|
protected Configuration |
configuration
|
Fields inherited from class org.reflections.ReflectionUtils |
---|
primitiveDescriptors, primitiveNames, primitiveTypes |
Constructor Summary | |
---|---|
protected |
Reflections()
|
|
Reflections(Configuration configuration)
constructs a Reflections instance and scan according to given Configuration |
|
Reflections(java.lang.Object[] urlHints,
Scanner... scanners)
a convenient constructor for scanning within given package prefixes and urls containing given classes. |
|
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 |
|
|
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 |
---|
areAnnotationMembersMatching, areAnnotationMembersMatching, forName, forNames, getAllSuperTypes, getAllSuperTypesAnnotatedWith, getMatchingAnnotations |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected final transient Configuration configuration
Constructor Detail |
---|
public Reflections(Configuration configuration)
Configuration
it is preferred to use ConfigurationBuilder
public Reflections(java.lang.String prefix, Scanner... scanners)
this actually create a Configuration
with:
- urls that contain resources with name prefix
- acceptsInput where name starts with the given prefix
- scanners set to the given scanners
, otherwise defaults to TypeAnnotationsScanner
and SubTypesScanner
.
- scanner results filter is set to accept results matching given prefix
public Reflections(java.lang.Object[] urlHints, Scanner... scanners)
given urlHints is an array of either String or Class elements, where Strings results in scanning package prefix and Class results in scanning url that contains that class, for example
new Reflections(new Object[] {"my.package", com.google.inject.Module, "javax.persistence"})would result in scanning packages 'my.package' and 'javax.persistence' and also the url that contains the class of com.google.inject.Module
this actually create a Configuration
with:
- urls that contain resources with name prefix
or that contains given classes
- acceptsInput where name starts with the given prefix
or with the classes package name
- scanners set to the given scanners
, otherwise defaults to TypeAnnotationsScanner
and SubTypesScanner
.
- scanner results filter is set to accept results matching given prefix
or the given classes package name
urlHints
- is an array of either String or Class elements, where Strings results in scanning package prefix and Class results in scanning urls containing the classprotected Reflections()
Method Detail |
---|
protected void scan()
public static Reflections collect()
by default, resources are collected from all urls that contains the package META-INF/reflections and includes files matching the pattern .*-reflections.xml
public Reflections collect(java.lang.String packagePrefix, com.google.common.base.Predicate<java.lang.String> resourceNameFilter)
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
public Reflections collect(java.lang.String packagePrefix, com.google.common.base.Predicate<java.lang.String> resourceNameFilter, Serializer serializer)
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
public Reflections collect(java.io.InputStream inputStream)
public Reflections collect(java.io.File file)
public Reflections merge(Reflections reflections)
public <T> java.util.Set<java.lang.Class<? extends T>> getSubTypesOf(java.lang.Class<T> type)
public java.util.Set<java.lang.Class<?>> getTypesAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
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
public java.util.Set<java.lang.Class<?>> getTypesAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotation, boolean honorInherited)
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
public java.util.Set<java.lang.Class<?>> getTypesAnnotatedWith(java.lang.annotation.Annotation annotation)
Inherited
is honored
public java.util.Set<java.lang.Class<?>> getTypesAnnotatedWith(java.lang.annotation.Annotation annotation, boolean honorInherited)
Inherited
is honored according to given honorInherited
public java.util.Set<java.lang.reflect.Method> getMethodsAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
public java.util.Set<java.lang.reflect.Method> getMethodsAnnotatedWith(java.lang.annotation.Annotation annotation)
public java.util.Set<java.lang.reflect.Field> getFieldsAnnotatedWith(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
public java.util.Set<java.lang.reflect.Field> getFieldsAnnotatedWith(java.lang.annotation.Annotation annotation)
public java.util.Set<java.lang.reflect.Method> getConverters(java.lang.Class<?> from, java.lang.Class<?> to)
depends on ConvertersScanner configured, otherwise an empty set is returned
from
- - the type to convert fromto
- - the required return typepublic java.util.Set<java.lang.String> getResources(com.google.common.base.Predicate<java.lang.String> namePredicate)
depends on ResourcesScanner configured, otherwise an empty set is returned
public java.util.Set<java.lang.String> getResources(java.util.regex.Pattern pattern)
depends on ResourcesScanner configured, otherwise an empty set is returned
Setxmls = reflections.getResources(".\*\.xml");
public Store getStore()
public java.io.File save(java.lang.String filename)
* it is preferred 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
public java.io.File save(java.lang.String filename, Serializer serializer)
* it is preferred to specify a designated directory (for example META-INF/reflections), so that it could be found later much faster using the load method
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |