Class ReflectionUtils

java.lang.Object
org.reflections.ReflectionUtils

public abstract class ReflectionUtils extends Object
convenient java reflection helper methods

1. some helper methods to get type by name: forName(String, ClassLoader...) and forNames(Collection, ClassLoader...) )}

2. some helper methods to get all types/methods/fields/constructors/properties matching some predicates, generally:

 Set&#60?> result = getAllXXX(type/s, withYYY) 

where get methods are:

and predicates included here all starts with "with", such as


for example, getting all getters would be:

      Set&#60Method> getters = getAllMethods(someClasses, 
              Predicates.and(
                      withModifier(Modifier.PUBLIC), 
                      withPrefix("get"), 
                      withParametersCount(0)));
     
  • Field Details

  • Constructor Details

    • ReflectionUtils

      public ReflectionUtils()
  • Method Details

    • getAllSuperTypes

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

      include Object.class if includeObject is true

    • getSuperTypes

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

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

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

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

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

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

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

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

      public static <T extends AnnotatedElement> Set<Annotation> getAnnotations(T type, Predicate<Annotation>... predicates)
      get annotations of given type, optionally honorInherited, optionally filtered by predicates
    • getAll

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

      public static <T extends Member> Predicate<T> withName(String name)
      where member name equals given name
    • withPrefix

      public static <T extends Member> Predicate<T> withPrefix(String prefix)
      where member name startsWith given prefix
    • withPattern

      public static <T extends AnnotatedElement> Predicate<T> withPattern(String regex)
      where member's toString matches given regex

      for example:

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

      public static <T extends AnnotatedElement> Predicate<T> withAnnotation(Class<? extends Annotation> annotation)
      where element is annotated with given annotation
    • withAnnotations

      public static <T extends AnnotatedElement> Predicate<T> withAnnotations(Class<? extends Annotation>... annotations)
      where element is annotated with given annotations
    • withAnnotation

      public static <T extends AnnotatedElement> Predicate<T> withAnnotation(Annotation annotation)
      where element is annotated with given annotation, including member matching
    • withAnnotations

      public static <T extends AnnotatedElement> Predicate<T> withAnnotations(Annotation... annotations)
      where element is annotated with given annotations, including member matching
    • withParameters

      public static Predicate<Member> withParameters(Class<?>... types)
      when method/constructor parameter types equals given types
    • withParametersAssignableTo

      public static Predicate<Member> withParametersAssignableTo(Class... types)
      when member parameter types assignable to given types
    • withParametersAssignableFrom

      public static Predicate<Member> withParametersAssignableFrom(Class... types)
      when method/constructor parameter types assignable from given types
    • withParametersCount

      public static Predicate<Member> withParametersCount(int count)
      when method/constructor parameters count equal given count
    • withAnyParameterAnnotation

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

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

      public static <T> Predicate<Field> withType(Class<T> type)
      when field type equal given type
    • withTypeAssignableTo

      public static <T> Predicate<Field> withTypeAssignableTo(Class<T> type)
      when field type assignable to given type
    • withReturnType

      public static <T> Predicate<Method> withReturnType(Class<T> type)
      when method return type equal given type
    • withReturnTypeAssignableTo

      public static <T> Predicate<Method> withReturnTypeAssignableTo(Class<T> type)
      when method return type assignable from given type
    • withModifier

      public static <T extends Member> Predicate<T> withModifier(int mod)
      when member modifier matches given mod

      for example:

       withModifier(Modifier.PUBLIC)
       
    • withClassModifier

      public static Predicate<Class<?>> withClassModifier(int mod)
      when class modifier matches given mod

      for example:

       withModifier(Modifier.PUBLIC)
       
    • forName

      public static Class<?> forName(String typeName, ClassLoader... classLoaders)
      tries to resolve a java type name to a Class

      if optional ClassLoaders are not specified, then both ClasspathHelper.contextClassLoader() and ClasspathHelper.staticClassLoader() are used

    • forNames

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

      private static Class[] parameterTypes(Member member)
    • parameterAnnotations

      private static Set<Annotation> parameterAnnotations(Member member)
    • annotationTypes

      private static Set<Class<? extends Annotation>> annotationTypes(Collection<Annotation> annotations)
    • annotationTypes

      private static Class<? extends Annotation>[] annotationTypes(Annotation[] annotations)
    • initPrimitives

      private static void initPrimitives()
    • getPrimitiveNames

      private static List<String> getPrimitiveNames()
    • getPrimitiveTypes

      private static List<Class> getPrimitiveTypes()
    • getPrimitiveDescriptors

      private static List<String> getPrimitiveDescriptors()
    • areAnnotationMembersMatching

      private static boolean areAnnotationMembersMatching(Annotation annotation1, Annotation annotation2)
    • isAssignable

      private static boolean isAssignable(Class[] childClasses, Class[] parentClasses)