Class ReflectionUtils

    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.Class<? extends java.lang.annotation.Annotation>[] annotationTypes​(java.lang.annotation.Annotation[] annotations)  
      private static java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>> annotationTypes​(java.util.Collection<java.lang.annotation.Annotation> annotations)  
      private static boolean areAnnotationMembersMatching​(java.lang.annotation.Annotation annotation1, java.lang.annotation.Annotation annotation2)  
      static java.lang.Class<?> forName​(java.lang.String typeName, java.lang.ClassLoader... classLoaders)
      tries to resolve a java type name to a Class
      static <T> java.util.Set<java.lang.Class<? extends T>> forNames​(java.util.Collection<java.lang.String> classes, java.lang.ClassLoader... classLoaders)
      try to resolve all given string representation of types to a list of java types
      static <T extends java.lang.reflect.AnnotatedElement>
      java.util.Set<T>
      getAll​(java.util.Set<T> elements, java.util.function.Predicate<? super T>... predicates)
      filter all given elements with predicates, if given
      static <T extends java.lang.reflect.AnnotatedElement>
      java.util.Set<java.lang.annotation.Annotation>
      getAllAnnotations​(T type, java.util.function.Predicate<java.lang.annotation.Annotation>... predicates)
      get all annotations of given type, up the super class hierarchy, optionally filtered by predicates
      static java.util.Set<java.lang.reflect.Constructor> getAllConstructors​(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.reflect.Constructor>... predicates)
      get all constructors of given type, up the super class hierarchy, optionally filtered by predicates
      static java.util.Set<java.lang.reflect.Field> getAllFields​(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.reflect.Field>... predicates)
      get all fields of given type, up the super class hierarchy, optionally filtered by predicates
      static java.util.Set<java.lang.reflect.Method> getAllMethods​(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.reflect.Method>... predicates)
      get all methods of given type, up the super class hierarchy, optionally filtered by predicates
      static java.util.Set<java.lang.Class<?>> getAllSuperTypes​(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.Class<?>>... predicates)
      get all super types of given type, including, optionally filtered by predicates
      static <T extends java.lang.reflect.AnnotatedElement>
      java.util.Set<java.lang.annotation.Annotation>
      getAnnotations​(T type, java.util.function.Predicate<java.lang.annotation.Annotation>... predicates)
      get annotations of given type, optionally honorInherited, optionally filtered by predicates
      static java.util.Set<java.lang.reflect.Constructor> getConstructors​(java.lang.Class<?> t, java.util.function.Predicate<? super java.lang.reflect.Constructor>... predicates)
      get constructors of given type, optionally filtered by predicates
      static java.util.Set<java.lang.reflect.Field> getFields​(java.lang.Class<?> type, java.util.function.Predicate<? super java.lang.reflect.Field>... predicates)
      get fields of given type, optionally filtered by predicates
      static java.util.Set<java.lang.reflect.Method> getMethods​(java.lang.Class<?> t, java.util.function.Predicate<? super java.lang.reflect.Method>... predicates)
      get methods of given type, optionally filtered by predicates
      private static java.util.List<java.lang.String> getPrimitiveDescriptors()  
      private static java.util.List<java.lang.String> getPrimitiveNames()  
      private static java.util.List<java.lang.Class> getPrimitiveTypes()  
      static java.util.Set<java.lang.Class<?>> getSuperTypes​(java.lang.Class<?> type)
      get the immediate supertype and interfaces of the given type
      private static void initPrimitives()  
      private static boolean isAssignable​(java.lang.Class[] childClasses, java.lang.Class[] parentClasses)  
      private static java.util.Set<java.lang.annotation.Annotation> parameterAnnotations​(java.lang.reflect.Member member)  
      private static java.lang.Class[] parameterTypes​(java.lang.reflect.Member member)  
      static <T extends java.lang.reflect.AnnotatedElement>
      java.util.function.Predicate<T>
      withAnnotation​(java.lang.annotation.Annotation annotation)
      where element is annotated with given annotation, including member matching
      static <T extends java.lang.reflect.AnnotatedElement>
      java.util.function.Predicate<T>
      withAnnotation​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
      where element is annotated with given annotation
      static <T extends java.lang.reflect.AnnotatedElement>
      java.util.function.Predicate<T>
      withAnnotations​(java.lang.annotation.Annotation... annotations)
      where element is annotated with given annotations, including member matching
      static <T extends java.lang.reflect.AnnotatedElement>
      java.util.function.Predicate<T>
      withAnnotations​(java.lang.Class<? extends java.lang.annotation.Annotation>... annotations)
      where element is annotated with given annotations
      static java.util.function.Predicate<java.lang.reflect.Member> withAnyParameterAnnotation​(java.lang.annotation.Annotation annotation)
      when method/constructor has any parameter with an annotation matches given annotations, including member matching
      static java.util.function.Predicate<java.lang.reflect.Member> withAnyParameterAnnotation​(java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
      when method/constructor has any parameter with an annotation matches given annotations
      static java.util.function.Predicate<java.lang.Class<?>> withClassModifier​(int mod)
      when class modifier matches given mod
      static <T extends java.lang.reflect.Member>
      java.util.function.Predicate<T>
      withModifier​(int mod)
      when member modifier matches given mod
      static <T extends java.lang.reflect.Member>
      java.util.function.Predicate<T>
      withName​(java.lang.String name)
      where member name equals given name
      static java.util.function.Predicate<java.lang.reflect.Member> withParameters​(java.lang.Class<?>... types)
      when method/constructor parameter types equals given types
      static java.util.function.Predicate<java.lang.reflect.Member> withParametersAssignableFrom​(java.lang.Class... types)
      when method/constructor parameter types assignable from given types
      static java.util.function.Predicate<java.lang.reflect.Member> withParametersAssignableTo​(java.lang.Class... types)
      when member parameter types assignable to given types
      static java.util.function.Predicate<java.lang.reflect.Member> withParametersCount​(int count)
      when method/constructor parameters count equal given count
      static <T extends java.lang.reflect.AnnotatedElement>
      java.util.function.Predicate<T>
      withPattern​(java.lang.String regex)
      where member's toString matches given regex
      static <T extends java.lang.reflect.Member>
      java.util.function.Predicate<T>
      withPrefix​(java.lang.String prefix)
      where member name startsWith given prefix
      static <T> java.util.function.Predicate<java.lang.reflect.Method> withReturnType​(java.lang.Class<T> type)
      when method return type equal given type
      static <T> java.util.function.Predicate<java.lang.reflect.Method> withReturnTypeAssignableTo​(java.lang.Class<T> type)
      when method return type assignable from given type
      static <T> java.util.function.Predicate<java.lang.reflect.Field> withType​(java.lang.Class<T> type)
      when field type equal given type
      static <T> java.util.function.Predicate<java.lang.reflect.Field> withTypeAssignableTo​(java.lang.Class<T> type)
      when field type assignable to given type
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • primitiveNames

        private static java.util.List<java.lang.String> primitiveNames
      • primitiveTypes

        private static java.util.List<java.lang.Class> primitiveTypes
      • primitiveDescriptors

        private static java.util.List<java.lang.String> primitiveDescriptors
    • Constructor Detail

      • ReflectionUtils

        public ReflectionUtils()
    • Method Detail

      • getAllSuperTypes

        public static java.util.Set<java.lang.Class<?>> getAllSuperTypes​(java.lang.Class<?> type,
                                                                         java.util.function.Predicate<? super java.lang.Class<?>>... predicates)
        get all super types of given type, including, optionally filtered by predicates

        include Object.class if includeObject is true

      • getSuperTypes

        public static java.util.Set<java.lang.Class<?>> getSuperTypes​(java.lang.Class<?> type)
        get the immediate supertype and interfaces of the given type
      • getAllMethods

        public static java.util.Set<java.lang.reflect.Method> getAllMethods​(java.lang.Class<?> type,
                                                                            java.util.function.Predicate<? super java.lang.reflect.Method>... predicates)
        get all methods of given type, up the super class hierarchy, optionally filtered by predicates
      • getMethods

        public static java.util.Set<java.lang.reflect.Method> getMethods​(java.lang.Class<?> t,
                                                                         java.util.function.Predicate<? super java.lang.reflect.Method>... predicates)
        get methods of given type, optionally filtered by predicates
      • getAllConstructors

        public static java.util.Set<java.lang.reflect.Constructor> getAllConstructors​(java.lang.Class<?> type,
                                                                                      java.util.function.Predicate<? super java.lang.reflect.Constructor>... predicates)
        get all constructors of given type, up the super class hierarchy, optionally filtered by predicates
      • getConstructors

        public static java.util.Set<java.lang.reflect.Constructor> getConstructors​(java.lang.Class<?> t,
                                                                                   java.util.function.Predicate<? super java.lang.reflect.Constructor>... predicates)
        get constructors of given type, optionally filtered by predicates
      • getAllFields

        public static java.util.Set<java.lang.reflect.Field> getAllFields​(java.lang.Class<?> type,
                                                                          java.util.function.Predicate<? super java.lang.reflect.Field>... predicates)
        get all fields of given type, up the super class hierarchy, optionally filtered by predicates
      • getFields

        public static java.util.Set<java.lang.reflect.Field> getFields​(java.lang.Class<?> type,
                                                                       java.util.function.Predicate<? super java.lang.reflect.Field>... predicates)
        get fields of given type, optionally filtered by predicates
      • getAllAnnotations

        public static <T extends java.lang.reflect.AnnotatedElement> java.util.Set<java.lang.annotation.Annotation> getAllAnnotations​(T type,
                                                                                                                                      java.util.function.Predicate<java.lang.annotation.Annotation>... predicates)
        get all annotations of given type, up the super class hierarchy, optionally filtered by predicates
      • getAnnotations

        public static <T extends java.lang.reflect.AnnotatedElement> java.util.Set<java.lang.annotation.Annotation> getAnnotations​(T type,
                                                                                                                                   java.util.function.Predicate<java.lang.annotation.Annotation>... predicates)
        get annotations of given type, optionally honorInherited, optionally filtered by predicates
      • getAll

        public static <T extends java.lang.reflect.AnnotatedElement> java.util.Set<T> getAll​(java.util.Set<T> elements,
                                                                                             java.util.function.Predicate<? super T>... predicates)
        filter all given elements with predicates, if given
      • withName

        public static <T extends java.lang.reflect.Member> java.util.function.Predicate<T> withName​(java.lang.String name)
        where member name equals given name
      • withPrefix

        public static <T extends java.lang.reflect.Member> java.util.function.Predicate<T> withPrefix​(java.lang.String prefix)
        where member name startsWith given prefix
      • withPattern

        public static <T extends java.lang.reflect.AnnotatedElement> java.util.function.Predicate<T> withPattern​(java.lang.String regex)
        where member's toString matches given regex

        for example:

          getAllMethods(someClass, withPattern("public void .*"))
         
      • withAnnotation

        public static <T extends java.lang.reflect.AnnotatedElement> java.util.function.Predicate<T> withAnnotation​(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
        where element is annotated with given annotation
      • withAnnotations

        public static <T extends java.lang.reflect.AnnotatedElement> java.util.function.Predicate<T> withAnnotations​(java.lang.Class<? extends java.lang.annotation.Annotation>... annotations)
        where element is annotated with given annotations
      • withAnnotation

        public static <T extends java.lang.reflect.AnnotatedElement> java.util.function.Predicate<T> withAnnotation​(java.lang.annotation.Annotation annotation)
        where element is annotated with given annotation, including member matching
      • withAnnotations

        public static <T extends java.lang.reflect.AnnotatedElement> java.util.function.Predicate<T> withAnnotations​(java.lang.annotation.Annotation... annotations)
        where element is annotated with given annotations, including member matching
      • withParameters

        public static java.util.function.Predicate<java.lang.reflect.Member> withParameters​(java.lang.Class<?>... types)
        when method/constructor parameter types equals given types
      • withParametersAssignableTo

        public static java.util.function.Predicate<java.lang.reflect.Member> withParametersAssignableTo​(java.lang.Class... types)
        when member parameter types assignable to given types
      • withParametersAssignableFrom

        public static java.util.function.Predicate<java.lang.reflect.Member> withParametersAssignableFrom​(java.lang.Class... types)
        when method/constructor parameter types assignable from given types
      • withParametersCount

        public static java.util.function.Predicate<java.lang.reflect.Member> withParametersCount​(int count)
        when method/constructor parameters count equal given count
      • withAnyParameterAnnotation

        public static java.util.function.Predicate<java.lang.reflect.Member> withAnyParameterAnnotation​(java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
        when method/constructor has any parameter with an annotation matches given annotations
      • withAnyParameterAnnotation

        public static java.util.function.Predicate<java.lang.reflect.Member> withAnyParameterAnnotation​(java.lang.annotation.Annotation annotation)
        when method/constructor has any parameter with an annotation matches given annotations, including member matching
      • withType

        public static <T> java.util.function.Predicate<java.lang.reflect.Field> withType​(java.lang.Class<T> type)
        when field type equal given type
      • withTypeAssignableTo

        public static <T> java.util.function.Predicate<java.lang.reflect.Field> withTypeAssignableTo​(java.lang.Class<T> type)
        when field type assignable to given type
      • withReturnType

        public static <T> java.util.function.Predicate<java.lang.reflect.Method> withReturnType​(java.lang.Class<T> type)
        when method return type equal given type
      • withReturnTypeAssignableTo

        public static <T> java.util.function.Predicate<java.lang.reflect.Method> withReturnTypeAssignableTo​(java.lang.Class<T> type)
        when method return type assignable from given type
      • withModifier

        public static <T extends java.lang.reflect.Member> java.util.function.Predicate<T> withModifier​(int mod)
        when member modifier matches given mod

        for example:

         withModifier(Modifier.PUBLIC)
         
      • withClassModifier

        public static java.util.function.Predicate<java.lang.Class<?>> withClassModifier​(int mod)
        when class modifier matches given mod

        for example:

         withModifier(Modifier.PUBLIC)
         
      • forNames

        public static <T> java.util.Set<java.lang.Class<? extends T>> forNames​(java.util.Collection<java.lang.String> classes,
                                                                               java.lang.ClassLoader... classLoaders)
        try to resolve all given string representation of types to a list of java types
      • parameterTypes

        private static java.lang.Class[] parameterTypes​(java.lang.reflect.Member member)
      • parameterAnnotations

        private static java.util.Set<java.lang.annotation.Annotation> parameterAnnotations​(java.lang.reflect.Member member)
      • annotationTypes

        private static java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>> annotationTypes​(java.util.Collection<java.lang.annotation.Annotation> annotations)
      • annotationTypes

        private static java.lang.Class<? extends java.lang.annotation.Annotation>[] annotationTypes​(java.lang.annotation.Annotation[] annotations)
      • initPrimitives

        private static void initPrimitives()
      • getPrimitiveNames

        private static java.util.List<java.lang.String> getPrimitiveNames()
      • getPrimitiveTypes

        private static java.util.List<java.lang.Class> getPrimitiveTypes()
      • getPrimitiveDescriptors

        private static java.util.List<java.lang.String> getPrimitiveDescriptors()
      • areAnnotationMembersMatching

        private static boolean areAnnotationMembersMatching​(java.lang.annotation.Annotation annotation1,
                                                            java.lang.annotation.Annotation annotation2)
      • isAssignable

        private static boolean isAssignable​(java.lang.Class[] childClasses,
                                            java.lang.Class[] parentClasses)