Class 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 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 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 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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 a Field 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 a Field 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)
    • Constructor Detail

      • Reflections

        public Reflections()
    • 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 hierarchy
        visitor - Visitor object
      • getFieldObject

        public 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. 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 given Field is final.
      • isFieldStatic

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

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

        public static java.lang.reflect.Field getFieldByName​(java.lang.Class<?> klass,
                                                             java.lang.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 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 from targetClass
        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 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:
        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 from targetClass
        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 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:
        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 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:
        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 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 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
        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 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.
        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 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 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