Class Reflections

java.lang.Object
com.palantir.ptoss.util.Reflections

public class Reflections extends 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:
  • Field Details

    • FIELD_TO_CONTAINING_CLASS_NAME

      public static final com.google.common.base.Function<Field,String> FIELD_TO_CONTAINING_CLASS_NAME
      Function that maps a Field to the simple name for the containing class.
      See Also:
    • FIELD_TO_NAME

      public static final com.google.common.base.Function<Field,String> FIELD_TO_NAME
      Function that maps a Field to its string name.
      See Also:
    • IS_FIELD_FINAL

      public static final com.google.common.base.Predicate<Field> IS_FIELD_FINAL
      Predicate to determine whether or not the specified field is final.
      See Also:
  • Constructor Details

    • Reflections

      public Reflections()
  • Method Details

    • visitClassHierarchy

      public static void visitClassHierarchy(Class<?> klass, Visitor<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 hierarchy
      visitor - Visitor object
    • getFieldObject

      public static <T> T getFieldObject(Object object, Field field, Class<T> klass)
      Given an 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.)
    • isFieldFinal

      public static boolean isFieldFinal(Field field)
      Returns whether or not the given Field is final.
    • isFieldStatic

      public static boolean isFieldStatic(Field field)
      Returns whether or not the given Field is static.
    • isMethodPublic

      public static boolean isMethodPublic(Method method)
      Returns whether or not the given Method is public.
    • getFieldByName

      public static Field getFieldByName(Class<?> klass, String fieldName)
      Find a Field 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 List<Class<?>> getTypesOfType(Class<?> klass, 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 from targetClass
      See Also:
    • getTypesOfTypeForClassHierarchy

      public static List<Class<?>> getTypesOfTypeForClassHierarchy(Class<?> klass, Class<?> targetClass)
      Gets all inner classes assignable from targetClass 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 from targetclass
      See Also:
    • getFieldsOfType

      public static List<Field> getFieldsOfType(Class<?> klass, 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 from targetClass
      See Also:
    • getFieldsOfTypeForClassHierarchy

      public static List<Field> getFieldsOfTypeForClassHierarchy(Class<?> klass, Class<?> targetClass)
      Gets all fields assignable from targetClass 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 from targetclass
      See Also:
    • evalEnum

      public static Object evalEnum(Class<?> enumType, String value)
      Looks up an Enum value by its String name.
      Parameters:
      enumType - Enum class to query.
      value - String name for the Enum value.
      Returns:
      the actual Enum value specified by the passed name.
      See Also:
    • isClassAnnotatedForClassHierarchy

      public static boolean isClassAnnotatedForClassHierarchy(Object object, Class<? extends Annotation> annotation)
      Checks whether or not the specified Annotation exists in the passed Object's class hierarchy.
      Parameters:
      object - object to check
      annotation - 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 passed Annotation
      Parameters:
      klass - checks the Fields on this class
      annotation - looks for this Annotation
      Returns:
      list of all Fields that are annotated with the specified Annotation
    • 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 passed Annotation.
      Parameters:
      klass - checks the Fields on this class and its ancestors
      annotation - looks for this Annotation
      Returns:
      list of all Fields that are annotated with the specified Annotation
    • getParameterlessMethodsForClassHierarchy

      public static List<ObjectFieldMethod> getParameterlessMethodsForClassHierarchy(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(Class<F> klass, Class<T> returnType, String getter)
      Returns a Function that will read values from the named field from a passed object.
      Parameters:
      klass - type to read values from
      returnType - return type of read field
      getter - name of the field
      Returns:
      a Function object that, when applied to an instance of klass, returns the of type returnType that resides in field getter