public abstract class ReflectionUtils extends Object
1. some helper methods to get type by name: forName(String, ClassLoader...) and forNames(Iterable, 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, com.google.common.base.Predicate...)
#getAllFields(Class, com.google.common.base.Predicate...)
#getAllMethods(Class, com.google.common.base.Predicate...)
#getAllConstructors(Class, com.google.common.base.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[])
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)));
| Modifier and Type | Field and Description |
|---|---|
static boolean |
includeObject
would include
Object.class when #getAllSuperTypes(Class, com.google.common.base.Predicate[]). |
| Constructor and Description |
|---|
ReflectionUtils() |
| Modifier and Type | Method and Description |
|---|---|
static Class<?> |
forName(String typeName,
ClassLoader... classLoaders)
tries to resolve a java type name to a Class
|
static <T> List<Class<? extends T>> |
forNames(Iterable<String> classes,
ClassLoader[] classLoaders)
try to resolve all given string representation of types to a list of java types
|
static <T extends AnnotatedElement> |
getAll(Set<T> elements,
filter all given
elements with predicates, if given |
static <T extends AnnotatedElement> |
getAllAnnotations(T type,
get all annotations of given
type, up the super class hierarchy, optionally filtered by predicates |
static Set<Constructor> |
getAllConstructors(Class<?> type,
get all constructors of given
type, up the super class hierarchy, optionally filtered by predicates |
static Set<Field> |
getAllFields(Class<?> type,
get all fields of given
type, up the super class hierarchy, optionally filtered by predicates |
static Set<Method> |
getAllMethods(Class<?> type,
get all methods of given
type, up the super class hierarchy, optionally filtered by predicates |
static Set<Class<?>> |
getAllSuperTypes(Class<?> type,
get all super types of given
type, including, optionally filtered by predicates |
static <T extends AnnotatedElement> |
getAnnotations(T type,
get annotations of given
type, optionally honorInherited, optionally filtered by predicates |
static Set<Constructor> |
getConstructors(Class<?> t,
get constructors of given
type, optionally filtered by predicates |
static Set<Field> |
getFields(Class<?> type,
get fields of given
type, optionally filtered by predicates |
static Set<Method> |
getMethods(Class<?> t,
get methods of given
type, optionally filtered by predicates |
static <T extends AnnotatedElement> |
withAnnotation(Annotation annotation)
where element is annotated with given
annotation, including member matching |
static <T extends AnnotatedElement> |
withAnnotation(Class<? extends Annotation> annotation)
where element is annotated with given
annotation |
static <T extends AnnotatedElement> |
withAnnotations(Annotation... annotations)
where element is annotated with given
annotations, including member matching |
static <T extends AnnotatedElement> |
withAnnotations(Class<? extends Annotation>... annotations)
where element is annotated with given
annotations |
static |
withAnyParameterAnnotation(Annotation annotation)
when method/constructor has any parameter with an annotation matches given
annotations, including member matching |
static |
withAnyParameterAnnotation(Class<? extends Annotation> annotationClass)
when method/constructor has any parameter with an annotation matches given
annotations |
static |
withClassModifier(int mod)
when class modifier matches given
mod |
static <T extends Member> |
withModifier(int mod)
when member modifier matches given
mod |
static <T extends Member> |
withName(String name)
where member name equals given
name |
static |
withParameters(Class<?>... types)
when method/constructor parameter types equals given
types |
static |
withParametersAssignableTo(Class... types)
when member parameter types assignable to given
types |
static |
withParametersCount(int count)
when method/constructor parameters count equal given
count |
static <T extends AnnotatedElement> |
withPattern(String regex)
where member's
toString matches given regex |
static <T extends Member> |
withPrefix(String prefix)
where member name startsWith given
prefix |
static <T> |
withReturnType(Class<T> type)
when method return type equal given
type |
static <T> |
withReturnTypeAssignableTo(Class<T> type)
when method return type assignable from given
type |
static <T> |
withType(Class<T> type)
when field type equal given
type |
static <T> |
withTypeAssignableTo(Class<T> type)
when field type assignable to given
type |
public static boolean includeObject
Object.class when #getAllSuperTypes(Class, com.google.common.base.Predicate[]). default is false.public static Set<Class<?>> getAllSuperTypes(Class<?> type,... predicates)
type, including, optionally filtered by predicates
include Object.class if includeObject is true
public static Set<Method> getAllMethods(Class<?> type,... predicates)
type, up the super class hierarchy, optionally filtered by predicatespublic static Set<Method> getMethods(Class<?> t,... predicates)
type, optionally filtered by predicatespublic static Set<Constructor> getAllConstructors(Class<?> type,... predicates)
type, up the super class hierarchy, optionally filtered by predicatespublic static Set<Constructor> getConstructors(Class<?> t,... predicates)
type, optionally filtered by predicatespublic static Set<Field> getAllFields(Class<?> type,... predicates)
type, up the super class hierarchy, optionally filtered by predicatespublic static Set<Field> getFields(Class<?> type,... predicates)
type, optionally filtered by predicatespublic static <T extends AnnotatedElement> Set<Annotation> getAllAnnotations(T type,... predicates)
type, up the super class hierarchy, optionally filtered by predicatespublic static <T extends AnnotatedElement> Set<Annotation> getAnnotations(T type,... predicates)
type, optionally honorInherited, optionally filtered by predicatespublic static <T extends AnnotatedElement> Set<T> getAll(Set<T> elements,... predicates)
elements with predicates, if givenpublic static <T extends Member>withPrefix(String prefix)
prefixpublic static <T extends AnnotatedElement>withPattern(String regex)
toString matches given regex
for example:
getAllMethods(someClass, withPattern("public void .*"))
public static <T extends AnnotatedElement>withAnnotation(Class<? extends Annotation> annotation)
annotationpublic static <T extends AnnotatedElement>withAnnotations(Class<? extends Annotation>... annotations)
annotationspublic static <T extends AnnotatedElement>withAnnotation(Annotation annotation)
annotation, including member matchingpublic static <T extends AnnotatedElement>withAnnotations(Annotation... annotations)
annotations, including member matchingpublic staticwithParameters(Class<?>... types)
typespublic staticwithParametersAssignableTo(Class... types)
typespublic staticwithParametersCount(int count)
countpublic staticwithAnyParameterAnnotation(Class<? extends Annotation> annotationClass)
annotationspublic staticwithAnyParameterAnnotation(Annotation annotation)
annotations, including member matchingpublic static <T>withType(Class<T> type)
typepublic static <T>withTypeAssignableTo(Class<T> type)
typepublic static <T>withReturnType(Class<T> type)
typepublic static <T>withReturnTypeAssignableTo(Class<T> type)
typepublic static <T extends Member>withModifier(int mod)
mod
for example:
withModifier(Modifier.PUBLIC)
public staticwithClassModifier(int mod)
mod
for example:
withModifier(Modifier.PUBLIC)
public static Class<?> forName(String typeName, ClassLoader... classLoaders)
if optional ClassLoaders are not specified, then both ClasspathHelper.contextClassLoader() and ClasspathHelper.staticClassLoader() are used
public static <T> List<Class<? extends T>> forNames(Iterable<String> classes, ClassLoader[] classLoaders)
Copyright © 2014. All rights reserved.