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
    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");
     

    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().

    Query using get(QueryFunction), such as:
    
     Set<Class<? extends Module>> 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)

    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.

    Classloader can optionally be used for resolving runtime classes from names.

    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")
        it is preferred to use a designated resource prefix (for example META-INF/reflections but not just META-INF), so that relevant urls could be found 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())
        it is preferred to use a designated resource prefix (for example META-INF/reflections but not just META-INF), so that relevant urls could be found much faster
      • collect

        public Reflections collect​(InputStream inputStream,
                                   Serializer serializer)
        deserialize and merge saved Reflections metadata from the given input stream, using the serializer configured in this instance's Configuration

        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 using the given 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 a 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. it uses ReflectionUtils.getSuperTypes(Class).

        for example, for classes A,B,C where A supertype of B, B supertype of C:

        • if scanning C resulted in B (B->C in store), but A was not scanned (although 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))
         

        supports QueryFunction functional methods such as map, filter, flatMap:

      • getSubTypesOf

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

        depends on SubTypesScanner configured

      • getTypesAnnotatedWith

        public Set<Class<?>> getTypesAnnotatedWith​(Class<? extends Annotation> annotation)
        get types annotated with a given annotation, both classes and annotations

        Inherited is not honored by default.

        when honoring @Inherited, meta-annotation should only effect annotated super classes and its sub types

        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 TypeAnnotationsScanner and SubTypesScanner configured

      • getTypesAnnotatedWith

        public Set<Class<?>> getTypesAnnotatedWith​(Class<? extends Annotation> annotation,
                                                   boolean honorInherited)
        get types annotated with a given annotation, both classes and annotations

        Inherited is honored according to 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 TypeAnnotationsScanner and SubTypesScanner configured

      • getTypesAnnotatedWith

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

        Inherited is not honored by default

        depends on TypeAnnotationsScanner configured

      • getTypesAnnotatedWith

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

        Inherited is honored according to given honorInherited

        depends on TypeAnnotationsScanner configured

      • getMethodsAnnotatedWith

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

        depends on METHODS_ANNOTATED configured

      • getMethodsAnnotatedWith

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

        depends on METHODS_ANNOTATED configured

      • getMethodsWithSignature

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

        public Set<Method> getMethodsWithParameter​(AnnotatedElement type)
        get methods with any parameter matching type or annotated with annotation
      • getMethodsWithParameter

        public Set<Method> getMethodsWithParameter​(Annotation annotation)
        get methods with any parameter matching type or annotated with annotation
      • getMethodsReturn

        public Set<Method> getMethodsReturn​(Class<?> returnType)
        get methods with return type match given type
      • getConstructorsAnnotatedWith

        public Set<Constructor> getConstructorsAnnotatedWith​(Class<? extends Annotation> annotation)
        get all constructors annotated with a given annotation

        depends on METHODS_ANNOTATED configured

      • getConstructorsAnnotatedWith

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

        depends on METHODS_ANNOTATED configured

      • getConstructorsWithSignature

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

        public Set<Constructor> getConstructorsWithParameter​(AnnotatedElement type)
        get constructors with any parameter matching type
      • getConstructorsWithParameter

        public Set<Constructor> getConstructorsWithParameter​(Annotation annotation)
        get constructors with any parameter matching annotation
      • getConstructorsWithParameterAnnotated

        public Set<Constructor> getConstructorsWithParameterAnnotated​(Class<? extends Annotation> annotation)
        get constructors with any parameter annotated with given annotation
      • getConstructorsWithParameterAnnotated

        public Set<Constructor> getConstructorsWithParameterAnnotated​(Annotation annotation)
        get constructors with any parameter annotated with given annotation, including annotation member values matching
      • getFieldsAnnotatedWith

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

        depends on FieldAnnotationsScanner configured

      • getFieldsAnnotatedWith

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

        depends on FieldAnnotationsScanner configured

      • getResources

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

        depends on Scanners.Resources configured

      • getResources

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

        depends on ResourcesScanner configured

      • getMemberParameterNames

        public List<String> getMemberParameterNames​(Member member)
        get parameter names of given method or constructor

        depends on MethodParameterNamesScanner configured

      • getMemberUsage

        public Collection<Member> getMemberUsage​(Member member)
        get all code usages for the given member field/method/constructor

        depends on MemberUsageScanner 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 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 to a given directory and filename

        * it is preferred to specify a designated directory (for example META-INF/reflections), so that it could be found later much faster using the load method

        see the documentation for the save method on the configured Serializer

      • save

        public File save​(String filename,
                         Serializer serializer)
        serialize metadata to the given filename and Serializer

        directory and filename using given serializer

        * it is preferred to specify a designated directory (for example META-INF/reflections), so that it could be found later much faster using the load method