Package com.palantir.ptoss.util
Class Reflections
java.lang.Object
com.palantir.ptoss.util.Reflections
A collection of utility methods and classes to handle all the of the Java Reflection calls
need to wire and fire bindings.
- See Also:
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
getAnnotatedFields
(Class<?> klass, Class<? extends Annotation> annotation) Returns the list of fields on this class annotated with the passedAnnotation
getAnnotatedFieldsForClassHierarchy
(Class<?> klass, Class<? extends Annotation> annotation) Returns the list of fields on this class or any of its ancestors annotated with the passedAnnotation
.static Field
getFieldByName
(Class<?> klass, String fieldName) Find aField
based on the field name.static <T> T
getFieldObject
(Object object, Field field, Class<T> klass) getFieldsOfType
(Class<?> klass, Class<?> targetClass) Gets all fields from a given class that are assignable from the target class.getFieldsOfTypeForClassHierarchy
(Class<?> klass, Class<?> targetClass) Gets all fields assignable fromtargetClass
in the passed class's type hierarchy.static List<ObjectFieldMethod>
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
(Class<F> klass, Class<T> returnType, String getter) Returns aFunction
that will read values from the named field from a passed object.getTypesOfType
(Class<?> klass, Class<?> targetClass) Gets all inner classes from a given class that are assignable from the target class.getTypesOfTypeForClassHierarchy
(Class<?> klass, Class<?> targetClass) Gets all inner classes assignable fromtargetClass
in the passed class's type hierarchy.static boolean
isClassAnnotatedForClassHierarchy
(Object object, Class<? extends Annotation> annotation) Checks whether or not the specifiedAnnotation
exists in the passedObject
's class hierarchy.static boolean
isFieldFinal
(Field field) Returns whether or not the givenField
is final.static boolean
isFieldStatic
(Field field) Returns whether or not the givenField
is static.static boolean
isMethodPublic
(Method method) Returns whether or not the givenMethod
is public.static void
visitClassHierarchy
(Class<?> klass, Visitor<Class<?>> visitor) Starting at the bottom of a class hierarchy, visit all classes (ancestors) in the hierarchy.
-
Field Details
-
Constructor Details
-
Reflections
public Reflections()
-
-
Method Details
-
visitClassHierarchy
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
-
isFieldFinal
Returns whether or not the givenField
is final. -
isFieldStatic
Returns whether or not the givenField
is static. -
isMethodPublic
Returns whether or not the givenMethod
is public. -
getFieldByName
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
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:
-
getTypesOfTypeForClassHierarchy
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:
-
getFieldsOfType
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:
-
getFieldsOfTypeForClassHierarchy
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:
-
evalEnum
-
isClassAnnotatedForClassHierarchy
public static boolean isClassAnnotatedForClassHierarchy(Object object, Class<? extends 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 List<Field> getAnnotatedFields(Class<?> klass, Class<? extends 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 List<Field> getAnnotatedFieldsForClassHierarchy(Class<?> klass, Class<? extends 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
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(Class<F> klass, Class<T> returnType, 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
-