Class 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 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 using get(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)

    For Javadoc, source code, and more information about Reflections Library, see http://github.com/ronmamo/reflections/
    • Field Detail

      • log

        public static final org.slf4j.Logger log
      • configuration

        protected final transient Configuration configuration
      • store

        protected final Store store
    • Method Detail

      • collect

        public static Reflections collect()
        collect saved Reflection xml resources and merge it into a Reflections instance

        by 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 given 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 faster
      • collect

        public 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
        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 faster
      • collect

        public Reflections collect​(InputStream inputStream,
                                   Serializer serializer)
        deserialize and merge saved Reflections metadata from the given inputStream and serializer

        useful 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 given file and serializer

        useful if you know the serialized resource location and prefer not to look it up the classpath

      • merge

        public Reflections merge​(Reflections reflections)
        merges the given reflections instance 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)
        apply 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))
         
      • getSubTypesOf

        public <T> Set<Class<? extends T>> getSubTypesOf​(Class<T> type)
        gets all subtypes in hierarchy of a given type.

        similar to get(SubTypes.of(type))

        depends on Scanners.SubTypes configured
      • getTypesAnnotatedWith

        public Set<Class<?>> getTypesAnnotatedWith​(Class<? extends Annotation> annotation,
                                                   boolean honorInherited)
        get types annotated with the given 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 on Scanners.TypesAnnotated and Scanners.SubTypes configured
      • getTypesAnnotatedWith

        public Set<Class<?>> getTypesAnnotatedWith​(Annotation annotation,
                                                   boolean honorInherited)
        get types annotated with the given annotation, both classes and annotations, including annotation member values matching

        Inherited is honored according to given honorInherited

        depends on Scanners.TypesAnnotated and Scanners.SubTypes configured
      • getMethodsAnnotatedWith

        public Set<Method> getMethodsAnnotatedWith​(Class<? extends Annotation> annotation)
        get methods annotated with the given annotation

        similar to get(MethodsAnnotated.with(annotation))

        depends on Scanners.MethodsAnnotated configured
      • getMethodsAnnotatedWith

        public Set<Method> getMethodsAnnotatedWith​(Annotation annotation)
        get methods annotated with the given annotation, including annotation member values matching

        similar to get(MethodsAnnotated.with(annotation))

        depends on Scanners.MethodsAnnotated configured
      • getMethodsWithSignature

        public Set<Method> getMethodsWithSignature​(Class<?>... types)
        get methods with signature matching the given types

        similar to get(MethodsSignature.of(types))

        depends on Scanners.MethodsSignature configured
      • getMethodsWithParameter

        public Set<Method> getMethodsWithParameter​(AnnotatedElement type)
        get methods with any parameter matching the given type, either class or annotation

        similar to get(MethodsParameter.with(type))

        depends on Scanners.MethodsParameter configured
      • getMethodsReturn

        public Set<Method> getMethodsReturn​(Class<?> type)
        get methods with return type matching the given returnType

        similar to get(MethodsReturn.of(type))

        depends on Scanners.MethodsParameter configured
      • getConstructorsAnnotatedWith

        public Set<Constructor> getConstructorsAnnotatedWith​(Annotation annotation)
        get constructors annotated with the given annotation, including annotation member values matching

        similar to get(ConstructorsAnnotated.with(annotation))

        depends on Scanners.ConstructorsAnnotated configured
      • getConstructorsWithSignature

        public Set<Constructor> getConstructorsWithSignature​(Class<?>... types)
        get constructors with signature matching the given types

        similar to get(ConstructorsSignature.with(types))

        depends on Scanners.ConstructorsSignature configured
      • getConstructorsWithParameter

        public Set<Constructor> getConstructorsWithParameter​(AnnotatedElement type)
        get constructors with any parameter matching the given type, either class or annotation

        similar to get(ConstructorsParameter.with(types))

        depends on Scanners.ConstructorsParameter configured
      • getFieldsAnnotatedWith

        public Set<Field> getFieldsAnnotatedWith​(Class<? extends Annotation> annotation)
        get fields annotated with the given annotation

        similar to get(FieldsAnnotated.with(annotation))

        depends on Scanners.FieldsAnnotated configured
      • getFieldsAnnotatedWith

        public Set<Field> getFieldsAnnotatedWith​(Annotation annotation)
        get fields annotated with the given annotation, including annotation member values matching

        similar to get(FieldsAnnotated.with(annotation))

        depends on Scanners.FieldsAnnotated configured
      • getResources

        public Set<String> getResources​(String pattern)
        get resources matching the given pattern regex
        Set<String> xmls = reflections.getResources(".*\\.xml")

        similar to get(Resources.with(pattern))

        depends on Scanners.Resources configured
      • getResources

        public Set<String> getResources​(Pattern pattern)
        get resources matching the given pattern regex
        Set<String> xmls = reflections.getResources(Pattern.compile(".*\\.xml"))

        similar to get(Resources.with(pattern))

        depends on Scanners.Resources configured
      • getAllTypes

        @Deprecated
        public Set<String> getAllTypes()
        Deprecated.
        returns all keys and values scanned by 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)

        deprecated, use getAll(Scanner) instead
      • getAll

        public Set<String> getAll​(Scanner scanner)
        returns all key and values scanned by the given 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)

      • getStore

        public Store getStore()
        returns the Store object used for storing and querying the metadata

        Store is basically Map<String, Map<String, Set<String>>>

      • save

        public File save​(String filename)
        serialize metadata to the given filename

        prefer using a designated directory (for example META-INF/reflections but not just META-INF), so that collect(String, Predicate) can work much faster