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 booleanincludeObjectwould includeObject.classwhengetAllSuperTypes(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 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 bypredicatesstatic Set<Field>getAllFields(Class<?> type, Predicate<? super Field>... predicates)get all fields of giventype, up the super class hierarchy, optionally filtered bypredicatesstatic Set<Method>getAllMethods(Class<?> type, Predicate<? super Method>... predicates)get all methods of giventype, up the super class hierarchy, optionally filtered bypredicatesstatic Set<Class<?>>getAllSuperTypes(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 bypredicatesstatic Set<Field>getFields(Class<?> type, Predicate<? super Field>... predicates)get fields of giventype, optionally filtered bypredicatesstatic Set<Method>getMethods(Class<?> t, Predicate<? super Method>... predicates)get methods of giventype, optionally filtered bypredicatesstatic Set<Class<?>>getSuperTypes(Class<?> type)get the immediate supertype and interfaces of the giventypestatic <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 givenannotationsstatic 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 givenannotationsstatic Predicate<Class<?>>withClassModifier(int mod)when class modifier matches givenmodstatic <T extends Member>
Predicate<T>withModifier(int mod)when member modifier matches givenmodstatic <T extends Member>
Predicate<T>withName(String name)where member name equals givennamestatic Predicate<Member>withParameters(Class<?>... types)when method/constructor parameter types equals giventypesstatic Predicate<Member>withParametersAssignableFrom(Class... types)when method/constructor parameter types assignable from giventypesstatic Predicate<Member>withParametersAssignableTo(Class... types)when member parameter types assignable to giventypesstatic Predicate<Member>withParametersCount(int count)when method/constructor parameters count equal givencountstatic <T extends AnnotatedElement>
Predicate<T>withPattern(String regex)where member'stoStringmatches givenregexstatic <T extends Member>
Predicate<T>withPrefix(String prefix)where member name startsWith givenprefixstatic <T> Predicate<Method>withReturnType(Class<T> type)when method return type equal giventypestatic <T> Predicate<Method>withReturnTypeAssignableTo(Class<T> type)when method return type assignable from giventypestatic <T> Predicate<Field>withType(Class<T> type)when field type equal giventypestatic <T> Predicate<Field>withTypeAssignableTo(Class<T> type)when field type assignable to giventype
-
-
-
Field Detail
-
includeObject
public static boolean includeObject
would includeObject.classwhengetAllSuperTypes(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 bypredicatesinclude
Object.classifincludeObjectis 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 givenelementswithpredicates, 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'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
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 givenmodfor example:
withModifier(Modifier.PUBLIC)
-
withClassModifier
public static Predicate<Class<?>> withClassModifier(int mod)
when class modifier matches givenmodfor example:
withModifier(Modifier.PUBLIC)
-
forName
public static Class<?> forName(String typeName, ClassLoader... classLoaders)
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
-
-