Class Reflections
- java.lang.Object
-
- org.reflections.Reflections
-
- All Implemented Interfaces:
NameHelper
public class Reflections extends Object implements NameHelper
Reflections one-stop-shop object Reflections 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
And more...
Create Reflections instance, preferably using
ConfigurationBuilder
:Reflections reflections = new Reflections( new ConfigurationBuilder() .forPackage("com.my.project")); // or similarly Reflections reflections = new Reflections("com.my.project"); // another example Reflections reflections = new Reflections( new ConfigurationBuilder() .forPackage("com.my.project") .setScanners(Scanners.values()) // all standard scanners .filterInputsBy(new FilterBuilder().includePackage("com.my.project").excludePackage("com.my.project.exclude")));
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.Scanners
must be configured in order to be queried, otherwise an empty result is returned.
Default scanners areSubTypes
andTypesAnnotated
. For all standard scanners useScanners.values()
.Classloader can optionally be used for resolving runtime classes from names.
Query usingget(QueryFunction)
, such as:
If not usingSet<Class<?>> 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:
Set<Class<? extends Module>> modules = reflections.getSubTypesOf(Module.class); Set<Class<?>> singletons = reflections.getTypesAnnotatedWith(Singleton.class);
Queries can combine
Scanners
andReflectionUtils
functions, and compose fluent functional methods fromQueryFunction
.Scanned metadata can be saved using
For Javadoc, source code, and more information about Reflections Library, see http://github.com/ronmamo/reflections/save(String)
, and collected usingcollect(String, java.util.function.Predicate, org.reflections.serializers.Serializer)
-
-
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 the given packageprefix
and optionalscanners
Reflections(Configuration configuration)
constructs Reflections instance and scan according to the 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
andserializer
Reflections
collect(InputStream inputStream, Serializer serializer)
deserialize and merge saved Reflections metadata from the giveninputStream
andserializer
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
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 by the givenscanner
Set<String>
getAllTypes()
Deprecated.Configuration
getConfiguration()
returns theConfiguration
object of this instanceSet<Constructor>
getConstructorsAnnotatedWith(Annotation annotation)
get constructors annotated with the givenannotation
, including annotation member values matchingSet<Constructor>
getConstructorsAnnotatedWith(Class<? extends Annotation> annotation)
get constructors annotated with the givenannotation
Set<Constructor>
getConstructorsWithParameter(AnnotatedElement type)
get constructors with any parameter matching the giventype
, either class or annotationSet<Constructor>
getConstructorsWithSignature(Class<?>... types)
get constructors with signature matching the giventypes
Set<Field>
getFieldsAnnotatedWith(Annotation annotation)
get fields annotated with the givenannotation
, including annotation member values matchingSet<Field>
getFieldsAnnotatedWith(Class<? extends Annotation> annotation)
get fields annotated with the givenannotation
List<String>
getMemberParameterNames(Member member)
get parameter names of the givenmember
, either method or constructorCollection<Member>
getMemberUsage(Member member)
get code usages for the givenmember
, either field, method or constructorSet<Method>
getMethodsAnnotatedWith(Annotation annotation)
get methods annotated with the givenannotation
, including annotation member values matchingSet<Method>
getMethodsAnnotatedWith(Class<? extends Annotation> annotation)
get methods annotated with the givenannotation
Set<Method>
getMethodsReturn(Class<?> type)
get methods with return type matching the givenreturnType
Set<Method>
getMethodsWithParameter(AnnotatedElement type)
get methods with any parameter matching the giventype
, either class or annotationSet<Method>
getMethodsWithSignature(Class<?>... types)
get methods with signature matching the giventypes
Set<String>
getResources(String pattern)
get resources matching the givenpattern
regexSet<String>
getResources(Pattern pattern)
get resources matching the givenpattern
regexStore
getStore()
returns theStore
object used for storing and querying the metadata<T> Set<Class<? extends T>>
getSubTypesOf(Class<T> type)
gets all subtypes in hierarchy of a giventype
.Set<Class<?>>
getTypesAnnotatedWith(Annotation annotation)
get types annotated with the givenannotation
, both classes and annotations, including annotation member values matchingSet<Class<?>>
getTypesAnnotatedWith(Annotation annotation, boolean honorInherited)
get types annotated with the givenannotation
, both classes and annotations, including annotation member values matchingSet<Class<?>>
getTypesAnnotatedWith(Class<? extends Annotation> annotation)
get types annotated with the givenannotation
, both classes and annotationsSet<Class<?>>
getTypesAnnotatedWith(Class<? extends Annotation> annotation, boolean honorInherited)
get types annotated with the givenannotation
, both classes and annotationsReflections
merge(Reflections reflections)
merges the givenreflections
instance metadata into this instanceFile
save(String filename)
serialize metadata to the givenfilename
File
save(String filename, Serializer serializer)
serialize metadata to the givenfilename
andserializer
protected Map<String,Map<String,Set<String>>>
scan()
-
-
-
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 the 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 the given packageprefix
and optionalscanners
new Reflections("org.reflections")
it is preferred to use
ConfigurationBuilder
instead, 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. see the javadoc ofConfigurationBuilder.build(Object...)
for details. it is preferred to useConfigurationBuilder
instead.
-
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
prefer using a designated directory (for example META-INF/reflections but not just META-INF), so that collect can work 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
prefer using a designated directory (for example META-INF/reflections but not just META-INF), so that collect can work 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 giveninputStream
andserializer
useful 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
andserializer
useful if you know the serialized resource location and prefer not to look it up the classpath
-
merge
public Reflections merge(Reflections reflections)
merges the givenreflections
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. for example, for classes A,B,C where A supertype of B, B supertype of C (A -> B -> C):- if scanning C resulted in B (B->C in store), but A was not scanned (although A is 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))
-
getSubTypesOf
public <T> Set<Class<? extends T>> getSubTypesOf(Class<T> type)
gets all subtypes in hierarchy of a giventype
.similar to
depends onget(SubTypes.of(type))
Scanners.SubTypes
configured
-
getTypesAnnotatedWith
public Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation)
get types annotated with the givenannotation
, both classes and annotationsInherited
is not honored by default, seegetTypesAnnotatedWith(Class, boolean)
.similar to
depends onget(SubTypes.of(TypesAnnotated.with(annotation)))
Scanners.TypesAnnotated
andScanners.SubTypes
configured
-
getTypesAnnotatedWith
public Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation, boolean honorInherited)
get types annotated with the givenannotation
, both classes and annotationsInherited
is honored according to the givenhonorInherited
.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 onScanners.TypesAnnotated
andScanners.SubTypes
configured
-
getTypesAnnotatedWith
public Set<Class<?>> getTypesAnnotatedWith(Annotation annotation)
get types annotated with the givenannotation
, both classes and annotations, including annotation member values matching
depends onInherited
is not honored by default, seegetTypesAnnotatedWith(Annotation, boolean)
.Scanners.TypesAnnotated
andScanners.SubTypes
configured
-
getTypesAnnotatedWith
public Set<Class<?>> getTypesAnnotatedWith(Annotation annotation, boolean honorInherited)
get types annotated with the givenannotation
, both classes and annotations, including annotation member values matching
depends onInherited
is honored according to given honorInheritedScanners.TypesAnnotated
andScanners.SubTypes
configured
-
getMethodsAnnotatedWith
public Set<Method> getMethodsAnnotatedWith(Class<? extends Annotation> annotation)
get methods annotated with the givenannotation
similar to
depends onget(MethodsAnnotated.with(annotation))
Scanners.MethodsAnnotated
configured
-
getMethodsAnnotatedWith
public Set<Method> getMethodsAnnotatedWith(Annotation annotation)
get methods annotated with the givenannotation
, including annotation member values matchingsimilar to
depends onget(MethodsAnnotated.with(annotation))
Scanners.MethodsAnnotated
configured
-
getMethodsWithSignature
public Set<Method> getMethodsWithSignature(Class<?>... types)
get methods with signature matching the giventypes
similar to
depends onget(MethodsSignature.of(types))
Scanners.MethodsSignature
configured
-
getMethodsWithParameter
public Set<Method> getMethodsWithParameter(AnnotatedElement type)
get methods with any parameter matching the giventype
, either class or annotationsimilar to
depends onget(MethodsParameter.with(type))
Scanners.MethodsParameter
configured
-
getMethodsReturn
public Set<Method> getMethodsReturn(Class<?> type)
get methods with return type matching the givenreturnType
similar to
depends onget(MethodsReturn.of(type))
Scanners.MethodsParameter
configured
-
getConstructorsAnnotatedWith
public Set<Constructor> getConstructorsAnnotatedWith(Class<? extends Annotation> annotation)
get constructors annotated with the givenannotation
similar to
depends onget(ConstructorsAnnotated.with(annotation))
Scanners.ConstructorsAnnotated
configured
-
getConstructorsAnnotatedWith
public Set<Constructor> getConstructorsAnnotatedWith(Annotation annotation)
get constructors annotated with the givenannotation
, including annotation member values matchingsimilar to
depends onget(ConstructorsAnnotated.with(annotation))
Scanners.ConstructorsAnnotated
configured
-
getConstructorsWithSignature
public Set<Constructor> getConstructorsWithSignature(Class<?>... types)
get constructors with signature matching the giventypes
similar to
depends onget(ConstructorsSignature.with(types))
Scanners.ConstructorsSignature
configured
-
getConstructorsWithParameter
public Set<Constructor> getConstructorsWithParameter(AnnotatedElement type)
get constructors with any parameter matching the giventype
, either class or annotationsimilar to
depends onget(ConstructorsParameter.with(types))
Scanners.ConstructorsParameter
configured
-
getFieldsAnnotatedWith
public Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation)
get fields annotated with the givenannotation
similar to
depends onget(FieldsAnnotated.with(annotation))
Scanners.FieldsAnnotated
configured
-
getFieldsAnnotatedWith
public Set<Field> getFieldsAnnotatedWith(Annotation annotation)
get fields annotated with the givenannotation
, including annotation member values matchingsimilar to
depends onget(FieldsAnnotated.with(annotation))
Scanners.FieldsAnnotated
configured
-
getResources
public Set<String> getResources(String pattern)
get resources matching the givenpattern
regexSet<String> xmls = reflections.getResources(".*\\.xml")
similar to
depends onget(Resources.with(pattern))
Scanners.Resources
configured
-
getResources
public Set<String> getResources(Pattern pattern)
get resources matching the givenpattern
regexSet<String> xmls = reflections.getResources(Pattern.compile(".*\\.xml"))
similar to
depends onget(Resources.with(pattern))
Scanners.Resources
configured
-
getMemberParameterNames
public List<String> getMemberParameterNames(Member member)
get parameter names of the givenmember
, either method or constructordepends on
MethodParameterNamesScanner
configured
-
getMemberUsage
public Collection<Member> getMemberUsage(Member member)
get code usages for the givenmember
, either field, method or 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 by the givenscanner
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 metadata to the givenfilename
prefer using a designated directory (for example META-INF/reflections but not just META-INF), so thatcollect(String, Predicate)
can work much faster
-
save
public File save(String filename, Serializer serializer)
serialize metadata to the givenfilename
andserializer
prefer using a designated directory (for example META-INF/reflections but not just META-INF), so thatcollect(String, Predicate, Serializer)
can work much faster
-
-