Class Reflection


  • public class Reflection
    extends java.lang.Object
    Reflection utilities used to support and augment enhancement. Used both at enhancement time and at runtime.
    Author:
    Abe White
    • Constructor Summary

      Constructors 
      Constructor Description
      Reflection()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.reflect.Field findField​(java.lang.Class cls, java.lang.String name, boolean mustExist)
      Return the field with the given name, optionally throwing an exception if none.
      static java.lang.reflect.Method findGetter​(java.lang.Class cls, java.lang.String prop, boolean mustExist)
      Return the getter method matching the given property name, optionally throwing an exception if none.
      static java.lang.reflect.Method findSetter​(java.lang.Class cls, java.lang.String prop, boolean mustExist)
      Return the setter method matching the given property name, optionally throwing an exception if none.
      static java.lang.reflect.Method findSetter​(java.lang.Class cls, java.lang.String prop, java.lang.Class param, boolean mustExist)
      Return the setter method matching the given property name, optionally throwing an exception if none.
      static java.lang.Object get​(java.lang.Object target, java.lang.reflect.Field field)
      Return the value of the given field in the given object.
      static java.lang.Object get​(java.lang.Object target, java.lang.reflect.Method getter)
      Return the return value of the given getter in the given object.
      static java.util.Set<java.lang.String> getBeanStylePropertyNames​(java.lang.Class<?> c)
      Gets all bean-style property names of the given Class or its superclass.
      static boolean getBoolean​(java.lang.Object target, java.lang.reflect.Field field)
      Return the value of the given field in the given object.
      static boolean getBoolean​(java.lang.Object target, java.lang.reflect.Method getter)
      Return the return value of the given getter in the given object.
      static byte getByte​(java.lang.Object target, java.lang.reflect.Field field)
      Return the value of the given field in the given object.
      static byte getByte​(java.lang.Object target, java.lang.reflect.Method getter)
      Return the return value of the given getter in the given object.
      static char getChar​(java.lang.Object target, java.lang.reflect.Field field)
      Return the value of the given field in the given object.
      static char getChar​(java.lang.Object target, java.lang.reflect.Method getter)
      Return the return value of the given getter in the given object.
      static java.lang.Class getDeclaringClass​(java.lang.reflect.Method m)
      Gets the declaring class of the given method signature but also checks if the method is declared in an interface.
      static java.lang.reflect.Method getDeclaringMethod​(java.lang.Class c, java.lang.reflect.Method m)
      Gets the method in the given class that has the same signature of the given method, if exists.
      static double getDouble​(java.lang.Object target, java.lang.reflect.Field field)
      Return the value of the given field in the given object.
      static double getDouble​(java.lang.Object target, java.lang.reflect.Method getter)
      Return the return value of the given getter in the given object.
      static <T> java.util.Set<T> getFieldValues​(java.lang.Class c, int mods, java.lang.Class<T> t)
      Gets values of all field f the given class such that f exactly match the given modifiers and are of given type (Object implies any type) unless f is annotated as Reflectable.
      static float getFloat​(java.lang.Object target, java.lang.reflect.Field field)
      Return the value of the given field in the given object.
      static float getFloat​(java.lang.Object target, java.lang.reflect.Method getter)
      Return the return value of the given getter in the given object.
      static int getInt​(java.lang.Object target, java.lang.reflect.Field field)
      Return the value of the given field in the given object.
      static int getInt​(java.lang.Object target, java.lang.reflect.Method getter)
      Return the return value of the given getter in the given object.
      static long getLong​(java.lang.Object target, java.lang.reflect.Field field)
      Return the value of the given field in the given object.
      static long getLong​(java.lang.Object target, java.lang.reflect.Method getter)
      Return the return value of the given getter in the given object.
      static java.util.Set<java.lang.String> getPublicFieldNames​(java.lang.Class c)
      Gets all public field names of the given Class.
      static short getShort​(java.lang.Object target, java.lang.reflect.Field field)
      Return the value of the given field in the given object.
      static short getShort​(java.lang.Object target, java.lang.reflect.Method getter)
      Return the return value of the given getter in the given object.
      static java.lang.Object getValue​(java.lang.Object obj, java.lang.String prop, boolean mustExist)
      Get the value of the given named field or a corresponding getter method.
      static void set​(java.lang.Object target, boolean value, java.lang.reflect.Field field)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, byte value, java.lang.reflect.Field field)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, char value, java.lang.reflect.Field field)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, double value, java.lang.reflect.Field field)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, float value, java.lang.reflect.Field field)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, int value, java.lang.reflect.Field field)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, long value, java.lang.reflect.Field field)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, short value, java.lang.reflect.Field field)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, java.lang.Object value, java.lang.reflect.Field field)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Field field, boolean value)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Field field, byte value)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Field field, char value)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Field field, double value)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Field field, float value)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Field field, int value)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Field field, long value)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Field field, short value)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Field field, java.lang.Object value)
      Set the value of the given field in the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Method setter, boolean value)
      Invoke the given setter on the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Method setter, byte value)
      Invoke the given setter on the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Method setter, char value)
      Invoke the given setter on the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Method setter, double value)
      Invoke the given setter on the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Method setter, float value)
      Invoke the given setter on the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Method setter, int value)
      Invoke the given setter on the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Method setter, long value)
      Invoke the given setter on the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Method setter, short value)
      Invoke the given setter on the given object.
      static void set​(java.lang.Object target, java.lang.reflect.Method setter, java.lang.Object value)
      Invoke the given setter on the given object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • Reflection

        public Reflection()
    • Method Detail

      • findGetter

        public static java.lang.reflect.Method findGetter​(java.lang.Class cls,
                                                          java.lang.String prop,
                                                          boolean mustExist)
        Return the getter method matching the given property name, optionally throwing an exception if none.
      • findSetter

        public static java.lang.reflect.Method findSetter​(java.lang.Class cls,
                                                          java.lang.String prop,
                                                          boolean mustExist)
        Return the setter method matching the given property name, optionally throwing an exception if none. The property must also have a getter.
      • findSetter

        public static java.lang.reflect.Method findSetter​(java.lang.Class cls,
                                                          java.lang.String prop,
                                                          java.lang.Class param,
                                                          boolean mustExist)
        Return the setter method matching the given property name, optionally throwing an exception if none.
      • findField

        public static java.lang.reflect.Field findField​(java.lang.Class cls,
                                                        java.lang.String name,
                                                        boolean mustExist)
        Return the field with the given name, optionally throwing an exception if none.
      • get

        public static java.lang.Object get​(java.lang.Object target,
                                           java.lang.reflect.Field field)
        Return the value of the given field in the given object.
      • getValue

        public static java.lang.Object getValue​(java.lang.Object obj,
                                                java.lang.String prop,
                                                boolean mustExist)
        Get the value of the given named field or a corresponding getter method.
        Returns:
        null if the field does not exist and mustExist is set to false or the given target is null.
        Throws:
        UserException - if mustExist is true and the field or getter method is non-existent
      • getBoolean

        public static boolean getBoolean​(java.lang.Object target,
                                         java.lang.reflect.Field field)
        Return the value of the given field in the given object.
      • getByte

        public static byte getByte​(java.lang.Object target,
                                   java.lang.reflect.Field field)
        Return the value of the given field in the given object.
      • getChar

        public static char getChar​(java.lang.Object target,
                                   java.lang.reflect.Field field)
        Return the value of the given field in the given object.
      • getDouble

        public static double getDouble​(java.lang.Object target,
                                       java.lang.reflect.Field field)
        Return the value of the given field in the given object.
      • getFloat

        public static float getFloat​(java.lang.Object target,
                                     java.lang.reflect.Field field)
        Return the value of the given field in the given object.
      • getInt

        public static int getInt​(java.lang.Object target,
                                 java.lang.reflect.Field field)
        Return the value of the given field in the given object.
      • getLong

        public static long getLong​(java.lang.Object target,
                                   java.lang.reflect.Field field)
        Return the value of the given field in the given object.
      • getShort

        public static short getShort​(java.lang.Object target,
                                     java.lang.reflect.Field field)
        Return the value of the given field in the given object.
      • get

        public static java.lang.Object get​(java.lang.Object target,
                                           java.lang.reflect.Method getter)
        Return the return value of the given getter in the given object.
      • getBoolean

        public static boolean getBoolean​(java.lang.Object target,
                                         java.lang.reflect.Method getter)
        Return the return value of the given getter in the given object.
      • getByte

        public static byte getByte​(java.lang.Object target,
                                   java.lang.reflect.Method getter)
        Return the return value of the given getter in the given object.
      • getChar

        public static char getChar​(java.lang.Object target,
                                   java.lang.reflect.Method getter)
        Return the return value of the given getter in the given object.
      • getDouble

        public static double getDouble​(java.lang.Object target,
                                       java.lang.reflect.Method getter)
        Return the return value of the given getter in the given object.
      • getFloat

        public static float getFloat​(java.lang.Object target,
                                     java.lang.reflect.Method getter)
        Return the return value of the given getter in the given object.
      • getInt

        public static int getInt​(java.lang.Object target,
                                 java.lang.reflect.Method getter)
        Return the return value of the given getter in the given object.
      • getLong

        public static long getLong​(java.lang.Object target,
                                   java.lang.reflect.Method getter)
        Return the return value of the given getter in the given object.
      • getShort

        public static short getShort​(java.lang.Object target,
                                     java.lang.reflect.Method getter)
        Return the return value of the given getter in the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Field field,
                               java.lang.Object value)
        Set the value of the given field in the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Field field,
                               boolean value)
        Set the value of the given field in the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Field field,
                               byte value)
        Set the value of the given field in the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Field field,
                               char value)
        Set the value of the given field in the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Field field,
                               double value)
        Set the value of the given field in the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Field field,
                               float value)
        Set the value of the given field in the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Field field,
                               int value)
        Set the value of the given field in the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Field field,
                               long value)
        Set the value of the given field in the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Field field,
                               short value)
        Set the value of the given field in the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.Object value,
                               java.lang.reflect.Field field)
        Set the value of the given field in the given object. Same behavior as above methods, but parameter ordering is rearranged to simplify usage from generated bytecodes.
        Since:
        1.0.0
      • set

        public static void set​(java.lang.Object target,
                               boolean value,
                               java.lang.reflect.Field field)
        Set the value of the given field in the given object. Same behavior as above methods, but parameter ordering is rearranged to simplify usage from generated bytecodes.
        Since:
        1.0.0
      • set

        public static void set​(java.lang.Object target,
                               byte value,
                               java.lang.reflect.Field field)
        Set the value of the given field in the given object. Same behavior as above methods, but parameter ordering is rearranged to simplify usage from generated bytecodes.
        Since:
        1.0.0
      • set

        public static void set​(java.lang.Object target,
                               char value,
                               java.lang.reflect.Field field)
        Set the value of the given field in the given object. Same behavior as above methods, but parameter ordering is rearranged to simplify usage from generated bytecodes.
        Since:
        1.0.0
      • set

        public static void set​(java.lang.Object target,
                               double value,
                               java.lang.reflect.Field field)
        Set the value of the given field in the given object. Same behavior as above methods, but parameter ordering is rearranged to simplify usage from generated bytecodes.
        Since:
        1.0.0
      • set

        public static void set​(java.lang.Object target,
                               float value,
                               java.lang.reflect.Field field)
        Set the value of the given field in the given object. Same behavior as above methods, but parameter ordering is rearranged to simplify usage from generated bytecodes.
        Since:
        1.0.0
      • set

        public static void set​(java.lang.Object target,
                               int value,
                               java.lang.reflect.Field field)
        Set the value of the given field in the given object. Same behavior as above methods, but parameter ordering is rearranged to simplify usage from generated bytecodes.
        Since:
        1.0.0
      • set

        public static void set​(java.lang.Object target,
                               long value,
                               java.lang.reflect.Field field)
        Set the value of the given field in the given object. Same behavior as above methods, but parameter ordering is rearranged to simplify usage from generated bytecodes.
        Since:
        1.0.0
      • set

        public static void set​(java.lang.Object target,
                               short value,
                               java.lang.reflect.Field field)
        Set the value of the given field in the given object. Same behavior as above methods, but parameter ordering is rearranged to simplify usage from generated bytecodes.
        Since:
        1.0.0
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Method setter,
                               java.lang.Object value)
        Invoke the given setter on the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Method setter,
                               boolean value)
        Invoke the given setter on the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Method setter,
                               byte value)
        Invoke the given setter on the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Method setter,
                               char value)
        Invoke the given setter on the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Method setter,
                               double value)
        Invoke the given setter on the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Method setter,
                               float value)
        Invoke the given setter on the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Method setter,
                               int value)
        Invoke the given setter on the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Method setter,
                               long value)
        Invoke the given setter on the given object.
      • set

        public static void set​(java.lang.Object target,
                               java.lang.reflect.Method setter,
                               short value)
        Invoke the given setter on the given object.
      • getBeanStylePropertyNames

        public static java.util.Set<java.lang.String> getBeanStylePropertyNames​(java.lang.Class<?> c)
        Gets all bean-style property names of the given Class or its superclass. A bean-style property 'abc' exists in Class C iff C has declared following pair of methods: public void setAbc(Y y) or public C setAbc(Y y) public Y getAbc(); If a getter property is annotated with Reflectable, then it is ignored.
      • getPublicFieldNames

        public static java.util.Set<java.lang.String> getPublicFieldNames​(java.lang.Class c)
        Gets all public field names of the given Class.
      • getFieldValues

        public static <T> java.util.Set<T> getFieldValues​(java.lang.Class c,
                                                          int mods,
                                                          java.lang.Class<T> t)
        Gets values of all field f the given class such that f exactly match the given modifiers and are of given type (Object implies any type) unless f is annotated as Reflectable.
      • getDeclaringClass

        public static java.lang.Class getDeclaringClass​(java.lang.reflect.Method m)
        Gets the declaring class of the given method signature but also checks if the method is declared in an interface. If yes, then returns the interface.
      • getDeclaringMethod

        public static java.lang.reflect.Method getDeclaringMethod​(java.lang.Class c,
                                                                  java.lang.reflect.Method m)
        Gets the method in the given class that has the same signature of the given method, if exists. Otherwise, null.