public class Reflections extends Object implements NameHelper
Using Reflections you can query for example:
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)
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)
Modifier and Type | Field and Description |
---|---|
protected Configuration |
configuration |
static org.slf4j.Logger |
log |
protected Store |
store |
primitiveDescriptors, primitiveNames, primitiveTypes
Modifier | Constructor and Description |
---|---|
protected |
Reflections() |
|
Reflections(Configuration configuration)
constructs Reflections instance and scan according to the given
Configuration |
|
Reflections(Object... params)
Convenient constructor for Reflections.
|
|
Reflections(Store store) |
|
Reflections(String prefix,
Scanner... scanners)
constructs Reflections instance and scan according to the given package
prefix and optional scanners |
Modifier and Type | Method and Description |
---|---|
static Reflections |
collect()
collect saved Reflection xml resources and merge it into a Reflections instance
|
Reflections |
collect(File file,
Serializer serializer)
deserialize and merge saved Reflections metadata from the given
file and serializer |
Reflections |
collect(InputStream inputStream,
Serializer serializer)
deserialize and merge saved Reflections metadata from the given
inputStream and serializer |
static Reflections |
collect(String packagePrefix,
Predicate<String> resourceNameFilter)
collect saved Reflections metadata from all urls that contains the given
packagePrefix and matches the given resourceNameFilter ,
and deserialize using the default serializer XmlSerializer |
static Reflections |
collect(String packagePrefix,
Predicate<String> resourceNameFilter,
Serializer serializer)
collect saved Reflections metadata from all urls that contains the given
packagePrefix and matches the given resourceNameFilter ,
and deserializes using the given serializer |
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)
apply
QueryFunction on Store |
Set<String> |
getAll(Scanner scanner)
returns all key and values scanned by the given
scanner |
Set<String> |
getAllTypes()
Deprecated.
|
Configuration |
getConfiguration()
returns the
Configuration object of this instance |
Set<Constructor> |
getConstructorsAnnotatedWith(Annotation annotation)
get constructors annotated with the given
annotation , including annotation member values matching |
Set<Constructor> |
getConstructorsAnnotatedWith(Class<? extends Annotation> annotation)
get constructors annotated with the given
annotation |
Set<Constructor> |
getConstructorsWithParameter(AnnotatedElement type)
get constructors with any parameter matching the given
type , either class or annotation |
Set<Constructor> |
getConstructorsWithSignature(Class<?>... types)
get constructors with signature matching the given
types |
Set<Field> |
getFieldsAnnotatedWith(Annotation annotation)
get fields annotated with the given
annotation , including annotation member values matching |
Set<Field> |
getFieldsAnnotatedWith(Class<? extends Annotation> annotation)
get fields annotated with the given
annotation |
List<String> |
getMemberParameterNames(Member member)
get parameter names of the given
member , either method or constructor |
Collection<Member> |
getMemberUsage(Member member)
get code usages for the given
member , either field, method or constructor |
Set<Method> |
getMethodsAnnotatedWith(Annotation annotation)
get methods annotated with the given
annotation , including annotation member values matching |
Set<Method> |
getMethodsAnnotatedWith(Class<? extends Annotation> annotation)
get methods annotated with the given
annotation |
Set<Method> |
getMethodsReturn(Class<?> type)
get methods with return type matching the given
returnType |
Set<Method> |
getMethodsWithParameter(AnnotatedElement type)
get methods with any parameter matching the given
type , either class or annotation |
Set<Method> |
getMethodsWithSignature(Class<?>... types)
get methods with signature matching the given
types |
Set<String> |
getResources(Pattern pattern)
get resources matching the given
pattern regex |
Set<String> |
getResources(String pattern)
get resources matching the given
pattern regex |
Store |
getStore()
returns the
Store object used for storing and querying the metadata |
<T> Set<Class<? extends T>> |
getSubTypesOf(Class<T> type)
gets all subtypes in hierarchy of a given
type . |
Set<Class<?>> |
getTypesAnnotatedWith(Annotation annotation)
get types annotated with the given
annotation , both classes and annotations, including annotation member values matching |
Set<Class<?>> |
getTypesAnnotatedWith(Annotation annotation,
boolean honorInherited)
get types annotated with the given
annotation , both classes and annotations, including annotation member values matching |
Set<Class<?>> |
getTypesAnnotatedWith(Class<? extends Annotation> annotation)
get types annotated with the given
annotation , both classes and annotations |
Set<Class<?>> |
getTypesAnnotatedWith(Class<? extends Annotation> annotation,
boolean honorInherited)
get types annotated with the given
annotation , both classes and annotations |
Reflections |
merge(Reflections reflections)
merges the given
reflections instance metadata into this instance |
File |
save(String filename)
serialize metadata to the given
filename |
File |
save(String filename,
Serializer serializer)
serialize metadata to the given
filename and serializer |
protected Map<String,Map<String,Set<String>>> |
scan() |
public static final org.slf4j.Logger log
protected final transient Configuration configuration
protected final Store store
public Reflections(Configuration configuration)
Configuration
it is preferred to use ConfigurationBuilder
new Reflections(new ConfigurationBuilder()...)
public Reflections(Store store)
public Reflections(String prefix, Scanner... scanners)
prefix
and optional scanners
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 given prefix
optional scanners
defaults to Scanners.TypesAnnotated
and Scanners.SubTypes
public Reflections(Object... params)
ConfigurationBuilder.build(Object...)
for details.
it is preferred to use ConfigurationBuilder
instead.protected Reflections()
public static Reflections collect()
by default, resources are collected from all urls that contains the package META-INF/reflections and includes files matching the pattern .*-reflections.xml
public static Reflections collect(String packagePrefix, Predicate<String> resourceNameFilter)
packagePrefix
and matches the given resourceNameFilter
,
and deserialize using the default serializer XmlSerializer
Reflections.collect("META-INF/reflections/",
new FilterBuilder().includePattern(".*-reflections\\.xml")
prefer using a designated directory (for example META-INF/reflections but not just META-INF), so that collect can work much fasterpublic static Reflections collect(String packagePrefix, Predicate<String> resourceNameFilter, Serializer serializer)
packagePrefix
and matches the given resourceNameFilter
,
and deserializes using the given serializer
Reflections reflections = Reflections.collect(
"META-INF/reflections/",
new FilterBuilder().includePattern(".*-reflections\\.xml"),
new XmlSerializer())
prefer using a designated directory (for example META-INF/reflections but not just META-INF), so that collect can work much fasterpublic Reflections collect(InputStream inputStream, Serializer serializer)
inputStream
and serializer
useful if you know the serialized resource location and prefer not to look it up the classpath
public Reflections collect(File file, Serializer serializer)
file
and serializer
useful if you know the serialized resource location and prefer not to look it up the classpath
public Reflections merge(Reflections reflections)
reflections
instance metadata into this instancepublic void expandSuperTypes(Map<String,Set<String>> map)
public <T> Set<T> get(QueryFunction<Store,T> query)
QueryFunction
on Store
Set<T> ts = get(query)
use Scanners
and ReflectionUtils
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))
public <T> Set<Class<? extends T>> getSubTypesOf(Class<T> type)
type
.
similar to get(SubTypes.of(type))
Scanners.SubTypes
configuredpublic Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation)
annotation
, both classes and annotations
Inherited
is not honored by default, see getTypesAnnotatedWith(Class, boolean)
.
similar to get(SubTypes.of(TypesAnnotated.with(annotation)))
Scanners.TypesAnnotated
and Scanners.SubTypes
configuredpublic Set<Class<?>> getTypesAnnotatedWith(Class<? extends Annotation> annotation, boolean honorInherited)
annotation
, both classes and annotations
Inherited
is honored according to the 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 onScanners.TypesAnnotated
and Scanners.SubTypes
configuredpublic Set<Class<?>> getTypesAnnotatedWith(Annotation annotation)
annotation
, both classes and annotations, including annotation member values matching
Inherited
is not honored by default, see getTypesAnnotatedWith(Annotation, boolean)
.
Scanners.TypesAnnotated
and Scanners.SubTypes
configuredpublic Set<Class<?>> getTypesAnnotatedWith(Annotation annotation, boolean honorInherited)
annotation
, both classes and annotations, including annotation member values matching
Inherited
is honored according to given honorInherited
Scanners.TypesAnnotated
and Scanners.SubTypes
configuredpublic Set<Method> getMethodsAnnotatedWith(Class<? extends Annotation> annotation)
annotation
similar to get(MethodsAnnotated.with(annotation))
Scanners.MethodsAnnotated
configuredpublic Set<Method> getMethodsAnnotatedWith(Annotation annotation)
annotation
, including annotation member values matching
similar to get(MethodsAnnotated.with(annotation))
Scanners.MethodsAnnotated
configuredpublic Set<Method> getMethodsWithSignature(Class<?>... types)
types
similar to get(MethodsSignature.of(types))
Scanners.MethodsSignature
configuredpublic Set<Method> getMethodsWithParameter(AnnotatedElement type)
type
, either class or annotation
similar to get(MethodsParameter.with(type))
Scanners.MethodsParameter
configuredpublic Set<Method> getMethodsReturn(Class<?> type)
returnType
similar to get(MethodsReturn.of(type))
Scanners.MethodsParameter
configuredpublic Set<Constructor> getConstructorsAnnotatedWith(Class<? extends Annotation> annotation)
annotation
similar to get(ConstructorsAnnotated.with(annotation))
Scanners.ConstructorsAnnotated
configuredpublic Set<Constructor> getConstructorsAnnotatedWith(Annotation annotation)
annotation
, including annotation member values matching
similar to get(ConstructorsAnnotated.with(annotation))
Scanners.ConstructorsAnnotated
configuredpublic Set<Constructor> getConstructorsWithSignature(Class<?>... types)
types
similar to get(ConstructorsSignature.with(types))
Scanners.ConstructorsSignature
configuredpublic Set<Constructor> getConstructorsWithParameter(AnnotatedElement type)
type
, either class or annotation
similar to get(ConstructorsParameter.with(types))
Scanners.ConstructorsParameter
configuredpublic Set<Field> getFieldsAnnotatedWith(Class<? extends Annotation> annotation)
annotation
similar to get(FieldsAnnotated.with(annotation))
Scanners.FieldsAnnotated
configuredpublic Set<Field> getFieldsAnnotatedWith(Annotation annotation)
annotation
, including annotation member values matching
similar to get(FieldsAnnotated.with(annotation))
Scanners.FieldsAnnotated
configuredpublic Set<String> getResources(String pattern)
pattern
regex Set<String> xmls = reflections.getResources(".\*\.xml")
similar to get(Resources.with(pattern))
Scanners.Resources
configuredpublic Set<String> getResources(Pattern pattern)
pattern
regex Set<String> xmls = reflections.getResources(Pattern.compile(".\*\.xml"))
similar to get(Resources.with(pattern))
Scanners.Resources
configuredpublic List<String> getMemberParameterNames(Member member)
member
, either method or constructor
depends on MethodParameterNamesScanner
configured
public Collection<Member> getMemberUsage(Member member)
member
, either field, method or constructor
depends on MemberUsageScanner
configured
@Deprecated public Set<String> getAllTypes()
Scanners.SubTypes
scanner
using this api is discouraged, it is better to get elements by specific criteria such as SubTypes.of(Class)
or TypesAnnotated.with(Class)
getAll(Scanner)
insteadpublic Set<String> getAll(Scanner scanner)
scanner
Set<String> all = reflections.getAll(SubTypes)
using this is discouraged, it is better to get elements by specific criteria such as SubTypes.of(Class)
or TypesAnnotated.with(Class)
public Store getStore()
Store
object used for storing and querying the metadata
Store
is basically Map<String, Map<String, Set<String>>>
public Configuration getConfiguration()
Configuration
object of this instancepublic File save(String filename)
filename
prefer using a designated directory (for example META-INF/reflections but not just META-INF), so that collect(String, Predicate)
can work much fasterpublic File save(String filename, Serializer serializer)
filename
and serializer
prefer using a designated directory (for example META-INF/reflections but not just META-INF), so that collect(String, Predicate, Serializer)
can work much fasterCopyright © 2021. All rights reserved.