Package org.reflections
Class ReflectionUtils
- java.lang.Object
-
- org.reflections.ReflectionUtils
-
public abstract class ReflectionUtils extends java.lang.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[])
.private static java.util.List<java.lang.String>
primitiveDescriptors
private static java.util.List<java.lang.String>
primitiveNames
private static java.util.List<java.lang.Class>
primitiveTypes
-
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 Classstatic <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 typesstatic <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 givenelements
withpredicates
, if givenstatic <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 giventype
, up the super class hierarchy, optionally filtered bypredicates
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 giventype
, up the super class hierarchy, optionally filtered bypredicates
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 giventype
, up the super class hierarchy, optionally filtered bypredicates
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 giventype
, up the super class hierarchy, optionally filtered bypredicates
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 giventype
, including, optionally filtered bypredicates
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 giventype
, optionally honorInherited, optionally filtered bypredicates
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 giventype
, optionally filtered bypredicates
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 giventype
, optionally filtered bypredicates
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 giventype
, optionally filtered bypredicates
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 giventype
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 givenannotation
, including member matchingstatic <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 givenannotation
static <T extends java.lang.reflect.AnnotatedElement>
java.util.function.Predicate<T>withAnnotations(java.lang.annotation.Annotation... annotations)
where element is annotated with givenannotations
, including member matchingstatic <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 givenannotations
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 givenannotations
, including member matchingstatic 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 givenannotations
static java.util.function.Predicate<java.lang.Class<?>>
withClassModifier(int mod)
when class modifier matches givenmod
static <T extends java.lang.reflect.Member>
java.util.function.Predicate<T>withModifier(int mod)
when member modifier matches givenmod
static <T extends java.lang.reflect.Member>
java.util.function.Predicate<T>withName(java.lang.String name)
where member name equals givenname
static java.util.function.Predicate<java.lang.reflect.Member>
withParameters(java.lang.Class<?>... types)
when method/constructor parameter types equals giventypes
static java.util.function.Predicate<java.lang.reflect.Member>
withParametersAssignableFrom(java.lang.Class... types)
when method/constructor parameter types assignable from giventypes
static java.util.function.Predicate<java.lang.reflect.Member>
withParametersAssignableTo(java.lang.Class... types)
when member parameter types assignable to giventypes
static java.util.function.Predicate<java.lang.reflect.Member>
withParametersCount(int count)
when method/constructor parameters count equal givencount
static <T extends java.lang.reflect.AnnotatedElement>
java.util.function.Predicate<T>withPattern(java.lang.String regex)
where member'stoString
matches givenregex
static <T extends java.lang.reflect.Member>
java.util.function.Predicate<T>withPrefix(java.lang.String prefix)
where member name startsWith givenprefix
static <T> java.util.function.Predicate<java.lang.reflect.Method>
withReturnType(java.lang.Class<T> type)
when method return type equal giventype
static <T> java.util.function.Predicate<java.lang.reflect.Method>
withReturnTypeAssignableTo(java.lang.Class<T> type)
when method return type assignable from giventype
static <T> java.util.function.Predicate<java.lang.reflect.Field>
withType(java.lang.Class<T> type)
when field type equal giventype
static <T> java.util.function.Predicate<java.lang.reflect.Field>
withTypeAssignableTo(java.lang.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.
-
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
-
-
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 giventype
, including, optionally filtered bypredicates
include
Object.class
ifincludeObject
is true
-
getSuperTypes
public static java.util.Set<java.lang.Class<?>> getSuperTypes(java.lang.Class<?> type)
get the immediate supertype and interfaces of the giventype
-
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 giventype
, up the super class hierarchy, optionally filtered bypredicates
-
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 giventype
, optionally filtered bypredicates
-
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 giventype
, up the super class hierarchy, optionally filtered bypredicates
-
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 giventype
, optionally filtered bypredicates
-
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 giventype
, up the super class hierarchy, optionally filtered bypredicates
-
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 giventype
, optionally filtered bypredicates
-
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 giventype
, up the super class hierarchy, optionally filtered bypredicates
-
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 giventype
, optionally honorInherited, optionally filtered bypredicates
-
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 givenelements
withpredicates
, 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 givenname
-
withPrefix
public static <T extends java.lang.reflect.Member> java.util.function.Predicate<T> withPrefix(java.lang.String prefix)
where member name startsWith givenprefix
-
withPattern
public static <T extends java.lang.reflect.AnnotatedElement> java.util.function.Predicate<T> withPattern(java.lang.String regex)
where member'stoString
matches givenregex
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 givenannotation
-
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 givenannotations
-
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 givenannotation
, 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 givenannotations
, 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 giventypes
-
withParametersAssignableTo
public static java.util.function.Predicate<java.lang.reflect.Member> withParametersAssignableTo(java.lang.Class... types)
when member parameter types assignable to giventypes
-
withParametersAssignableFrom
public static java.util.function.Predicate<java.lang.reflect.Member> withParametersAssignableFrom(java.lang.Class... types)
when method/constructor parameter types assignable from giventypes
-
withParametersCount
public static java.util.function.Predicate<java.lang.reflect.Member> withParametersCount(int count)
when method/constructor parameters count equal givencount
-
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 givenannotations
-
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 givenannotations
, 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 giventype
-
withTypeAssignableTo
public static <T> java.util.function.Predicate<java.lang.reflect.Field> withTypeAssignableTo(java.lang.Class<T> type)
when field type assignable to giventype
-
withReturnType
public static <T> java.util.function.Predicate<java.lang.reflect.Method> withReturnType(java.lang.Class<T> type)
when method return type equal giventype
-
withReturnTypeAssignableTo
public static <T> java.util.function.Predicate<java.lang.reflect.Method> withReturnTypeAssignableTo(java.lang.Class<T> type)
when method return type assignable from giventype
-
withModifier
public static <T extends java.lang.reflect.Member> java.util.function.Predicate<T> withModifier(int mod)
when member modifier matches givenmod
for example:
withModifier(Modifier.PUBLIC)
-
withClassModifier
public static java.util.function.Predicate<java.lang.Class<?>> withClassModifier(int mod)
when class modifier matches givenmod
for example:
withModifier(Modifier.PUBLIC)
-
forName
public static java.lang.Class<?> forName(java.lang.String typeName, java.lang.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> 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)
-
-