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 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)
getAll(Iterable, 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[])
withParameterAnnotations(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)));
Constructor and Description |
---|
ReflectionUtils() |
Modifier and Type | Method and Description |
---|---|
static boolean |
areAnnotationMembersMatching(Annotation annotation1,
Annotation annotation2)
checks for annotation member values matching, based on equality of members
|
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(Iterable<? extends T> elements,
com.google.common.base.Predicate<? super T> predicate)
filter all given
elements with predicate |
static Set<Field> |
getAllFields(Class<?> type,
com.google.common.base.Predicate<? super Field> predicate)
get all fields of given
type , including, filtered by predicate |
static Set<Field> |
getAllFields(Iterable<? extends Class<?>> types,
com.google.common.base.Predicate<? super Field> predicate) |
static Set<Method> |
getAllMethods(Class<?> type,
com.google.common.base.Predicate<? super Method> predicate)
get all methods of given
type , including, filtered by predicate |
static Set<Method> |
getAllMethods(Iterable<? extends Class<?>> types,
com.google.common.base.Predicate<? super Method> predicate)
get all methods of given
types , filtered by predicate |
static Set<Class<?>> |
getAllSuperTypes(Class<?> type,
com.google.common.base.Predicate<? super Class<?>> predicate)
get all super types of given
type , including, filtered by optional predicates |
static Set<Class<?>> |
getAllSuperTypes(Iterable<? extends Class<?>> types,
com.google.common.base.Predicate<? super Class<?>> predicate) |
static <T extends AnnotatedElement> |
getMatchingAnnotations(Set<T> annotatedElements,
Annotation annotation)
returns a subset of given annotatedWith, where annotation member values matches the given annotation
|
static List<String> |
getPrimitiveDescriptors() |
static List<String> |
getPrimitiveNames() |
static List<Class> |
getPrimitiveTypes() |
static List<String> |
names(Iterable<Class<?>> types) |
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 <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 com.google.common.base.Predicate<Method> |
withParameterAnnotations(Annotation... annotations) |
static com.google.common.base.Predicate<Method> |
withParameterAnnotations(Class<? extends Annotation>... annotationClasses) |
static com.google.common.base.Predicate<Method> |
withParameters(Class<?>... types)
when method parameter types equals given
types |
static com.google.common.base.Predicate<Method> |
withParametersAssignableTo(Class... types)
when method parameter types assignable to given
types |
static com.google.common.base.Predicate<Method> |
withParametersCount(int count)
when method parameter types count equal given
count |
static <T extends Member> |
withPrefix(String prefix)
where member name startsWith given
prefix |
static <T> com.google.common.base.Predicate<Method> |
withReturnType(Class<T> type)
when method return type equal given
type |
static <T> com.google.common.base.Predicate<Method> |
withReturnTypeAssignableTo(Class<T> type)
when method return type assignable from given
type |
static <T> com.google.common.base.Predicate<Field> |
withType(Class<T> type)
when field type equal given
type |
static <T> com.google.common.base.Predicate<Field> |
withTypeAssignableTo(Class<T> type)
when field type assignable to given
type |
public static Set<Class<?>> getAllSuperTypes(Class<?> type, com.google.common.base.Predicate<? super Class<?>> predicate)
type
, including, filtered by optional predicates
for example:
Set<Class<?>> annotatedSuperTypes = getAllSuperTypes(type, withAnnotation(annotation));
public static Set<Class<?>> getAllSuperTypes(Iterable<? extends Class<?>> types, com.google.common.base.Predicate<? super Class<?>> predicate)
public static Set<Field> getAllFields(Class<?> type, com.google.common.base.Predicate<? super Field> predicate)
type
, including, filtered by predicate
for example:
Set<Field<?>> injectables = getAllFields(type, Predicates.or(withAnnotation(Inject.class), withAnnotation(Autowired.class)));
public static Set<Field> getAllFields(Iterable<? extends Class<?>> types, com.google.common.base.Predicate<? super Field> predicate)
public static Set<Method> getAllMethods(Class<?> type, com.google.common.base.Predicate<? super Method> predicate)
type
, including, filtered by predicate
for example:
Set<Method> getters = getAllMethods(someClasses, Predicates.and( withModifier(Modifier.PUBLIC), withPrefix("get"), withParametersCount(0)));
public static Set<Method> getAllMethods(Iterable<? extends Class<?>> types, com.google.common.base.Predicate<? super Method> predicate)
types
, filtered by predicate
public static <T extends AnnotatedElement> Set<T> getAll(Iterable<? extends T> elements, com.google.common.base.Predicate<? super T> predicate)
elements
with predicate
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> 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 matchingpublic static <T extends AnnotatedElement> com.google.common.base.Predicate<T> withAnnotations(Annotation... annotations)
annotations
, including member matchingpublic static com.google.common.base.Predicate<Method> withParameters(Class<?>... types)
types
public static com.google.common.base.Predicate<Method> withParametersAssignableTo(Class... types)
types
public static com.google.common.base.Predicate<Method> withParametersCount(int count)
count
public static com.google.common.base.Predicate<Method> withParameterAnnotations(Annotation... annotations)
public static com.google.common.base.Predicate<Method> withParameterAnnotations(Class<? extends Annotation>... annotationClasses)
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 boolean areAnnotationMembersMatching(Annotation annotation1, Annotation annotation2)
public static <T extends AnnotatedElement> Set<T> getMatchingAnnotations(Set<T> annotatedElements, Annotation annotation)
public static Class<?> forName(String typeName, ClassLoader... classLoaders)
if optional ClassLoader
s 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 © 2012. All Rights Reserved.