public abstract class ReflectionUtils
extends java.lang.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[]) . |
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 and Description |
---|
ReflectionUtils() |
Modifier and Type | Method and 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.lang.Iterable<java.lang.annotation.Annotation> annotations) |
private static boolean |
areAnnotationMembersMatching(java.lang.annotation.Annotation annotation1,
java.lang.annotation.Annotation annotation2) |
(package private) static <T> java.util.Set<T> |
filter(java.lang.Iterable<T> elements,
com.google.common.base.Predicate<? super T>... predicates) |
(package private) static <T> java.util.Set<T> |
filter(T[] elements,
com.google.common.base.Predicate<? super T>... predicates) |
static java.lang.Class<?> |
forName(java.lang.String typeName,
java.lang.ClassLoader... classLoaders)
tries to resolve a java type name to a Class
|
static <T> java.util.List<java.lang.Class<? extends T>> |
forNames(java.lang.Iterable<java.lang.String> classes,
java.lang.ClassLoader... classLoaders)
try to resolve all given string representation of types to a list of java types
|
static <T extends java.lang.reflect.AnnotatedElement> |
getAll(java.util.Set<T> elements,
com.google.common.base.Predicate<? super T>... predicates)
filter all given
elements with predicates , if given |
static <T extends java.lang.reflect.AnnotatedElement> |
getAllAnnotations(T type,
com.google.common.base.Predicate<java.lang.annotation.Annotation>... predicates)
get all annotations of given
type , up the super class hierarchy, optionally filtered by predicates |
static java.util.Set<java.lang.reflect.Constructor> |
getAllConstructors(java.lang.Class<?> type,
com.google.common.base.Predicate<? super java.lang.reflect.Constructor>... predicates)
get all constructors of given
type , up the super class hierarchy, optionally filtered by predicates |
static java.util.Set<java.lang.reflect.Field> |
getAllFields(java.lang.Class<?> type,
com.google.common.base.Predicate<? super java.lang.reflect.Field>... predicates)
get all fields of given
type , up the super class hierarchy, optionally filtered by predicates |
static java.util.Set<java.lang.reflect.Method> |
getAllMethods(java.lang.Class<?> type,
com.google.common.base.Predicate<? super java.lang.reflect.Method>... predicates)
get all methods of given
type , up the super class hierarchy, optionally filtered by predicates |
static java.util.Set<java.lang.Class<?>> |
getAllSuperTypes(java.lang.Class<?> type,
com.google.common.base.Predicate<? super java.lang.Class<?>>... predicates)
get all super types of given
type , including, optionally filtered by predicates |
static <T extends java.lang.reflect.AnnotatedElement> |
getAnnotations(T type,
com.google.common.base.Predicate<java.lang.annotation.Annotation>... predicates)
get annotations of given
type , optionally honorInherited, optionally filtered by predicates |
static java.util.Set<java.lang.reflect.Constructor> |
getConstructors(java.lang.Class<?> t,
com.google.common.base.Predicate<? super java.lang.reflect.Constructor>... predicates)
get constructors of given
type , optionally filtered by predicates |
static java.util.Set<java.lang.reflect.Field> |
getFields(java.lang.Class<?> type,
com.google.common.base.Predicate<? super java.lang.reflect.Field>... predicates)
get fields of given
type , optionally filtered by predicates |
static java.util.Set<java.lang.reflect.Method> |
getMethods(java.lang.Class<?> t,
com.google.common.base.Predicate<? super java.lang.reflect.Method>... predicates)
get methods of given
type , optionally filtered by predicates |
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() |
private static void |
initPrimitives() |
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> |
withAnnotation(java.lang.annotation.Annotation annotation)
where element is annotated with given
annotation , including member matching |
static <T extends java.lang.reflect.AnnotatedElement> |
withAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
where element is annotated with given
annotation |
static <T extends java.lang.reflect.AnnotatedElement> |
withAnnotations(java.lang.annotation.Annotation... annotations)
where element is annotated with given
annotations , including member matching |
static <T extends java.lang.reflect.AnnotatedElement> |
withAnnotations(java.lang.Class<? extends java.lang.annotation.Annotation>... annotations)
where element is annotated with given
annotations |
static com.google.common.base.Predicate<java.lang.reflect.Member> |
withAnyParameterAnnotation(java.lang.annotation.Annotation annotation)
when method/constructor has any parameter with an annotation matches given
annotations , including member matching |
static com.google.common.base.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 given
annotations |
static com.google.common.base.Predicate<java.lang.Class<?>> |
withClassModifier(int mod)
when class modifier matches given
mod |
static <T extends java.lang.reflect.Member> |
withModifier(int mod)
when member modifier matches given
mod |
static <T extends java.lang.reflect.Member> |
withName(java.lang.String name)
where member name equals given
name |
static com.google.common.base.Predicate<java.lang.reflect.Member> |
withParameters(java.lang.Class<?>... types)
when method/constructor parameter types equals given
types |
static com.google.common.base.Predicate<java.lang.reflect.Member> |
withParametersAssignableTo(java.lang.Class... types)
when member parameter types assignable to given
types |
static com.google.common.base.Predicate<java.lang.reflect.Member> |
withParametersCount(int count)
when method/constructor parameters count equal given
count |
static <T extends java.lang.reflect.AnnotatedElement> |
withPattern(java.lang.String regex)
where member's
toString matches given regex |
static <T extends java.lang.reflect.Member> |
withPrefix(java.lang.String prefix)
where member name startsWith given
prefix |
static <T> com.google.common.base.Predicate<java.lang.reflect.Method> |
withReturnType(java.lang.Class<T> type)
when method return type equal given
type |
static <T> com.google.common.base.Predicate<java.lang.reflect.Method> |
withReturnTypeAssignableTo(java.lang.Class<T> type)
when method return type assignable from given
type |
static <T> com.google.common.base.Predicate<java.lang.reflect.Field> |
withType(java.lang.Class<T> type)
when field type equal given
type |
static <T> com.google.common.base.Predicate<java.lang.reflect.Field> |
withTypeAssignableTo(java.lang.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.private static java.util.List<java.lang.String> primitiveNames
private static java.util.List<java.lang.Class> primitiveTypes
private static java.util.List<java.lang.String> primitiveDescriptors
public static java.util.Set<java.lang.Class<?>> getAllSuperTypes(java.lang.Class<?> type, com.google.common.base.Predicate<? super java.lang.Class<?>>... predicates)
type
, including, optionally filtered by predicates
include Object.class
if includeObject
is true
public static java.util.Set<java.lang.reflect.Method> getAllMethods(java.lang.Class<?> type, com.google.common.base.Predicate<? super java.lang.reflect.Method>... predicates)
type
, up the super class hierarchy, optionally filtered by predicates
public static java.util.Set<java.lang.reflect.Method> getMethods(java.lang.Class<?> t, com.google.common.base.Predicate<? super java.lang.reflect.Method>... predicates)
type
, optionally filtered by predicates
public static java.util.Set<java.lang.reflect.Constructor> getAllConstructors(java.lang.Class<?> type, com.google.common.base.Predicate<? super java.lang.reflect.Constructor>... predicates)
type
, up the super class hierarchy, optionally filtered by predicates
public static java.util.Set<java.lang.reflect.Constructor> getConstructors(java.lang.Class<?> t, com.google.common.base.Predicate<? super java.lang.reflect.Constructor>... predicates)
type
, optionally filtered by predicates
public static java.util.Set<java.lang.reflect.Field> getAllFields(java.lang.Class<?> type, com.google.common.base.Predicate<? super java.lang.reflect.Field>... predicates)
type
, up the super class hierarchy, optionally filtered by predicates
public static java.util.Set<java.lang.reflect.Field> getFields(java.lang.Class<?> type, com.google.common.base.Predicate<? super java.lang.reflect.Field>... predicates)
type
, optionally filtered by predicates
public static <T extends java.lang.reflect.AnnotatedElement> java.util.Set<java.lang.annotation.Annotation> getAllAnnotations(T type, com.google.common.base.Predicate<java.lang.annotation.Annotation>... predicates)
type
, up the super class hierarchy, optionally filtered by predicates
public static <T extends java.lang.reflect.AnnotatedElement> java.util.Set<java.lang.annotation.Annotation> getAnnotations(T type, com.google.common.base.Predicate<java.lang.annotation.Annotation>... predicates)
type
, optionally honorInherited, optionally filtered by predicates
public static <T extends java.lang.reflect.AnnotatedElement> java.util.Set<T> getAll(java.util.Set<T> elements, com.google.common.base.Predicate<? super T>... predicates)
elements
with predicates
, if givenpublic static <T extends java.lang.reflect.Member> com.google.common.base.Predicate<T> withName(java.lang.String name)
name
public static <T extends java.lang.reflect.Member> com.google.common.base.Predicate<T> withPrefix(java.lang.String prefix)
prefix
public static <T extends java.lang.reflect.AnnotatedElement> com.google.common.base.Predicate<T> withPattern(java.lang.String regex)
toString
matches given regex
for example:
getAllMethods(someClass, withPattern("public void .*"))
public static <T extends java.lang.reflect.AnnotatedElement> com.google.common.base.Predicate<T> withAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
annotation
public static <T extends java.lang.reflect.AnnotatedElement> com.google.common.base.Predicate<T> withAnnotations(java.lang.Class<? extends java.lang.annotation.Annotation>... annotations)
annotations
public static <T extends java.lang.reflect.AnnotatedElement> com.google.common.base.Predicate<T> withAnnotation(java.lang.annotation.Annotation annotation)
annotation
, including member matchingpublic static <T extends java.lang.reflect.AnnotatedElement> com.google.common.base.Predicate<T> withAnnotations(java.lang.annotation.Annotation... annotations)
annotations
, including member matchingpublic static com.google.common.base.Predicate<java.lang.reflect.Member> withParameters(java.lang.Class<?>... types)
types
public static com.google.common.base.Predicate<java.lang.reflect.Member> withParametersAssignableTo(java.lang.Class... types)
types
public static com.google.common.base.Predicate<java.lang.reflect.Member> withParametersCount(int count)
count
public static com.google.common.base.Predicate<java.lang.reflect.Member> withAnyParameterAnnotation(java.lang.Class<? extends java.lang.annotation.Annotation> annotationClass)
annotations
public static com.google.common.base.Predicate<java.lang.reflect.Member> withAnyParameterAnnotation(java.lang.annotation.Annotation annotation)
annotations
, including member matchingpublic static <T> com.google.common.base.Predicate<java.lang.reflect.Field> withType(java.lang.Class<T> type)
type
public static <T> com.google.common.base.Predicate<java.lang.reflect.Field> withTypeAssignableTo(java.lang.Class<T> type)
type
public static <T> com.google.common.base.Predicate<java.lang.reflect.Method> withReturnType(java.lang.Class<T> type)
type
public static <T> com.google.common.base.Predicate<java.lang.reflect.Method> withReturnTypeAssignableTo(java.lang.Class<T> type)
type
public static <T extends java.lang.reflect.Member> com.google.common.base.Predicate<T> withModifier(int mod)
mod
for example:
withModifier(Modifier.PUBLIC)
public static com.google.common.base.Predicate<java.lang.Class<?>> withClassModifier(int mod)
mod
for example:
withModifier(Modifier.PUBLIC)
public static java.lang.Class<?> forName(java.lang.String typeName, java.lang.ClassLoader... classLoaders)
if optional ClassLoader
s are not specified, then both ClasspathHelper.contextClassLoader()
and ClasspathHelper.staticClassLoader()
are used
public static <T> java.util.List<java.lang.Class<? extends T>> forNames(java.lang.Iterable<java.lang.String> classes, java.lang.ClassLoader... classLoaders)
private static java.lang.Class[] parameterTypes(java.lang.reflect.Member member)
private static java.util.Set<java.lang.annotation.Annotation> parameterAnnotations(java.lang.reflect.Member member)
private static java.util.Set<java.lang.Class<? extends java.lang.annotation.Annotation>> annotationTypes(java.lang.Iterable<java.lang.annotation.Annotation> annotations)
private static java.lang.Class<? extends java.lang.annotation.Annotation>[] annotationTypes(java.lang.annotation.Annotation[] annotations)
private static void initPrimitives()
private static java.util.List<java.lang.String> getPrimitiveNames()
private static java.util.List<java.lang.Class> getPrimitiveTypes()
private static java.util.List<java.lang.String> getPrimitiveDescriptors()
static <T> java.util.Set<T> filter(T[] elements, com.google.common.base.Predicate<? super T>... predicates)
static <T> java.util.Set<T> filter(java.lang.Iterable<T> elements, com.google.common.base.Predicate<? super T>... predicates)
private static boolean areAnnotationMembersMatching(java.lang.annotation.Annotation annotation1, java.lang.annotation.Annotation annotation2)