Package com.palantir.ptoss.util
Class Reflections
- java.lang.Object
-
- com.palantir.ptoss.util.Reflections
-
public class Reflections extends java.lang.ObjectA 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_NAMEFunctionthat maps aFieldto the simple name for the containing class.static com.google.common.base.Function<java.lang.reflect.Field,java.lang.String>FIELD_TO_NAMEFunctionthat maps aFieldto its string name.static com.google.common.base.Predicate<java.lang.reflect.Field>IS_FIELD_FINALPredicateto 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.ObjectevalEnum(java.lang.Class<?> enumType, java.lang.String value)Looks up anEnumvalue by itsStringname.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 passedAnnotationstatic 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.FieldgetFieldByName(java.lang.Class<?> klass, java.lang.String fieldName)Find aFieldbased on the field name.static <T> TgetFieldObject(java.lang.Object object, java.lang.reflect.Field field, java.lang.Class<T> klass)Given anObjectand aFieldof a knownClasstype, 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 fromtargetClassin 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 aFunctionthat 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 fromtargetClassin the passed class's type hierarchy.static booleanisClassAnnotatedForClassHierarchy(java.lang.Object object, java.lang.Class<? extends java.lang.annotation.Annotation> annotation)Checks whether or not the specifiedAnnotationexists in the passedObject's class hierarchy.static booleanisFieldFinal(java.lang.reflect.Field field)Returns whether or not the givenFieldis final.static booleanisFieldStatic(java.lang.reflect.Field field)Returns whether or not the givenFieldis static.static booleanisMethodPublic(java.lang.reflect.Method method)Returns whether or not the givenMethodis public.static voidvisitClassHierarchy(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
Functionthat maps aFieldto 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
Functionthat maps aFieldto 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
Predicateto 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 anObjectand aFieldof a knownClasstype, 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 givenFieldis final.
-
isFieldStatic
public static boolean isFieldStatic(java.lang.reflect.Field field)
Returns whether or not the givenFieldis static.
-
isMethodPublic
public static boolean isMethodPublic(java.lang.reflect.Method method)
Returns whether or not the givenMethodis public.
-
getFieldByName
public static java.lang.reflect.Field getFieldByName(java.lang.Class<?> klass, java.lang.String fieldName)Find aFieldbased 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
klassthat 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 fromtargetClassin 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
klassthat 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 fromtargetClassin 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 anEnumvalue by itsStringname.- Parameters:
enumType-Enumclass to query.value-Stringname for theEnumvalue.- Returns:
- the actual
Enumvalue 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 specifiedAnnotationexists 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 theFields on this classannotation- looks for thisAnnotation- Returns:
- list of all
Fields 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 theFields on this class and its ancestorsannotation- looks for thisAnnotation- Returns:
- list of all
Fields 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
ObjectFieldMethodtuples 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 aFunctionthat 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
Functionobject that, when applied to an instance ofklass, returns the of typereturnTypethat resides in fieldgetter
-
-