|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.reflections.ReflectionUtils
public abstract class ReflectionUtils
convenient java reflection helper methods
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)
for example, getting all getters would be:
Set<Method> getters = getAllMethods(someClasses,
Predicates.and(
withModifier(Modifier.PUBLIC),
withPrefix("get"),
withParametersCount(0)));
| Field Summary | |
|---|---|
static boolean |
includeObject
would include Object.class when getAllSuperTypes(Class). |
| Constructor Summary | |
|---|---|
ReflectionUtils()
|
|
| Method Summary | ||
|---|---|---|
static Class<?> |
forName(String typeName,
ClassLoader... classLoaders)
tries to resolve a java type name to a Class |
|
static
|
forNames(Iterable<String> classes,
ClassLoader... classLoaders)
try to resolve all given string representation of types to a list of java types |
|
static
|
getAll(Set<T> elements,
com.google.common.base.Predicate<? super T>... predicates)
filter all given elements with predicates, if given |
|
static
|
getAllAnnotations(T type,
com.google.common.base.Predicate<Annotation>... predicates)
get all annotations of given type, up the super class hierarchy, optionally honorInherited, optionally filtered by predicates |
|
static Set<Constructor> |
getAllConstructors(Class<?> type,
com.google.common.base.Predicate<? super Constructor>... predicates)
get all constructors of given type, up the super class hierarchy, optionally filtered by predicates |
|
static Set<Field> |
getAllFields(Class<?> type,
com.google.common.base.Predicate<? super Field>... predicates)
get all fields of given type, up the super class hierarchy, optionally filtered by predicates |
|
static Set<Method> |
getAllMethods(Class<?> type,
com.google.common.base.Predicate<? super Method>... predicates)
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 Set<Class<?>> |
getAllSuperTypes(Class<?> type,
com.google.common.base.Predicate<? super Class<?>>... predicates)
get all super types of given type, including, optionally filtered by predicates |
|
static
|
getAnnotations(T type,
com.google.common.base.Predicate<Annotation>... predicates)
get annotations of given type, optionally honorInherited, optionally filtered by predicates |
|
static Set<Constructor> |
getConstructors(Class<?> t,
com.google.common.base.Predicate<? super Constructor>... predicates)
get constructors of given type, optionally filtered by predicates |
|
static Set<Field> |
getFields(Class<?> type,
com.google.common.base.Predicate<? super Field>... predicates)
get fields of given type, optionally filtered by predicates |
|
static Set<Method> |
getMethods(Class<?> t,
com.google.common.base.Predicate<? super Method>... predicates)
get methods of given type, optionally filtered by predicates |
|
static List<String> |
names(Class<?>... types)
|
|
static List<String> |
names(Collection<Class<?>> types)
|
|
static
|
withAnnotation(Annotation annotation)
where element is annotated with given annotation, including member matching |
|
static
|
withAnnotation(Class<? extends Annotation> annotation)
where element is annotated with given annotation |
|
static
|
withAnnotations(Annotation... annotations)
where element is annotated with given annotations, including member matching |
|
static
|
withAnnotations(Class<? extends Annotation>... annotations)
where element is annotated with given annotations |
|
static com.google.common.base.Predicate<Member> |
withAnyParameterAnnotation(Annotation annotation)
when method/constructor has any parameter with an annotation matches given annotations, including member matching |
|
static com.google.common.base.Predicate<Member> |
withAnyParameterAnnotation(Class<? extends Annotation> annotationClass)
when method/constructor has any parameter with an annotation matches given annotations |
|
static
|
withModifier(int mod)
when member modifier matches given mod |
|
static
|
withName(String name)
where member name equals given name |
|
static com.google.common.base.Predicate<Member> |
withParameters(Class<?>... types)
when method/constructor parameter types equals given types |
|
static com.google.common.base.Predicate<Member> |
withParametersAssignableTo(Class... types)
when member parameter types assignable to given types |
|
static com.google.common.base.Predicate<Member> |
withParametersCount(int count)
when method/constructor parameters count equal given count |
|
static
|
withPattern(String regex)
where member's toString matches given regex |
|
static
|
withPrefix(String prefix)
where member name startsWith given prefix |
|
static
|
withReturnType(Class<T> type)
when method return type equal given type |
|
static
|
withReturnTypeAssignableTo(Class<T> type)
when method return type assignable from given type |
|
static
|
withType(Class<T> type)
when field type equal given type |
|
static
|
withTypeAssignableTo(Class<T> type)
when field type assignable to given type |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static boolean includeObject
Object.class when getAllSuperTypes(Class). default is false.
| Constructor Detail |
|---|
public ReflectionUtils()
| Method Detail |
|---|
public static Set<Class<?>> getAllSuperTypes(Class<?> type)
type, including, optionally filtered by predicates
include Object.class if includeObject is true
public static Set<Class<?>> getAllSuperTypes(Class<?> type,
com.google.common.base.Predicate<? super Class<?>>... predicates)
type, including, optionally filtered by predicates
public static Set<Method> getAllMethods(Class<?> type,
com.google.common.base.Predicate<? super Method>... predicates)
type, up the super class hierarchy, optionally filtered by predicates
public static Set<Method> getMethods(Class<?> t,
com.google.common.base.Predicate<? super Method>... predicates)
type, optionally filtered by predicates
public static Set<Constructor> getAllConstructors(Class<?> type,
com.google.common.base.Predicate<? super Constructor>... predicates)
type, up the super class hierarchy, optionally filtered by predicates
public static Set<Constructor> getConstructors(Class<?> t,
com.google.common.base.Predicate<? super Constructor>... predicates)
type, optionally filtered by predicates
public static Set<Field> getAllFields(Class<?> type,
com.google.common.base.Predicate<? super Field>... predicates)
type, up the super class hierarchy, optionally filtered by predicates
public static Set<Field> getFields(Class<?> type,
com.google.common.base.Predicate<? super Field>... predicates)
type, optionally filtered by predicates
public static <T extends AnnotatedElement> Set<Annotation> getAllAnnotations(T type,
com.google.common.base.Predicate<Annotation>... predicates)
type, up the super class hierarchy, optionally honorInherited, optionally filtered by predicates
public static <T extends AnnotatedElement> Set<Annotation> getAnnotations(T type,
com.google.common.base.Predicate<Annotation>... predicates)
type, optionally honorInherited, optionally filtered by predicates
public static <T extends AnnotatedElement> Set<T> getAll(Set<T> elements,
com.google.common.base.Predicate<? super T>... predicates)
elements with predicates, if given
public static <T extends Member> com.google.common.base.Predicate<T> withName(String name)
name
public static <T extends Member> com.google.common.base.Predicate<T> withPrefix(String prefix)
prefix
public static <T extends AnnotatedElement> com.google.common.base.Predicate<T> withPattern(String regex)
toString matches given regex
for example:
getAllMethods(someClass, withPattern("public void .*"))
public static <T extends AnnotatedElement> com.google.common.base.Predicate<T> withAnnotation(Class<? extends Annotation> annotation)
annotation
public static <T extends AnnotatedElement> com.google.common.base.Predicate<T> withAnnotations(Class<? extends Annotation>... annotations)
annotations
public static <T extends AnnotatedElement> com.google.common.base.Predicate<T> withAnnotation(Annotation annotation)
annotation, including member matching
public static <T extends AnnotatedElement> com.google.common.base.Predicate<T> withAnnotations(Annotation... annotations)
annotations, including member matching
public static com.google.common.base.Predicate<Member> withParameters(Class<?>... types)
types
public static com.google.common.base.Predicate<Member> withParametersAssignableTo(Class... types)
types
public static com.google.common.base.Predicate<Member> withParametersCount(int count)
count
public static com.google.common.base.Predicate<Member> withAnyParameterAnnotation(Class<? extends Annotation> annotationClass)
annotations
public static com.google.common.base.Predicate<Member> withAnyParameterAnnotation(Annotation annotation)
annotations, including member matching
public static <T> com.google.common.base.Predicate<Field> withType(Class<T> type)
type
public static <T> com.google.common.base.Predicate<Field> withTypeAssignableTo(Class<T> type)
type
public static <T> com.google.common.base.Predicate<Method> withReturnType(Class<T> type)
type
public static <T> com.google.common.base.Predicate<Method> withReturnTypeAssignableTo(Class<T> type)
type
public static <T extends Member> com.google.common.base.Predicate<T> withModifier(int mod)
mod
for example:
withModifier(Modifier.PUBLIC) withModifier(Modifier.PROTECTED | 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)
@Nonnull public static List<String> names(Class<?>... types)
public static List<String> names(Collection<Class<?>> types)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||