Package com.palantir.ptoss.util
Class Reflections
- java.lang.Object
-
- com.palantir.ptoss.util.Reflections
-
public class Reflections extends java.lang.Object
A collection of utility methods and classes to handle all the of the Java Reflection calls need to wire and fire bindings.- See Also:
- java.lang.reflect
-
-
Field Summary
Fields Modifier and Type Field Description static com.google.common.base.Function<java.lang.reflect.Field,java.lang.String>
FIELD_TO_CONTAINING_CLASS_NAME
Function
that maps aField
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 aField
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 Summary
Constructors Constructor Description Reflections()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.Object
evalEnum(java.lang.Class<?> enumType, java.lang.String value)
Looks up anEnum
value by itsString
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 passedAnnotation
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 passedAnnotation
.static java.lang.reflect.Field
getFieldByName(java.lang.Class<?> klass, java.lang.String fieldName)
Find aField
based on the field name.static <T> T
getFieldObject(java.lang.Object object, java.lang.reflect.Field field, java.lang.Class<T> klass)
Given anObject
and aField
of a knownClass
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 fromtargetClass
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 parametersstatic <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 aFunction
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 fromtargetClass
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 specifiedAnnotation
exists in the passedObject
's class hierarchy.static boolean
isFieldFinal(java.lang.reflect.Field field)
Returns whether or not the givenField
is final.static boolean
isFieldStatic(java.lang.reflect.Field field)
Returns whether or not the givenField
is static.static boolean
isMethodPublic(java.lang.reflect.Method method)
Returns whether or not the givenMethod
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.
-
-
-
Field Detail
-
FIELD_TO_CONTAINING_CLASS_NAME
public static final com.google.common.base.Function<java.lang.reflect.Field,java.lang.String> FIELD_TO_CONTAINING_CLASS_NAME
Function
that maps aField
to the simple name for the containing class.- See Also:
Class.getSimpleName()
-
FIELD_TO_NAME
public static final com.google.common.base.Function<java.lang.reflect.Field,java.lang.String> FIELD_TO_NAME
Function
that maps aField
to its string name.- See Also:
Field.getName()
-
IS_FIELD_FINAL
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.- See Also:
Modifier.isFinal(int)
-
-
Method Detail
-
visitClassHierarchy
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. Does not visit interfaces.- Parameters:
klass
- Class to use as the bottom of the class hierarchyvisitor
- Visitor object
-
getFieldObject
public static <T> T getFieldObject(java.lang.Object object, java.lang.reflect.Field field, java.lang.Class<T> klass)
Given anObject
and aField
of a knownClass
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.)
-
isFieldFinal
public static boolean isFieldFinal(java.lang.reflect.Field field)
Returns whether or not the givenField
is final.
-
isFieldStatic
public static boolean isFieldStatic(java.lang.reflect.Field field)
Returns whether or not the givenField
is static.
-
isMethodPublic
public static boolean isMethodPublic(java.lang.reflect.Method method)
Returns whether or not the givenMethod
is public.
-
getFieldByName
public static java.lang.reflect.Field getFieldByName(java.lang.Class<?> klass, java.lang.String fieldName)
Find aField
based on the field name. Will return private fields but will not look in superclasses.- Returns:
- null if there is no field found
-
getTypesOfType
public 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.- Parameters:
klass
- type to query for inner-classes.targetClass
- interface or class that inner classes must be assignable from to be returned.- Returns:
- all inner classes in
klass
that are assignable fromtargetClass
- See Also:
Class.isAssignableFrom(Class)
,Class.getDeclaredClasses()
-
getTypesOfTypeForClassHierarchy
public static java.util.List<java.lang.Class<?>> getTypesOfTypeForClassHierarchy(java.lang.Class<?> klass, java.lang.Class<?> targetClass)
Gets all inner classes assignable fromtargetClass
in the passed class's type hierarchy.- Parameters:
klass
- starting point in the type stack to query for inner classes.targetClass
- looks for inner classes that are assignable from this type.- Returns:
- all inner classes in
klass
's type hierarchy assignable fromtargetclass
- See Also:
Class.isAssignableFrom(Class)
,Class.getDeclaredClasses()
,getTypesOfType(Class, Class)
-
getFieldsOfType
public 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.- Parameters:
klass
- type to query for fields.targetClass
- interface or class that fields must be assignable from to be returned.- Returns:
- all fields in
klass
that are assignable fromtargetClass
- See Also:
Class.isAssignableFrom(Class)
,Class.getDeclaredFields()
-
getFieldsOfTypeForClassHierarchy
public static java.util.List<java.lang.reflect.Field> getFieldsOfTypeForClassHierarchy(java.lang.Class<?> klass, java.lang.Class<?> targetClass)
Gets all fields assignable fromtargetClass
in the passed class's type hierarchy.- Parameters:
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.- Returns:
- all fields declared by classes in
klass
's type hierarchy assignable fromtargetclass
- See Also:
Class.isAssignableFrom(Class)
,Class.getDeclaredClasses()
,getTypesOfType(Class, Class)
-
evalEnum
public static java.lang.Object evalEnum(java.lang.Class<?> enumType, java.lang.String value)
Looks up anEnum
value by itsString
name.- Parameters:
enumType
-Enum
class to query.value
-String
name for theEnum
value.- Returns:
- the actual
Enum
value specified by the passed name. - See Also:
Enum.valueOf(Class, String)
-
isClassAnnotatedForClassHierarchy
public static boolean isClassAnnotatedForClassHierarchy(java.lang.Object object, java.lang.Class<? extends java.lang.annotation.Annotation> annotation)
Checks whether or not the specifiedAnnotation
exists in the passedObject
's class hierarchy.- Parameters:
object
- object to checkannotation
- annotation to look for- Returns:
- true is a class in this passed object's type hierarchy is annotated with the
passed
Annotation
-
getAnnotatedFields
public 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 passedAnnotation
- Parameters:
klass
- checks theField
s on this classannotation
- looks for thisAnnotation
- Returns:
- list of all
Field
s that are annotated with the specifiedAnnotation
-
getAnnotatedFieldsForClassHierarchy
public 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 passedAnnotation
.- Parameters:
klass
- checks theField
s on this class and its ancestorsannotation
- looks for thisAnnotation
- Returns:
- list of all
Field
s that are annotated with the specifiedAnnotation
-
getParameterlessMethodsForClassHierarchy
public 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- Parameters:
object
- object to query for parameterless methods- Returns:
- a list
ObjectFieldMethod
tuples mapping the parameterless methods to the passed object.
-
getterFunction
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)
Returns aFunction
that will read values from the named field from a passed object.- Parameters:
klass
- type to read values fromreturnType
- return type of read fieldgetter
- name of the field- Returns:
- a
Function
object that, when applied to an instance ofklass
, returns the of typereturnType
that resides in fieldgetter
-
-