Awesome Open Source
Awesome Open Source

Released org.reflections:reflections:0.10.2

Reflections library has ~4 million downloads per month from Maven Central, and is being used by thousands of projects and libraries.
Thank you for your continuous support! And apologize for the issues. We're looking for community collaborators to assist in reviewing pull requests and issues, please reach out.

Java runtime metadata analysis

Build Status

Reflections scans and indexes your project's classpath metadata, allowing reverse transitive query of the type system 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...

Reflections was written in the spirit of Scannotations library


Add Reflections dependency to your project:

# Maven

# Gradle
implementation 'org.reflections:reflections:0.10.2'

Create Reflections instance and use the query functions:

Reflections reflections = new Reflections("");

Set<Class<?>> subTypes =

Set<Class<?>> annotated = 

Or using previous 0.9.x APIs, for example:

Set<Class<? extends SomeType>> subTypes =

Set<Class<?>> annotated = 

Note that there are some breaking changes with Reflections 0.10+, along with performance improvements and more functional API, see below.


Creating Reflections instance requires ConfigurationBuilder, typically configured with packages and Scanners to use:

// typical usage: scan package with the default scanners SubTypes, TypesAnnotated
Reflections reflections = new Reflections(
  new ConfigurationBuilder()
    .filterInputsBy(new FilterBuilder().includePackage("")));

// or similarly using the convenient constructor
Reflections reflections = new Reflections("");

Other examples:

import static org.reflections.scanners.Scanners.*;

// scan package with specific scanners
Reflections reflections = new Reflections(
  new ConfigurationBuilder()
    .filterInputsBy(new FilterBuilder().includePackage("").excludePackage(""))
    .setScanners(TypesAnnotated, MethodsAnnotated, MethodsReturn));

// scan package with all standard scanners
Reflections reflections = new Reflections("", Scanners.values());

Note that:

  • Scanner must be configured in order to be queried, otherwise an empty result is returned
    If not specified, default scanners will be used SubTypes, TypesAnnotated.
    For all standard scanners use Scanners.values(). See more scanners in the source package.
  • All relevant URLs should be configured
    Consider .filterInputsBy() in case too many classes are scanned.
    If required, Reflections will expand super types in order to get the transitive closure metadata without scanning large 3rd party urls.
  • Classloader can optionally be used for resolving runtime classes from names.


Once Reflections was instantiated and scanning was successful, it can be used for querying the indexed metadata.

import static org.reflections.scanners.Scanners.*;

// SubTypes
Set<Class<?>> modules = 

// TypesAnnotated (*1)
Set<Class<?>> singletons = 

// MethodsAnnotated
Set<Method> resources =

// FieldsAnnotated
Set<Field> ids = 

// Resources
Set<String> properties = 

More scanners:

// MethodsReturn
Set<Method> voidMethods = 

// MethodsSignature
Set<Method> someMethods = 
  reflections.get(MethodsSignature.of(long.class, int.class).as(Method.class));

// MethodsParameter
Set<Method> pathParam = 

// ConstructorsAnnotated
Set<Constructor> injectables =

// ConstructorsSignature
Set<Constructor> someConstructors = 

// MethodParameterNamesScanner
List<String> parameterNames =

// MemberUsageScanner
Set<Member> usages =

See more examples in ReflectionsQueryTest.

Note that previous 0.9.x APIs are still supported

Compare Scanners and previous 0.9.x API (*)
Scanners previous 0.9.x API previous Scanner
get(SubType.of(T)) getSubTypesOf(T) SubTypesScanner
getTypesAnnotatedWith(A) (1) TypeAnnotationsScanner
get(MethodsAnnotated.with(A)) getMethodsAnnotatedWith(A) MethodAnnotationsScanner
get(ConstructorsAnnotated.with(A)) getConstructorsAnnotatedWith(A) (2) MethodAnnotationsScanner
get(FieldsAnnotated.with(A)) getFieldsAnnotatedWith(A) FieldAnnotationsScanner
get(Resources.with(regex)) getResources(regex) ResourcesScanner
get(MethodsParameter.with(P)) getMethodsWithParameter(P) (3)
get(MethodsSignature.of(P, ...)) getMethodsWithSignature(P, ...) (3)
getMethodsMatchParams(P, ...)
get(MethodsReturn.of(T)) getMethodsReturn(T) (3) "
get(ConstructorsParameter.with(P)) getConstructorsWithParameter(P) (3)
get(ConstructorsSignature.of(P, ...)) getConstructorsWithSignature(P, ...) (3)
getConstructorsMatchParams(P, ...)

