Class Reflections
- java.lang.Object
-
- org.reflections.Reflections
-
- All Implemented Interfaces:
NameHelper
public class Reflections extends Object implements NameHelper
Reflections one-stop-shop objectReflections scans and indexes your project's classpath, allowing reverse query of the type system metadata on runtime.
Using Reflections you can query for example:
- Subtypes of a type
- Types annotated with an annotation
- Methods with annotation, parameters, return type
- Resources found in classpath
ConfigurationBuilder
:Reflections reflections = new Reflections( new ConfigurationBuilder() .forPackage("com.my.project")); // or similarly Reflections reflections = new Reflections("com.my.project");
Query usingScanners
must be configured in order to be queried, otherwise an empty result is returned. Default scanners areSubTypes
andTypesAnnotated
. For all standard scanners useScanners.values()
.get(QueryFunction)
, such as:
If not usingSet<Class<? extends Module>> modules = reflections.get(SubTypes.of(Module.class).asClass()); Set<Class<?>> singletons = reflections.get(TypesAnnotated.with(Singleton.class).asClass()); Set<String> properties = reflections.get(Resources.with(".*\\.properties")); Set<Method> requests = reflections.get(MethodsAnnotated.with(RequestMapping.class).as(Method.class)); Set<Method> voidMethods = reflections.get(MethodsReturn.with(void.class).as(Method.class)); Set<Method> someMethods = reflections.get(MethodsSignature.of(long.class, int.class).as(Method.class));
asClass()
oras()
query results are strings, such that:Set<String> modules = reflections.get(SubTypes.of(Module.class)); Set<String> singletons = reflections.get(TypesAnnotated.with(Singleton.class));
Note that previous 0.9.x API is still supported, for example:
Queries can combineSet<Class<? extends Module>> modules = reflections.getSubTypesOf(Module.class); Set<Class<?>> singletons = reflections.getTypesAnnotatedWith(Singleton.class);
Scanners
andReflectionUtils
functions, and compose fluent functional methods fromQueryFunction
. Scanned metadata can be saved usingsave(String)
, and collected usingcollect(String, java.util.function.Predicate, org.reflections.serializers.Serializer)
All relevant URLs should be configured.
If required, Reflections willexpandSuperTypes(Map)
in order to get the transitive closure metadata without scanning large 3rd party urls. Classloader can optionally be used for resolving runtime classes from names. For Javadoc, source code, and more information about Reflections Library, see http://github.com/ronmamo/reflections/
-
-
Field Summary
Fields Modifier and Type Field Description protected Configuration
configuration
static org.slf4j.Logger
log
protected Store
store
-
Fields inherited from interface org.reflections.util.NameHelper
primitiveDescriptors, primitiveNames, primitiveTypes
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
Reflections()
Reflections(Object... params)
Convenient constructor for Reflections.Reflections(String prefix, Scanner... scanners)
constructs Reflections instance and scan according to given packageprefix
and optionalscanners
Reflections(Configuration configuration)
constructs Reflections instance and scan according to givenConfiguration
Reflections(Store store)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Reflections
collect()
collect saved Reflection xml resources and merge it into a Reflections instanceReflections
collect(File file, Serializer serializer)
deserialize and merge saved Reflections metadata from the givenfile
using the givenserializer
Reflections
collect(InputStream inputStream, Serializer serializer)
deserialize and merge saved Reflections metadata from the given input stream, using the serializer configured in this instance's Configurationstatic Reflections
collect(String packagePrefix, Predicate<String> resourceNameFilter)
collect saved Reflections metadata from all urls that contains the givenpackagePrefix
and matches the givenresourceNameFilter
, and deserialize using the default serializerXmlSerializer
static Reflections
collect(String packagePrefix, Predicate<String> resourceNameFilter, Serializer serializer)
collect saved Reflections metadata from all urls that contains the givenpackagePrefix
and matches the givenresourceNameFilter
, and deserializes using the givenserializer
void
expandSuperTypes(Map<String,Set<String>> map)
expand super types after scanning, for super types that were not scanned.<T> Set<T>
get(QueryFunction<Store,T> query)
applyQueryFunction
onStore
Set<String>
getAll(Scanner scanner)
returns all key and values scanned byscanner
Set<String>
getAllTypes()
Deprecated.Configuration
getConfiguration()
returns theConfiguration
object of this instanceSet<Constructor>
getConstructorsAnnotatedWith(Annotation annotation)
get all constructors annotated with a given annotation, including annotation member values matchingSet<Constructor>
getConstructorsAnnotatedWith(Class<? extends Annotation> annotation)
get all constructors annotated with a given annotationSet<Constructor>
getConstructorsWithParameter(Annotation annotation)
get constructors with any parameter matchingannotation
Set<Constructor>
getConstructorsWithParameter(AnnotatedElement type)
get constructors with any parameter matchingtype
Set<Constructor>
getConstructorsWithParameterAnnotated(Annotation annotation)
get constructors with any parameter annotated with given annotation, including annotation member values matchingSet<Constructor>
getConstructorsWithParameterAnnotated(Class<? extends Annotation> annotation)
get constructors with any parameter annotated with given annotationSet<Constructor>
getConstructorsWithSignature(Class<?>... types)
get constructors with signature matching giventypes
Set<Field>
getFieldsAnnotatedWith(Annotation annotation)
get all methods annotated with a given annotation, including annotation member values matchingSet<Field>
getFieldsAnnotatedWith(Class<? extends Annotation> annotation)
get all fields annotated with a given annotationList<String>
getMemberParameterNames(Member member)
get parameter names of given method or constructorCollection<Member>
getMemberUsage(Member member)
get all code usages for the givenmember
field/method/constructorSet<Method>
getMethodsAnnotatedWith(Annotation annotation)
get all methods annotated with a given annotation, including annotation member values matchingSet<Method>
getMethodsAnnotatedWith(Class<? extends Annotation> annotation)
get all methods annotated with a given annotationSet<Method>
getMethodsReturn(Class<?> returnType)
get methods with return type match given typeSet<Method>
getMethodsWithParameter(Annotation annotation)
get methods with any parameter matchingtype
or annotated withannotation
Set<Method>
getMethodsWithParameter(AnnotatedElement type)
get methods with any parameter matchingtype
or annotated withannotation
Set<Method>
getMethodsWithSignature(Class<?>... types)
get methods with signature matching giventypes
Set<String>
getResources(String pattern)
get resources matching regular expressionSet<String>
getResources(Pattern pattern)
get resources matching regular expressionStore
getStore()
returns theStore
object used for storing and querying the metadata<T> Set<Class<? extends T>>
getSubTypesOf(Class<T> type)
gets all sub types in hierarchy of a given typeSet<Class<?>>
getTypesAnnotatedWith(Annotation annotation)
get types annotated with a given annotation, both classes and annotations, including annotation member values matchingSet<Class<?>>
getTypesAnnotatedWith(Annotation annotation, boolean honorInherited)
get types annotated with a given annotation, both classes and annotations, including annotation member values matchingSet<Class<?>>
getTypesAnnotatedWith(Class<? extends Annotation> annotation)
get types annotated with a given annotation, both classes and annotationsSet<Class<?>>
getTypesAnnotatedWith(Class<? extends Annotation> annotation, boolean honorInherited)
get types annotated with a given annotation, both classes and annotationsReflections
merge(Reflections reflections)
merges a Reflections instance metadata into this instanceFile
save(String filename)
serialize to a given directory and filenameFile
save(String filename, Serializer serializer)
serialize metadata to the givenfilename
andSerializer
protected Map<String,Map<String,Set<String>>>
scan()
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.reflections.util.NameHelper
forClass, forConstructor, forElement, forField, forMember, forMethod, forName, forNames, forNames, isConstructor, isMethod, toName, toName, toName, toName, toName, toNames, toNames
-
-
-
-
Field Detail
-
log
public static final org.slf4j.Logger log
-
configuration
protected final transient Configuration configuration
-
store
protected final Store store
-
-
Constructor Detail
-
Reflections
public Reflections(Configuration configuration)
constructs Reflections instance and scan according to givenConfiguration
it is preferred to use
ConfigurationBuilder
new Reflections(new ConfigurationBuilder()...)
-
Reflections
public Reflections(Store store)
-
Reflections
public Reflections(String prefix, Scanner... scanners)
constructs Reflections instance and scan according to given packageprefix
and optionalscanners
new Reflections("org.reflections")
it is preferred to use
ConfigurationBuilder
, this is actually similar to:new Reflections( new ConfigurationBuilder() .forPackage(prefix) .setScanners(scanners))
uses
ClasspathHelper.forPackage(String, ClassLoader...)
to resolve urls from givenprefix
optional
scanners
defaults toScanners.TypesAnnotated
andScanners.SubTypes
-
Reflections
public Reflections(Object... params)
Convenient constructor for Reflections. each parameter inparams
is referred by its type, see the javadoc ofConfigurationBuilder.build(Object...)
for details. it is preferred to useConfigurationBuilder
explicitly instead to avoid ambiguity.
-
Reflections
protected Reflections()
-
-
Method Detail
-
collect
public static Reflections collect()
collect saved Reflection xml resources and merge it into a Reflections instanceby default, resources are collected from all urls that contains the package META-INF/reflections and includes files matching the pattern .*-reflections.xml
-
collect
public static Reflections collect(String packagePrefix, Predicate<String> resourceNameFilter)
collect saved Reflections metadata from all urls that contains the givenpackagePrefix
and matches the givenresourceNameFilter
, and deserialize using the default serializerXmlSerializer
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 fasterReflections.collect("META-INF/reflections/", new FilterBuilder().includePattern(".*-reflections\\.xml")
-
collect
public static Reflections collect(String packagePrefix, Predicate<String> resourceNameFilter, Serializer serializer)
collect saved Reflections metadata from all urls that contains the givenpackagePrefix
and matches the givenresourceNameFilter
, and deserializes using the givenserializer
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 fasterReflections reflections = Reflections.collect( "META-INF/reflections/", new FilterBuilder().includePattern(".*-reflections\\.xml"), new XmlSerializer())
-
collect
public Reflections collect(InputStream inputStream, Serializer serializer)
deserialize and merge saved Reflections metadata from the given input stream, using the serializer configured in this instance's Configurationuseful if you know the serialized resource location and prefer not to look it up the classpath
-
collect
public Reflections collect(File file, Serializer serializer)
deserialize and merge saved Reflections metadata from the givenfile
using the givenserializer
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
-
expandSuperTypes
public void expandSuperTypes(Map<String,Set<String>> map)
expand super types after scanning, for super types that were not scanned. this is helpful in finding the transitive closure without scanning all 3rd party dependencies. it usesReflectionUtils.getSuperTypes(Class)
.for example, for classes A,B,C where A supertype of B, B supertype of C:
- if scanning C resulted in B (B->C in store), but A was not scanned (although A supertype of B) - then getSubTypes(A) will not return C
- if expanding supertypes, B will be expanded with A (A->B in store) - then getSubTypes(A) will return C
-
get
public <T> Set<T> get(QueryFunction<Store,T> query)
applyQueryFunction
onStore
Set<T> ts = get(query)
use
Scanners
andReflectionUtils
query functions, such as:Set<String> annotated = get(Scanners.TypesAnnotated.with(A.class)) Set<Class<?>> subtypes = get(Scanners.SubTypes.of(B.class).asClass()) Set<Method> methods = get(ReflectionUtils.Methods.of(B.class))
supports
QueryFunction
functional methods such asmap
,filter
,flatMap
:
-
getSubTypesOf
public <T> Set<Class<? extends T>> getSubTypesOf(Class<T> type)
gets all sub types in hierarchy of a given type depends on SubTypesScanner configured
-
getTypesAnnotatedWith
public Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation)
get types annotated with a given annotation, both classes and annotationsInherited
is not honored by default.when honoring @Inherited, meta-annotation should only effect annotated super classes and its sub types
Note that this (@Inherited) meta-annotation type has no effect if the annotated type is used for anything other then 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
-
getTypesAnnotatedWith
public Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation, boolean honorInherited)
get types annotated with a given annotation, both classes and annotationsInherited
is honored according to given honorInherited.when honoring @Inherited, meta-annotation should only effect annotated super classes and subtypes
when not honoring @Inherited, meta annotation effects all subtypes, including annotations interfaces and classes
Note that this (@Inherited) meta-annotation type has no effect if the annotated type is used for anything other then 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
-
getTypesAnnotatedWith
public Set<Class<?>> getTypesAnnotatedWith(Annotation annotation)
get types annotated with a given annotation, both classes and annotations, including annotation member values matching
depends on TypeAnnotationsScanner configuredInherited
is not honored by default
-
getTypesAnnotatedWith
public Set<Class<?>> getTypesAnnotatedWith(Annotation annotation, boolean honorInherited)
get types annotated with a given annotation, both classes and annotations, including annotation member values matching
depends on TypeAnnotationsScanner configuredInherited
is honored according to given honorInherited
-
getMethodsAnnotatedWith
public Set<Method> getMethodsAnnotatedWith(Class<? extends Annotation> annotation)
get all methods annotated with a given annotation depends on METHODS_ANNOTATED configured
-
getMethodsAnnotatedWith
public Set<Method> getMethodsAnnotatedWith(Annotation annotation)
get all methods annotated with a given annotation, including annotation member values matching depends on METHODS_ANNOTATED configured
-
getMethodsWithSignature
public Set<Method> getMethodsWithSignature(Class<?>... types)
get methods with signature matching giventypes
-
getMethodsWithParameter
public Set<Method> getMethodsWithParameter(AnnotatedElement type)
get methods with any parameter matchingtype
or annotated withannotation
-
getMethodsWithParameter
public Set<Method> getMethodsWithParameter(Annotation annotation)
get methods with any parameter matchingtype
or annotated withannotation
-
getMethodsReturn
public Set<Method> getMethodsReturn(Class<?> returnType)
get methods with return type match given type
-
getConstructorsAnnotatedWith
public Set<Constructor> getConstructorsAnnotatedWith(Class<? extends Annotation> annotation)
get all constructors annotated with a given annotation depends on METHODS_ANNOTATED configured
-
getConstructorsAnnotatedWith
public Set<Constructor> getConstructorsAnnotatedWith(Annotation annotation)
get all constructors annotated with a given annotation, including annotation member values matching depends on METHODS_ANNOTATED configured
-
getConstructorsWithSignature
public Set<Constructor> getConstructorsWithSignature(Class<?>... types)
get constructors with signature matching giventypes
-
getConstructorsWithParameter
public Set<Constructor> getConstructorsWithParameter(AnnotatedElement type)
get constructors with any parameter matchingtype
-
getConstructorsWithParameter
public Set<Constructor> getConstructorsWithParameter(Annotation annotation)
get constructors with any parameter matchingannotation
-
getConstructorsWithParameterAnnotated
public Set<Constructor> getConstructorsWithParameterAnnotated(Class<? extends Annotation> annotation)
get constructors with any parameter annotated with given annotation
-
getConstructorsWithParameterAnnotated
public Set<Constructor> getConstructorsWithParameterAnnotated(Annotation annotation)
get constructors with any parameter annotated with given annotation, including annotation member values matching
-
getFieldsAnnotatedWith
public Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation)
get all fields annotated with a given annotation depends on FieldAnnotationsScanner configured
-
getFieldsAnnotatedWith
public Set<Field> getFieldsAnnotatedWith(Annotation annotation)
get all methods annotated with a given annotation, including annotation member values matching depends on FieldAnnotationsScanner configured
-
getResources
public Set<String> getResources(String pattern)
get resources matching regular expressionSet<String> xmls = reflections.getResources(".*\\.xml")
depends on
Scanners.Resources
configured
-
getResources
public Set<String> getResources(Pattern pattern)
get resources matching regular expressionSet<String> xmls = reflections.getResources(Pattern.compile(".*\\.xml"))
depends on ResourcesScanner configured
-
getMemberParameterNames
public List<String> getMemberParameterNames(Member member)
get parameter names of given method or constructordepends on MethodParameterNamesScanner configured
-
getMemberUsage
public Collection<Member> getMemberUsage(Member member)
get all code usages for the givenmember
field/method/constructordepends on MemberUsageScanner configured
-
getAllTypes
@Deprecated public Set<String> getAllTypes()
Deprecated.returns all keys and values scanned byScanners.SubTypes
scannerusing this api is discouraged, it is better to get elements by specific criteria such as
deprecated, useSubTypes.of(Class)
orTypesAnnotated.with(Class)
getAll(Scanner)
instead
-
getAll
public Set<String> getAll(Scanner scanner)
returns all key and values scanned byscanner
Set<String> all = reflections.getAll(SubTypes)
using this is discouraged, it is better to get elements by specific criteria such as
SubTypes.of(Class)
orTypesAnnotated.with(Class)
-
getStore
public Store getStore()
returns theStore
object used for storing and querying the metadataStore
is basicallyMap<String, Map<String, Set<String>>>
-
getConfiguration
public Configuration getConfiguration()
returns theConfiguration
object of this instance
-
save
public File save(String filename)
serialize to a given directory and 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
-
save
public File save(String filename, Serializer serializer)
serialize metadata to the givenfilename
andSerializer
directory and filename using given 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
-
-