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 Configurationstatic final org.slf4j.Loggerprotected final StoreFields inherited from interface NameHelper
primitiveDescriptors, primitiveNames, primitiveTypes -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedReflections(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
Modifier and TypeMethodDescriptionstatic Reflectionscollect()collect saved Reflection xml resources and merge it into a Reflections instancecollect(File file, Serializer serializer) deserialize and merge saved Reflections metadata from the givenfileandserializercollect(InputStream inputStream, Serializer serializer) deserialize and merge saved Reflections metadata from the giveninputStreamandserializerstatic Reflectionscollect 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 givenserializerprivate booleanprivate voidexpandSupertypes(Map<String, Set<String>> subTypesStore, Map<String, Set<String>> typesAnnotatedStore, String key, Class<?> type) voidexpandSuperTypes(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) applyQueryFunctiononStorereturns all key and values scanned by the givenscannerDeprecated.private javassist.bytecode.ClassFilegetClassFile(Vfs.File file) returns theConfigurationobject 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 givenannotationget constructors with any parameter matching the giventype, either class or annotationgetConstructorsWithSignature(Class<?>... types) get constructors with signature matching the giventypesgetFieldsAnnotatedWith(Annotation annotation) get fields annotated with the givenannotation, including annotation member values matchinggetFieldsAnnotatedWith(Class<? extends Annotation> annotation) get fields annotated with the givenannotationgetMemberParameterNames(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 givenannotationgetMethodsReturn(Class<?> type) get methods with return type matching the givenreturnTypeget methods with any parameter matching the giventype, either class or annotationgetMethodsWithSignature(Class<?>... types) get methods with signature matching the giventypesgetResources(String pattern) get resources matching the givenpatternregexgetResources(Pattern pattern) get resources matching the givenpatternregexgetStore()returns theStoreobject 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 givenreflectionsinstance metadata into this instanceserialize metadata to the givenfilenamesave(String filename, Serializer serializer) serialize metadata to the givenfilenameandserializerscan()
-
Field Details
-
log
public static final org.slf4j.Logger log -
configuration
-
store
-
-
Constructor Details
-
Reflections
constructs Reflections instance and scan according to the givenConfigurationit is preferred to use
ConfigurationBuildernew Reflections(new ConfigurationBuilder()...) -
Reflections
-
Reflections
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
Convenient constructor for Reflections. see the javadoc ofConfigurationBuilder.build(Object...)for details. it is preferred to useConfigurationBuilderinstead. -
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 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
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
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
merges the givenreflectionsinstance 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
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
gets all subtypes in hierarchy of a giventype.similar to
depends onget(SubTypes.of(type))Scanners.SubTypesconfigured -
getTypesAnnotatedWith
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
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
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
get methods annotated with the givenannotationsimilar to
depends onget(MethodsAnnotated.with(annotation))Scanners.MethodsAnnotatedconfigured -
getMethodsAnnotatedWith
get methods annotated with the givenannotation, including annotation member values matchingsimilar to
depends onget(MethodsAnnotated.with(annotation))Scanners.MethodsAnnotatedconfigured -
getMethodsWithSignature
get methods with signature matching the giventypessimilar to
depends onget(MethodsSignature.of(types))Scanners.MethodsSignatureconfigured -
getMethodsWithParameter
get methods with any parameter matching the giventype, either class or annotationsimilar to
depends onget(MethodsParameter.with(type))Scanners.MethodsParameterconfigured -
getMethodsReturn
get methods with return type matching the givenreturnTypesimilar to
depends onget(MethodsReturn.of(type))Scanners.MethodsParameterconfigured -
getConstructorsAnnotatedWith
get constructors annotated with the givenannotationsimilar to
depends onget(ConstructorsAnnotated.with(annotation))Scanners.ConstructorsAnnotatedconfigured -
getConstructorsAnnotatedWith
get constructors annotated with the givenannotation, including annotation member values matchingsimilar to
depends onget(ConstructorsAnnotated.with(annotation))Scanners.ConstructorsAnnotatedconfigured -
getConstructorsWithSignature
get constructors with signature matching the giventypessimilar to
depends onget(ConstructorsSignature.with(types))Scanners.ConstructorsSignatureconfigured -
getConstructorsWithParameter
get constructors with any parameter matching the giventype, either class or annotationsimilar to
depends onget(ConstructorsParameter.with(types))Scanners.ConstructorsParameterconfigured -
getFieldsAnnotatedWith
get fields annotated with the givenannotationsimilar to
depends onget(FieldsAnnotated.with(annotation))Scanners.FieldsAnnotatedconfigured -
getFieldsAnnotatedWith
get fields annotated with the givenannotation, including annotation member values matchingsimilar to
depends onget(FieldsAnnotated.with(annotation))Scanners.FieldsAnnotatedconfigured -
getResources
get resources matching the givenpatternregexSet<String> xmls = reflections.getResources(".*\\.xml")similar to
depends onget(Resources.with(pattern))Scanners.Resourcesconfigured -
getResources
get resources matching the givenpatternregexSet<String> xmls = reflections.getResources(Pattern.compile(".*\\.xml"))similar to
depends onget(Resources.with(pattern))Scanners.Resourcesconfigured -
getMemberParameterNames
get parameter names of the givenmember, either method or constructordepends on
MethodParameterNamesScannerconfigured -
getMemberUsage
get code usages for the givenmember, either field, method or constructordepends on
MemberUsageScannerconfigured -
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
-
getStore
-
getConfiguration
returns theConfigurationobject of this instance -
save
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
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 -
loaders
ClassLoader[] loaders()
-