Note: asClass() and as() mappings were omitted

(1): The equivalent of getTypesAnnotatedWith(A) is get(SubTypes.of(TypesAnnotated.with(A))), including SubTypes

(2): MethodsAnnotatedScanner does not include constructor annotation scanning, use instead Scanners.ConstructorsAnnotated

(3): MethodParameterScanner is obsolete, use instead as required:
Scanners.MethodsParameter, Scanners.MethodsSignature, Scanners.MethodsReturn, Scanners.ConstructorsParameter, Scanners.ConstructorsSignature


Apart from scanning classpath metadata using Javassist, Java Reflection convenient methods are available using ReflectionsUtils:

import static org.reflections.ReflectionUtils.*;

Set<Class<?>>    superTypes   = get(SuperTypes.of(T));
Set<Field>       fields       = get(Fields.of(T));
Set<Constructor> constructors = get(Constructors.of(T));
Set<Methods>     methods      = get(Methods.of(T));
Set<URL>         resources    = get(Resources.with(T));

Set<Annotation>  annotations  = get(Annotations.of(T));
Set<Class<? extends Annotation>> annotationTypes = get(AnnotationTypes.of(T));

Previous ReflectionUtils 0.9.x API is still supported though marked for removal, more info in the javadocs.

Query API

Each Scanner and ReflectionUtils function implements QueryBuilder, and supports:

  • get() - function returns direct values
  • with() or of() - function returns all transitive values

For example, Scanners.SubTypes.get(T) return direct subtypes, while Scanners.SubTypes.of(T) return transitive subtypes hierarchy. Same goes for Scanners.TypesAnnotated and ReflectionUtils.SuperTypes etc.

Next, each function implements QueryFunction, and provides fluent functional interface for composing filter(), map(), flatMap(), as() and more, such that:

// filter, as/map
QueryFunction<Store, Method> getters =

// compose Scanners and ReflectionUtils functions 
QueryFunction<Store, Method> methods = 
  SubTypes.of(type).asClass()  // <-- classpath scanned metadata
    .flatMap(Methods::of);     // <-- java reflection api

// function of function
QueryFunction<Store, Class<? extends Annotation>> queryAnnotations = 

See more in ReflectionUtilsQueryTest

A more complex example demonstrates getting merged annotations of rest controllers endpoints:

// get all annotations of RequestMapping hierarchy (GetMapping, PostMapping, ...)
Set<Class<?>> metaAnnotations =

QueryFunction<Store, Map<String, Object>> queryAnnotations =
  // get all controller endpoint methods      
    .map(method ->
      // get both method's + declaring class's RequestMapping annotations   
        .filter(a -> metaAnnotations.contains(a.annotationType())))
        // merge annotations' member values into a single hash map
        .collect(new AnnotationMergeCollector(method)));

// apply query and map merged hashmap into java annotation proxy
Set<RequestMapping> mergedAnnotations = 
    .map(map -> ReflectionUtils.toAnnotation(map, metaAnnotation)));

Check the tests folder for more examples and API usage

What else?

  • Integrating with build lifecycle
    It is sometime useful to the scanned metadata into xml/json as part of the build lifecycle for generating resources, and then collect it on bootstrap with Reflections.collect() and avoid scanning. See reflections-maven for example.
  • JavaCodeSerializer - scanned metadata can be persisted into a generated Java source code. Although less common, it can be useful for accessing types and members in a strongly typed manner. (see example)
  • AnnotationMergeCollector - can be used to merge similar annotations. (see test)
  • MemberUsageScanner - experimental scanner allow querying for member usages getMemberUsages() of packages/types/elements in the classpath. Can be used for finding usages between packages, layers, modules, types etc.


Pull requests are welcomed!!
Here are some issues labeled with please contribute ❤️
We're looking for community collaborators to assist in reviewing pull requests and issues, please reach out.

Dual licenced with Apache 2 and WTFPL, just do what the fuck you want to.

This library is published as an act of giving and generosity, from developers to developers, to promote knowledge sharing and a--hole free working environments.
Please feel free to use it, and to contribute to the developers' community in the same manner.

PayPal / Patreon


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Java (412,144
Scanner (13,964
Reflection (2,436
Javadoc (1,796