Package org.reflections
Class ReflectionUtils
- java.lang.Object
-
- org.reflections.ReflectionUtils
-
public abstract class ReflectionUtils extends Object
convenient java reflection helper methods1. some helper methods to get type by name:
forName(String, ClassLoader...)
andforNames(Collection, ClassLoader...)
)}2. some helper methods to get all types/methods/fields/constructors/properties matching some predicates, generally:
Set<?> result = getAllXXX(type/s, withYYY)
where get methods are:
getAllSuperTypes(Class, java.util.function.Predicate...)
getAllFields(Class, java.util.function.Predicate...)
getAllMethods(Class, java.util.function.Predicate...)
getAllConstructors(Class, java.util.function.Predicate...)
and predicates included here all starts with "with", such as
withAnnotation(java.lang.annotation.Annotation)
withModifier(int)
withName(String)
withParameters(Class[])
withAnyParameterAnnotation(Class)
withParametersAssignableTo(Class[])
withParametersAssignableFrom(Class[])
withPrefix(String)
withReturnType(Class)
withType(Class)
withTypeAssignableTo(java.lang.Class<T>)
for example, getting all getters would be:Set<Method> getters = getAllMethods(someClasses, Predicates.and( withModifier(Modifier.PUBLIC), withPrefix("get"), withParametersCount(0)));
-
-
Field Summary
Fields Modifier and Type Field Description static boolean
includeObject
would includeObject.class
whengetAllSuperTypes(Class, java.util.function.Predicate[])
.
-
Constructor Summary
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Class<?>
forName(String typeName, ClassLoader... classLoaders)
tries to resolve a java type name to a Classstatic <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 typesstatic <T extends AnnotatedElement>
Set<T>getAll(Set<T> elements, Predicate<? super T>... predicates)
filter all givenelements
withpredicates
, if givenstatic <T extends AnnotatedElement>
Set<Annotation>getAllAnnotations(T type, Predicate<Annotation>... predicates)
get all annotations of giventype
, up the super class hierarchy, optionally filtered bypredicates
static Set<Constructor>
getAllConstructors(Class<?> type, Predicate<? super Constructor>... predicates)
get all constructors of giventype
, up the super class hierarchy, optionally filtered bypredicates
static Set<Field>
getAllFields(Class<?> type, Predicate<? super Field>... predicates)
get all fields of giventype
, up the super class hierarchy, optionally filtered bypredicates
static Set<Method>
getAllMethods(Class<?> type, Predicate<? super Method>... predicates)
get all methods of giventype
, up the super class hierarchy, optionally filtered bypredicates
static Set<Class<?>>
getAllSuperTypes(Class<?> type, Predicate<? super Class<?>>... predicates)
get all super types of giventype
, including, optionally filtered bypredicates
static <T extends AnnotatedElement>
Set<Annotation>getAnnotations(T type, Predicate<Annotation>... predicates)
get annotations of giventype
, optionally honorInherited, optionally filtered bypredicates
static Set<Constructor>
getConstructors(Class<?> t, Predicate<? super Constructor>... predicates)
get constructors of giventype
, optionally filtered bypredicates
static Set<Field>
getFields(Class<?> type, Predicate<? super Field>... predicates)
get fields of giventype
, optionally filtered bypredicates
static Set<Method>
getMethods(Class<?> t, Predicate<? super Method>... predicates)
get methods of giventype
, optionally filtered bypredicates
static Set<Class<?>>
getSuperTypes(Class<?> type)
get the immediate supertype and interfaces of the giventype
static <T extends AnnotatedElement>
Predicate<T>withAnnotation(Annotation annotation)
where element is annotated with givenannotation
, including member matchingstatic <T extends AnnotatedElement>
Predicate<T>withAnnotation(Class<? extends Annotation> annotation)
where element is annotated with givenannotation
static <T extends AnnotatedElement>
Predicate<T>withAnnotations(Annotation... annotations)
where element is annotated with givenannotations
, including member matchingstatic <T extends AnnotatedElement>
Predicate<T>withAnnotations(Class<? extends Annotation>... annotations)
where element is annotated with givenannotations
static Predicate<Member>
withAnyParameterAnnotation(Annotation annotation)
when method/constructor has any parameter with an annotation matches givenannotations
, including member matchingstatic Predicate<Member>
withAnyParameterAnnotation(Class<? extends Annotation> annotationClass)
when method/constructor has any parameter with an annotation matches givenannotations
static Predicate<Class<?>>
withClassModifier(int mod)
when class modifier matches givenmod
static <T extends Member>
Predicate<T>withModifier(int mod)
when member modifier matches givenmod
static <T extends Member>
Predicate<T>withName(String name)
where member name equals givenname
static Predicate<Member>
withParameters(Class<?>... types)
when method/constructor parameter types equals giventypes
static Predicate<Member>
withParametersAssignableFrom(Class... types)
when method/constructor parameter types assignable from giventypes
static Predicate<Member>
withParametersAssignableTo(Class... types)
when member parameter types assignable to giventypes
static Predicate<Member>
withParametersCount(int count)
when method/constructor parameters count equal givencount
static <T extends AnnotatedElement>
Predicate<T>withPattern(String regex)
where member'stoString
matches givenregex
static <T extends Member>
Predicate<T>withPrefix(String prefix)
where member name startsWith givenprefix
static <T> Predicate<Method>
withReturnType(Class<T> type)
when method return type equal giventype
static <T> Predicate<Method>
withReturnTypeAssignableTo(Class<T> type)
when method return type assignable from giventype
static <T> Predicate<Field>
withType(Class<T> type)
when field type equal giventype
static <T> Predicate<Field>
withTypeAssignableTo(Class<T> type)
when field type assignable to giventype
-
-
-
Field Detail
-
includeObject
public static boolean includeObject
would includeObject.class
whengetAllSuperTypes(Class, java.util.function.Predicate[])
. default is false.
-
-
Method Detail
-
getAllSuperTypes
public static Set<Class<?>> getAllSuperTypes(Class<?> type, Predicate<? super Class<?>>... predicates)
get all super types of giventype
, including, optionally filtered bypredicates
include
Object.class
ifincludeObject
is true
-
getSuperTypes
public static Set<Class<?>> getSuperTypes(Class<?> type)
get the immediate supertype and interfaces of the giventype
-
getAllMethods
public static Set<Method> getAllMethods(Class<?> type, Predicate<? super Method>... predicates)
get all methods of giventype
, up the super class hierarchy, optionally filtered bypredicates
-
getMethods
public static Set<Method> getMethods(Class<?> t, Predicate<? super Method>... predicates)
get methods of giventype
, optionally filtered bypredicates
-
getAllConstructors
public static Set<Constructor> getAllConstructors(Class<?> type, Predicate<? super Constructor>... predicates)
get all constructors of giventype
, up the super class hierarchy, optionally filtered bypredicates
-
getConstructors
public static Set<Constructor> getConstructors(Class<?> t, Predicate<? super Constructor>... predicates)
get constructors of giventype
, optionally filtered bypredicates
-
getAllFields
public static Set<Field> getAllFields(Class<?> type, Predicate<? super Field>... predicates)
get all fields of giventype
, up the super class hierarchy, optionally filtered bypredicates
-
getFields
public static Set<Field> getFields(Class<?> type, Predicate<? super Field>... predicates)
get fields of giventype
, optionally filtered bypredicates
-
getAllAnnotations
public static <T extends AnnotatedElement> Set<Annotation> getAllAnnotations(T type, Predicate<Annotation>... predicates)
get all annotations of giventype
, up the super class hierarchy, optionally filtered bypredicates
-
getAnnotations
public static <T extends AnnotatedElement> Set<Annotation> getAnnotations(T type, Predicate<Annotation>... predicates)
get annotations of giventype
, optionally honorInherited, optionally filtered bypredicates
-
getAll
public static <T extends AnnotatedElement> Set<T> getAll(Set<T> elements, Predicate<? super T>... predicates)
filter all givenelements
withpredicates
, if given
-
withName
public static <T extends Member> Predicate<T> withName(String name)
where member name equals givenname
-
withPrefix
public static <T extends Member> Predicate<T> withPrefix(String prefix)
where member name startsWith givenprefix
-
withPattern
public static <T extends AnnotatedElement> Predicate<T> withPattern(String regex)
where member'stoString
matches givenregex
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 givenannotation
-
withAnnotations
public static <T extends AnnotatedElement> Predicate<T> withAnnotations(Class<? extends Annotation>... annotations)
where element is annotated with givenannotations
-
withAnnotation
public static <T extends AnnotatedElement> Predicate<T> withAnnotation(Annotation annotation)
where element is annotated with givenannotation
, including member matching
-
withAnnotations
public static <T extends AnnotatedElement> Predicate<T> withAnnotations(Annotation... annotations)
where element is annotated with givenannotations
, including member matching
-
withParameters
public static Predicate<Member> withParameters(Class<?>... types)
when method/constructor parameter types equals giventypes
-
withParametersAssignableTo
public static Predicate<Member> withParametersAssignableTo(Class... types)
when member parameter types assignable to giventypes
-
withParametersAssignableFrom
public static Predicate<Member> withParametersAssignableFrom(Class... types)
when method/constructor parameter types assignable from giventypes
-
withParametersCount
public static Predicate<Member> withParametersCount(int count)
when method/constructor parameters count equal givencount
-
withAnyParameterAnnotation
public static Predicate<Member> withAnyParameterAnnotation(Class<? extends Annotation> annotationClass)
when method/constructor has any parameter with an annotation matches givenannotations
-
withAnyParameterAnnotation
public static Predicate<Member> withAnyParameterAnnotation(Annotation annotation)
when method/constructor has any parameter with an annotation matches givenannotations
, including member matching
-
withType
public static <T> Predicate<Field> withType(Class<T> type)
when field type equal giventype
-
withTypeAssignableTo
public static <T> Predicate<Field> withTypeAssignableTo(Class<T> type)
when field type assignable to giventype
-
withReturnType
public static <T> Predicate<Method> withReturnType(Class<T> type)
when method return type equal giventype
-
withReturnTypeAssignableTo
public static <T> Predicate<Method> withReturnTypeAssignableTo(Class<T> type)
when method return type assignable from giventype
-
withModifier
public static <T extends Member> Predicate<T> withModifier(int mod)
when member modifier matches givenmod
for example:
withModifier(Modifier.PUBLIC)
-
withClassModifier
public static Predicate<Class<?>> withClassModifier(int mod)
when class modifier matches givenmod
for example:
withModifier(Modifier.PUBLIC)
-
forName
public static Class<?> forName(String typeName, ClassLoader... classLoaders)
tries to resolve a java type name to a Classif optional
ClassLoader
s are not specified, then bothClasspathHelper.contextClassLoader()
andClasspathHelper.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
-
-