Class Reflections
- All Implemented Interfaces:
NameHelper
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 will expandSuperTypes(Map, 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 are SubTypes
and TypesAnnotated
.
For all standard scanners use Scanners.values()
.
Classloader can optionally be used for resolving runtime classes from names.
Query usingget(QueryFunction)
, such as:
Set<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));
If not using asClass()
or as()
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
and ReflectionUtils
functions, and compose fluent functional methods from QueryFunction
.
Scanned metadata can be saved using save(String)
, and collected using collect(String, java.util.function.Predicate, org.reflections.serializers.Serializer)
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final Configuration
static final org.slf4j.Logger
protected final Store
Fields inherited from interface org.reflections.util.NameHelper
primitiveDescriptors, primitiveNames, primitiveTypes
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
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
Modifier and TypeMethodDescriptionstatic Reflections
collect()
collect saved Reflection xml resources and merge it into a Reflections instancecollect
(File file, Serializer serializer) deserialize and merge saved Reflections metadata from the givenfile
andserializer
collect
(InputStream inputStream, Serializer serializer) deserialize and merge saved Reflections metadata from the giveninputStream
andserializer
static Reflections
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
private boolean
private void
expandSupertypes
(Map<String, Set<String>> subTypesStore, Map<String, Set<String>> typesAnnotatedStore, String key, Class<?> type) void
expandSuperTypes
(Map<String, Set<String>> subTypesStore, Map<String, Set<String>> typesAnnotatedStore) expand super types after scanning, for super types that were not scanned.<T> Set
<T> get
(QueryFunction<Store, T> query) applyQueryFunction
onStore
returns all key and values scanned by the givenscanner
Deprecated.private javassist.bytecode.ClassFile
getClassFile
(Vfs.File file) returns theConfiguration
object of this instancegetConstructorsAnnotatedWith
(Annotation annotation) get constructors annotated with the givenannotation
, including annotation member values matchinggetConstructorsAnnotatedWith
(Class<? extends Annotation> annotation) get constructors annotated with the givenannotation
get constructors with any parameter matching the giventype
, either class or annotationgetConstructorsWithSignature
(Class<?>... types) get constructors with signature matching the giventypes
getFieldsAnnotatedWith
(Annotation annotation) get fields annotated with the givenannotation
, including annotation member values matchinggetFieldsAnnotatedWith
(Class<? extends Annotation> annotation) get fields annotated with the givenannotation
getMemberParameterNames
(Member member) get parameter names of the givenmember
, either method or constructorgetMemberUsage
(Member member) get code usages for the givenmember
, either field, method or constructorgetMethodsAnnotatedWith
(Annotation annotation) get methods annotated with the givenannotation
, including annotation member values matchinggetMethodsAnnotatedWith
(Class<? extends Annotation> annotation) get methods annotated with the givenannotation
getMethodsReturn
(Class<?> type) get methods with return type matching the givenreturnType
get methods with any parameter matching the giventype
, either class or annotationgetMethodsWithSignature
(Class<?>... types) get methods with signature matching the giventypes
getResources
(String pattern) get resources matching the givenpattern
regexgetResources
(Pattern pattern) get resources matching the givenpattern
regexgetStore()
returns theStore
object used for storing and querying the metadatagetSubTypesOf
(Class<T> type) gets all subtypes in hierarchy of a giventype
.getTypesAnnotatedWith
(Annotation annotation) get types annotated with the givenannotation
, both classes and annotations, including annotation member values matchinggetTypesAnnotatedWith
(Annotation annotation, boolean honorInherited) get types annotated with the givenannotation
, both classes and annotations, including annotation member values matchinggetTypesAnnotatedWith
(Class<? extends Annotation> annotation) get types annotated with the givenannotation
, both classes and annotationsgetTypesAnnotatedWith
(Class<? extends Annotation> annotation, boolean honorInherited) get types annotated with the givenannotation
, both classes and annotations(package private) ClassLoader[]
loaders()
merge
(Reflections reflections) merges the givenreflections
instance metadata into this instanceserialize metadata to the givenfilename
save
(String filename, Serializer serializer) serialize metadata to the givenfilename
andserializer
scan()
-
Field Details
-
log
public static final org.slf4j.Logger log -
configuration
-
store
-
-
Constructor Details
-
Reflections
constructs Reflections instance and scan according to the givenConfiguration
it is preferred to use
ConfigurationBuilder
new Reflections(new ConfigurationBuilder()...)
-
Reflections
-
Reflections
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
Convenient constructor for Reflections. see the javadoc ofConfigurationBuilder.build(Object...)
for details. it is preferred to useConfigurationBuilder
instead. -
Reflections
protected Reflections()
-
-
Method Details
-
scan
-
doFilter
-
getClassFile
-
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
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
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
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
merges the givenreflections
instance metadata into this instance -
expandSuperTypes
public void expandSuperTypes(Map<String, Set<String>> subTypesStore, Map<String, Set<String>> typesAnnotatedStore) expand super types after scanning, for super types that were not scanned.
this is helpful for 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
-
expandSupertypes
-
get
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
gets all subtypes in hierarchy of a giventype
.similar to
depends onget(SubTypes.of(type))
Scanners.SubTypes
configured -
getTypesAnnotatedWith
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
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
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
get methods annotated with the givenannotation
similar to
depends onget(MethodsAnnotated.with(annotation))
Scanners.MethodsAnnotated
configured -
getMethodsAnnotatedWith
get methods annotated with the givenannotation
, including annotation member values matchingsimilar to
depends onget(MethodsAnnotated.with(annotation))
Scanners.MethodsAnnotated
configured -
getMethodsWithSignature
get methods with signature matching the giventypes
similar to
depends onget(MethodsSignature.of(types))
Scanners.MethodsSignature
configured -
getMethodsWithParameter
get methods with any parameter matching the giventype
, either class or annotationsimilar to
depends onget(MethodsParameter.with(type))
Scanners.MethodsParameter
configured -
getMethodsReturn
get methods with return type matching the givenreturnType
similar to
depends onget(MethodsReturn.of(type))
Scanners.MethodsParameter
configured -
getConstructorsAnnotatedWith
get constructors annotated with the givenannotation
similar to
depends onget(ConstructorsAnnotated.with(annotation))
Scanners.ConstructorsAnnotated
configured -
getConstructorsAnnotatedWith
get constructors annotated with the givenannotation
, including annotation member values matchingsimilar to
depends onget(ConstructorsAnnotated.with(annotation))
Scanners.ConstructorsAnnotated
configured -
getConstructorsWithSignature
get constructors with signature matching the giventypes
similar to
depends onget(ConstructorsSignature.with(types))
Scanners.ConstructorsSignature
configured -
getConstructorsWithParameter
get constructors with any parameter matching the giventype
, either class or annotationsimilar to
depends onget(ConstructorsParameter.with(types))
Scanners.ConstructorsParameter
configured -
getFieldsAnnotatedWith
get fields annotated with the givenannotation
similar to
depends onget(FieldsAnnotated.with(annotation))
Scanners.FieldsAnnotated
configured -
getFieldsAnnotatedWith
get fields annotated with the givenannotation
, including annotation member values matchingsimilar to
depends onget(FieldsAnnotated.with(annotation))
Scanners.FieldsAnnotated
configured -
getResources
get resources matching the givenpattern
regexSet<String> xmls = reflections.getResources(".*\\.xml")
similar to
depends onget(Resources.with(pattern))
Scanners.Resources
configured -
getResources
get resources matching the givenpattern
regexSet<String> xmls = reflections.getResources(Pattern.compile(".*\\.xml"))
similar to
depends onget(Resources.with(pattern))
Scanners.Resources
configured -
getMemberParameterNames
get parameter names of the givenmember
, either method or constructordepends on
MethodParameterNamesScanner
configured -
getMemberUsage
get code usages for the givenmember
, either field, method or constructordepends on
MemberUsageScanner
configured -
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
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
returns theStore
object used for storing and querying the metadataStore
is basicallyMap<String, Map<String, Set<String>>>
-
getConfiguration
returns theConfiguration
object of this instance -
save
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
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 -
loaders
ClassLoader[] loaders()
-