Package org.reflections
Class ReflectionUtils
java.lang.Object
org.reflections.ReflectionUtils
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<?> 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
FieldsModifier and TypeFieldDescriptionstatic boolean
would includeObject.class
whengetAllSuperTypes(Class, java.util.function.Predicate[])
. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static Class<? extends Annotation>[]
annotationTypes
(Annotation[] annotations) private static Set<Class<? extends Annotation>>
annotationTypes
(Collection<Annotation> annotations) private static boolean
areAnnotationMembersMatching
(Annotation annotation1, Annotation annotation2) static Class<?>
forName
(String typeName, ClassLoader... classLoaders) tries to resolve a java type name to a ClassforNames
(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>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
getAllFields
(Class<?> type, Predicate<? super Field>... predicates) get all fields of giventype
, up the super class hierarchy, optionally filtered bypredicates
getAllMethods
(Class<?> type, Predicate<? super Method>... predicates) get all methods of giventype
, up the super class hierarchy, optionally filtered bypredicates
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
get fields of giventype
, optionally filtered bypredicates
getMethods
(Class<?> t, Predicate<? super Method>... predicates) get methods of giventype
, optionally filtered bypredicates
getSuperTypes
(Class<?> type) get the immediate supertype and interfaces of the giventype
private static void
private static boolean
isAssignable
(Class[] childClasses, Class[] parentClasses) private static Set<Annotation>
parameterAnnotations
(Member member) private static Class[]
parameterTypes
(Member member) 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
withAnyParameterAnnotation
(Annotation annotation) when method/constructor has any parameter with an annotation matches givenannotations
, including member matchingwithAnyParameterAnnotation
(Class<? extends Annotation> annotationClass) when method/constructor has any parameter with an annotation matches givenannotations
withClassModifier
(int mod) when class modifier matches givenmod
withModifier
(int mod) when member modifier matches givenmod
where member name equals givenname
withParameters
(Class<?>... types) when method/constructor parameter types equals giventypes
withParametersAssignableFrom
(Class... types) when method/constructor parameter types assignable from giventypes
withParametersAssignableTo
(Class... types) when member parameter types assignable to giventypes
withParametersCount
(int count) when method/constructor parameters count equal givencount
static <T extends AnnotatedElement>
Predicate<T>withPattern
(String regex) where member'stoString
matches givenregex
withPrefix
(String prefix) where member name startsWith givenprefix
withReturnType
(Class<T> type) when method return type equal giventype
withReturnTypeAssignableTo
(Class<T> type) when method return type assignable from giventype
when field type equal giventype
withTypeAssignableTo
(Class<T> type) when field type assignable to giventype
-
Field Details
-
includeObject
public static boolean includeObjectwould includeObject.class
whengetAllSuperTypes(Class, java.util.function.Predicate[])
. default is false. -
primitiveNames
-
primitiveTypes
-
primitiveDescriptors
-
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
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
get the immediate supertype and interfaces of the giventype
-
getAllMethods
get all methods of giventype
, up the super class hierarchy, optionally filtered bypredicates
-
getMethods
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
get all fields of giventype
, up the super class hierarchy, optionally filtered bypredicates
-
getFields
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
where member name equals givenname
-
withPrefix
where member name startsWith givenprefix
-
withPattern
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
where element is annotated with givenannotation
, including member matching -
withAnnotations
where element is annotated with givenannotations
, including member matching -
withParameters
when method/constructor parameter types equals giventypes
-
withParametersAssignableTo
when member parameter types assignable to giventypes
-
withParametersAssignableFrom
when method/constructor parameter types assignable from giventypes
-
withParametersCount
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
when method/constructor has any parameter with an annotation matches givenannotations
, including member matching -
withType
when field type equal giventype
-
withTypeAssignableTo
when field type assignable to giventype
-
withReturnType
when method return type equal giventype
-
withReturnTypeAssignableTo
when method return type assignable from giventype
-
withModifier
when member modifier matches givenmod
for example:
withModifier(Modifier.PUBLIC)
-
withClassModifier
when class modifier matches givenmod
for example:
withModifier(Modifier.PUBLIC)
-
forName
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 -
parameterTypes
-
parameterAnnotations
-
annotationTypes
-
annotationTypes
-
initPrimitives
private static void initPrimitives() -
getPrimitiveNames
-
getPrimitiveTypes
-
getPrimitiveDescriptors
-
areAnnotationMembersMatching
-
isAssignable
-