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.Scannersmust be configured in order to be queried, otherwise an empty result is returned.
Default scanners areSubTypesandTypesAnnotated. 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
ScannersandReflectionUtilsfunctions, 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 Configurationconfigurationstatic org.slf4j.Loggerlogprotected Storestore-
Fields inherited from interface org.reflections.util.NameHelper
primitiveDescriptors, primitiveNames, primitiveTypes
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedReflections()Reflections(Object... params)Convenient constructor for Reflections.Reflections(String prefix, Scanner... scanners)constructs Reflections instance and scan according to the given packageprefixand optionalscannersReflections(Configuration configuration)constructs Reflections instance and scan according to the givenConfigurationReflections(Store store)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static Reflectionscollect()collect saved Reflection xml resources and merge it into a Reflections instanceReflectionscollect(File file, Serializer serializer)deserialize and merge saved Reflections metadata from the givenfileandserializerReflectionscollect(InputStream inputStream, Serializer serializer)deserialize and merge saved Reflections metadata from the giveninputStreamandserializerstatic Reflectionscollect(String packagePrefix, Predicate<String> resourceNameFilter)collect saved Reflections metadata from all urls that contains the givenpackagePrefixand matches the givenresourceNameFilter, and deserialize using the default serializerXmlSerializerstatic Reflectionscollect(String packagePrefix, Predicate<String> resourceNameFilter, Serializer serializer)collect saved Reflections metadata from all urls that contains the givenpackagePrefixand matches the givenresourceNameFilter, and deserializes using the givenserializervoidexpandSuperTypes(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)applyQueryFunctiononStoreSet<String>getAll(Scanner scanner)returns all key and values scanned by the givenscannerSet<String>getAllTypes()Deprecated.ConfigurationgetConfiguration()returns theConfigurationobject 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 givenannotationSet<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 giventypesSet<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 givenannotationList<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 givenannotationSet<Method>getMethodsReturn(Class<?> type)get methods with return type matching the givenreturnTypeSet<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 giventypesSet<String>getResources(String pattern)get resources matching the givenpatternregexSet<String>getResources(Pattern pattern)get resources matching the givenpatternregexStoregetStore()returns theStoreobject 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 annotationsReflectionsmerge(Reflections reflections)merges the givenreflectionsinstance metadata into this instanceFilesave(String filename)serialize metadata to the givenfilenameFilesave(String filename, Serializer serializer)serialize metadata to the givenfilenameandserializerprotected 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 givenConfigurationit is preferred to use
ConfigurationBuildernew Reflections(new ConfigurationBuilder()...)
-
Reflections
public Reflections(Store store)
-
Reflections
public Reflections(String prefix, Scanner... scanners)
constructs Reflections instance and scan according to the given packageprefixand optionalscannersnew Reflections("org.reflections")it is preferred to use
ConfigurationBuilderinstead, this is actually similar to:new Reflections( new ConfigurationBuilder() .forPackage(prefix) .setScanners(scanners))uses
ClasspathHelper.forPackage(String, ClassLoader...)to resolve urls from givenprefixoptional
scannersdefaults toScanners.TypesAnnotatedandScanners.SubTypes
-
Reflections
public Reflections(Object... params)
Convenient constructor for Reflections. see the javadoc ofConfigurationBuilder.build(Object...)for details. it is preferred to useConfigurationBuilderinstead.
-
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 givenpackagePrefixand 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 givenpackagePrefixand 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 giveninputStreamandserializeruseful 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 givenfileandserializeruseful if you know the serialized resource location and prefer not to look it up the classpath
-
merge
public Reflections merge(Reflections reflections)
merges the givenreflectionsinstance 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)
applyQueryFunctiononStoreSet<T> ts = get(query)use
ScannersandReflectionUtilsquery 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.SubTypesconfigured
-
getTypesAnnotatedWith
public Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation)
get types annotated with the givenannotation, both classes and annotationsInheritedis not honored by default, seegetTypesAnnotatedWith(Class, boolean).similar to
depends onget(SubTypes.of(TypesAnnotated.with(annotation)))Scanners.TypesAnnotatedandScanners.SubTypesconfigured
-
getTypesAnnotatedWith
public Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation, boolean honorInherited)
get types annotated with the givenannotation, both classes and annotationsInheritedis 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.TypesAnnotatedandScanners.SubTypesconfigured
-
getTypesAnnotatedWith
public Set<Class<?>> getTypesAnnotatedWith(Annotation annotation)
get types annotated with the givenannotation, both classes and annotations, including annotation member values matching
depends onInheritedis not honored by default, seegetTypesAnnotatedWith(Annotation, boolean).Scanners.TypesAnnotatedandScanners.SubTypesconfigured
-
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 onInheritedis honored according to given honorInheritedScanners.TypesAnnotatedandScanners.SubTypesconfigured
-
getMethodsAnnotatedWith
public Set<Method> getMethodsAnnotatedWith(Class<? extends Annotation> annotation)
get methods annotated with the givenannotationsimilar to
depends onget(MethodsAnnotated.with(annotation))Scanners.MethodsAnnotatedconfigured
-
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.MethodsAnnotatedconfigured
-
getMethodsWithSignature
public Set<Method> getMethodsWithSignature(Class<?>... types)
get methods with signature matching the giventypessimilar to
depends onget(MethodsSignature.of(types))Scanners.MethodsSignatureconfigured
-
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.MethodsParameterconfigured
-
getMethodsReturn
public Set<Method> getMethodsReturn(Class<?> type)
get methods with return type matching the givenreturnTypesimilar to
depends onget(MethodsReturn.of(type))Scanners.MethodsParameterconfigured
-
getConstructorsAnnotatedWith
public Set<Constructor> getConstructorsAnnotatedWith(Class<? extends Annotation> annotation)
get constructors annotated with the givenannotationsimilar to
depends onget(ConstructorsAnnotated.with(annotation))Scanners.ConstructorsAnnotatedconfigured
-
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.ConstructorsAnnotatedconfigured
-
getConstructorsWithSignature
public Set<Constructor> getConstructorsWithSignature(Class<?>... types)
get constructors with signature matching the giventypessimilar to
depends onget(ConstructorsSignature.with(types))Scanners.ConstructorsSignatureconfigured
-
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.ConstructorsParameterconfigured
-
getFieldsAnnotatedWith
public Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation)
get fields annotated with the givenannotationsimilar to
depends onget(FieldsAnnotated.with(annotation))Scanners.FieldsAnnotatedconfigured
-
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.FieldsAnnotatedconfigured
-
getResources
public Set<String> getResources(String pattern)
get resources matching the givenpatternregexSet<String> xmls = reflections.getResources(".*\\.xml")similar to
depends onget(Resources.with(pattern))Scanners.Resourcesconfigured
-
getResources
public Set<String> getResources(Pattern pattern)
get resources matching the givenpatternregexSet<String> xmls = reflections.getResources(Pattern.compile(".*\\.xml"))similar to
depends onget(Resources.with(pattern))Scanners.Resourcesconfigured
-
getMemberParameterNames
public List<String> getMemberParameterNames(Member member)
get parameter names of the givenmember, either method or constructordepends on
MethodParameterNamesScannerconfigured
-
getMemberUsage
public Collection<Member> getMemberUsage(Member member)
get code usages for the givenmember, either field, method or constructordepends on
MemberUsageScannerconfigured
-
getAllTypes
@Deprecated public Set<String> getAllTypes()
Deprecated.returns all keys and values scanned byScanners.SubTypesscannerusing 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 givenscannerSet<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 theStoreobject used for storing and querying the metadataStoreis basicallyMap<String, Map<String, Set<String>>>
-
getConfiguration
public Configuration getConfiguration()
returns theConfigurationobject of this instance
-
save
public File save(String filename)
serialize metadata to the givenfilenameprefer 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 givenfilenameandserializerprefer using a designated directory (for example META-INF/reflections but not just META-INF), so thatcollect(String, Predicate, Serializer)can work much faster
-
-