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:
Setinvalid input: '<'?> 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:
Setinvalid input: '<'Method> getters = getAllMethods(someClasses,
Predicates.and(
withModifier(Modifier.PUBLIC),
withPrefix("get"),
withParametersCount(0)));
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic booleanwould includeObject.classwhengetAllSuperTypes(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 booleanareAnnotationMembersMatching(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 givenelementswithpredicates, 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 bypredicatesstatic Set<Constructor> getAllConstructors(Class<?> type, Predicate<? super Constructor>... predicates) get all constructors of giventype, up the super class hierarchy, optionally filtered bypredicatesgetAllFields(Class<?> type, Predicate<? super Field>... predicates) get all fields of giventype, up the super class hierarchy, optionally filtered bypredicatesgetAllMethods(Class<?> type, Predicate<? super Method>... predicates) get all methods of giventype, up the super class hierarchy, optionally filtered bypredicatesgetAllSuperTypes(Class<?> type, Predicate<? super Class<?>>... predicates) get all super types of giventype, including, optionally filtered bypredicatesstatic <T extends AnnotatedElement>
Set<Annotation> getAnnotations(T type, Predicate<Annotation>... predicates) get annotations of giventype, optionally honorInherited, optionally filtered bypredicatesstatic Set<Constructor> getConstructors(Class<?> t, Predicate<? super Constructor>... predicates) get constructors of giventype, optionally filtered bypredicatesget fields of giventype, optionally filtered bypredicatesgetMethods(Class<?> t, Predicate<? super Method>... predicates) get methods of giventype, optionally filtered bypredicatesgetSuperTypes(Class<?> type) get the immediate supertype and interfaces of the giventypeprivate static voidprivate static booleanisAssignable(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 givenannotationstatic <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 givenannotationswithAnyParameterAnnotation(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 givenannotationswithClassModifier(int mod) when class modifier matches givenmodwithModifier(int mod) when member modifier matches givenmodwhere member name equals givennamewithParameters(Class<?>... types) when method/constructor parameter types equals giventypeswithParametersAssignableFrom(Class... types) when method/constructor parameter types assignable from giventypeswithParametersAssignableTo(Class... types) when member parameter types assignable to giventypeswithParametersCount(int count) when method/constructor parameters count equal givencountstatic <T extends AnnotatedElement>
Predicate<T> withPattern(String regex) where member'stoStringmatches givenregexwithPrefix(String prefix) where member name startsWith givenprefixwithReturnType(Class<T> type) when method return type equal giventypewithReturnTypeAssignableTo(Class<T> type) when method return type assignable from giventypewhen field type equal giventypewithTypeAssignableTo(Class<T> type) when field type assignable to giventype
-
Field Details
-
includeObject
public static boolean includeObjectwould includeObject.classwhengetAllSuperTypes(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 bypredicatesinclude
Object.classifincludeObjectis 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 givenelementswithpredicates, if given -
withName
where member name equals givenname -
withPrefix
where member name startsWith givenprefix -
withPattern
where member'stoStringmatches givenregexfor 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 givenmodfor example:
withModifier(Modifier.PUBLIC)
-
withClassModifier
when class modifier matches givenmodfor example:
withModifier(Modifier.PUBLIC)
-
forName
tries to resolve a java type name to a Classif optional
ClassLoaders 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
-