public class Reflections
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static com.google.common.base.Function<java.lang.reflect.Field,java.lang.String> |
FIELD_TO_CONTAINING_CLASS_NAME
Function that maps a Field to the simple name for the containing class. |
static com.google.common.base.Function<java.lang.reflect.Field,java.lang.String> |
FIELD_TO_NAME
Function that maps a Field to its string name. |
static com.google.common.base.Predicate<java.lang.reflect.Field> |
IS_FIELD_FINAL
Predicate to determine whether or not the specified field is final. |
| Constructor and Description |
|---|
Reflections() |
| Modifier and Type | Method and Description |
|---|---|
static java.lang.Object |
evalEnum(java.lang.Class<?> enumType,
java.lang.String value)
Looks up an
Enum value by its String name. |
static java.util.List<java.lang.reflect.Field> |
getAnnotatedFields(java.lang.Class<?> klass,
java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Returns the list of fields on this class annotated with the passed
Annotation |
static java.util.List<java.lang.reflect.Field> |
getAnnotatedFieldsForClassHierarchy(java.lang.Class<?> klass,
java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Returns the list of fields on this class or any of its ancestors annotated with the
passed
Annotation. |
static java.lang.reflect.Field |
getFieldByName(java.lang.Class<?> klass,
java.lang.String fieldName)
Find a
Field based on the field name. |
static <T> T |
getFieldObject(java.lang.Object object,
java.lang.reflect.Field field,
java.lang.Class<T> klass)
Given an
Object and a Field of a known Class type, get the field. |
static java.util.List<java.lang.reflect.Field> |
getFieldsOfType(java.lang.Class<?> klass,
java.lang.Class<?> targetClass)
Gets all fields from a given class that are assignable from the target class.
|
static java.util.List<java.lang.reflect.Field> |
getFieldsOfTypeForClassHierarchy(java.lang.Class<?> klass,
java.lang.Class<?> targetClass)
Gets all fields assignable from
targetClass in the passed class's type
hierarchy. |
static java.util.List<ObjectFieldMethod> |
getParameterlessMethodsForClassHierarchy(java.lang.Object object)
Returns all methods in the passed object's class hierarchy that do no not take parameters
|
static <F,T> com.google.common.base.Function<F,T> |
getterFunction(java.lang.Class<F> klass,
java.lang.Class<T> returnType,
java.lang.String getter)
Returns a
Function that will read values from the named field from a passed object. |
static java.util.List<java.lang.Class<?>> |
getTypesOfType(java.lang.Class<?> klass,
java.lang.Class<?> targetClass)
Gets all inner classes from a given class that are assignable from the target class.
|
static java.util.List<java.lang.Class<?>> |
getTypesOfTypeForClassHierarchy(java.lang.Class<?> klass,
java.lang.Class<?> targetClass)
Gets all inner classes assignable from
targetClass in the passed class's type
hierarchy. |
static boolean |
isClassAnnotatedForClassHierarchy(java.lang.Object object,
java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Checks whether or not the specified
Annotation exists in the passed Object's
class hierarchy. |
static boolean |
isFieldFinal(java.lang.reflect.Field field)
Returns whether or not the given
Field is final. |
static boolean |
isFieldStatic(java.lang.reflect.Field field)
Returns whether or not the given
Field is static. |
static boolean |
isMethodPublic(java.lang.reflect.Method method)
Returns whether or not the given
Method is public. |
static void |
visitClassHierarchy(java.lang.Class<?> klass,
Visitor<java.lang.Class<?>> visitor)
Starting at the bottom of a class hierarchy, visit all classes (ancestors) in the hierarchy.
|
public static final com.google.common.base.Function<java.lang.reflect.Field,java.lang.String> FIELD_TO_CONTAINING_CLASS_NAME
Function that maps a Field to the simple name for the containing class.Class.getSimpleName()public static final com.google.common.base.Function<java.lang.reflect.Field,java.lang.String> FIELD_TO_NAME
Function that maps a Field to its string name.Field.getName()public static final com.google.common.base.Predicate<java.lang.reflect.Field> IS_FIELD_FINAL
Predicate to determine whether or not the specified field is final.Modifier.isFinal(int)public static void visitClassHierarchy(java.lang.Class<?> klass,
Visitor<java.lang.Class<?>> visitor)
klass - Class to use as the bottom of the class hierarchyvisitor - Visitor objectpublic static <T> T getFieldObject(java.lang.Object object,
java.lang.reflect.Field field,
java.lang.Class<T> klass)
Object and a Field of a known Class type, get the field.
This will return the value of the field regardless of visibility modifiers (i.e., it will
return the value of private fields.)public static boolean isFieldFinal(java.lang.reflect.Field field)
Field is final.public static boolean isFieldStatic(java.lang.reflect.Field field)
Field is static.public static boolean isMethodPublic(java.lang.reflect.Method method)
Method is public.public static java.lang.reflect.Field getFieldByName(java.lang.Class<?> klass,
java.lang.String fieldName)
Field based on the field name. Will return private fields but will not
look in superclasses.public static java.util.List<java.lang.Class<?>> getTypesOfType(java.lang.Class<?> klass,
java.lang.Class<?> targetClass)
klass - type to query for inner-classes.targetClass - interface or class that inner classes must be assignable from to be
returned.klass that are assignable from targetClassClass.isAssignableFrom(Class),
Class.getDeclaredClasses()public static java.util.List<java.lang.Class<?>> getTypesOfTypeForClassHierarchy(java.lang.Class<?> klass,
java.lang.Class<?> targetClass)
targetClass in the passed class's type
hierarchy.klass - starting point in the type stack to query for inner classes.targetClass - looks for inner classes that are assignable from this type.klass's type hierarchy assignable from targetclassClass.isAssignableFrom(Class),
Class.getDeclaredClasses(),
getTypesOfType(Class, Class)public static java.util.List<java.lang.reflect.Field> getFieldsOfType(java.lang.Class<?> klass,
java.lang.Class<?> targetClass)
klass - type to query for fields.targetClass - interface or class that fields must be assignable from to be
returned.klass that are assignable from targetClassClass.isAssignableFrom(Class),
Class.getDeclaredFields()public static java.util.List<java.lang.reflect.Field> getFieldsOfTypeForClassHierarchy(java.lang.Class<?> klass,
java.lang.Class<?> targetClass)
targetClass in the passed class's type
hierarchy.klass - starting point in the type stack to query for fields of the specified type.targetClass - looks for fields that are assignable from this type.klass's type hierarchy assignable from
targetclassClass.isAssignableFrom(Class),
Class.getDeclaredClasses(),
getTypesOfType(Class, Class)public static java.lang.Object evalEnum(java.lang.Class<?> enumType,
java.lang.String value)
Enum value by its String name.enumType - Enum class to query.value - String name for the Enum value.Enum value specified by the passed name.Enum.valueOf(Class, String)public static boolean isClassAnnotatedForClassHierarchy(java.lang.Object object,
java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Annotation exists in the passed Object's
class hierarchy.object - object to checkannotation - annotation to look forAnnotationpublic static java.util.List<java.lang.reflect.Field> getAnnotatedFields(java.lang.Class<?> klass,
java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Annotationklass - checks the Fields on this classannotation - looks for this AnnotationFields that are annotated with the specified Annotationpublic static java.util.List<java.lang.reflect.Field> getAnnotatedFieldsForClassHierarchy(java.lang.Class<?> klass,
java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Annotation.klass - checks the Fields on this class and its ancestorsannotation - looks for this AnnotationFields that are annotated with the specified Annotationpublic static java.util.List<ObjectFieldMethod> getParameterlessMethodsForClassHierarchy(java.lang.Object object)
object - object to query for parameterless methodsObjectFieldMethod tuples mapping the parameterless methods to
the passed object.public static <F,T> com.google.common.base.Function<F,T> getterFunction(java.lang.Class<F> klass,
java.lang.Class<T> returnType,
java.lang.String getter)
Function that will read values from the named field from a passed object.klass - type to read values fromreturnType - return type of read fieldgetter - name of the fieldFunction object that, when applied to an instance of klass, returns the
of type returnType that resides in field getter