Class ReflectionUtils


  • public class ReflectionUtils
    extends java.lang.Object
    Reflection utility class. Ikasan Development Team.
    • Constructor Summary

      Constructors 
      Constructor Description
      ReflectionUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      protected static java.util.Map<java.lang.String,​java.lang.reflect.Field> getDeclaredFields​(java.lang.Class cls)
      Get all fields of the given class and all parent classes regardless of access (public, protected, private, ..).
      static java.lang.reflect.Method getGetterMethod​(java.lang.Class cls, java.lang.reflect.Field field)
      Return a getter method instance in the given class for the specified field.
      static java.util.Map<java.lang.String,​java.lang.Object> getPropertiesIgnoringExceptions​(java.lang.Object target)
      Get all non-static field names and values for the given target object.
      static java.lang.Object getProperty​(java.lang.Object target, java.lang.String fieldName)
      Get the value from the target object for the given field name.
      static java.lang.Class getPropertyType​(java.lang.Object target, java.lang.String fieldName)
      Get the type for the given field name in the given target object.
      static java.lang.reflect.Method getSetterMethod​(java.lang.Class cls, java.lang.reflect.Field field)
      Return a setter method instance for the given class for the specified field.
      static void setProperty​(java.lang.Object target, java.lang.String fieldName, java.lang.Object value)
      Set property on target object based on the field name and given value.
      • Methods inherited from class java.lang.Object

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

      • ReflectionUtils

        public ReflectionUtils()
    • Method Detail

      • getSetterMethod

        public static java.lang.reflect.Method getSetterMethod​(java.lang.Class cls,
                                                               java.lang.reflect.Field field)
                                                        throws java.lang.NoSuchMethodException
        Return a setter method instance for the given class for the specified field. NOTE if no valid method is found, including adherence to the JavaBean standards, then throw a NoSuchMethodException.
        Parameters:
        cls -
        field -
        Returns:
        Throws:
        java.lang.NoSuchMethodException
      • getProperty

        public static java.lang.Object getProperty​(java.lang.Object target,
                                                   java.lang.String fieldName)
                                            throws java.lang.NoSuchFieldException,
                                                   java.lang.NoSuchMethodException,
                                                   java.lang.reflect.InvocationTargetException,
                                                   java.lang.IllegalAccessException
        Get the value from the target object for the given field name.
        Parameters:
        target -
        fieldName -
        Returns:
        Throws:
        java.lang.NoSuchFieldException
        java.lang.NoSuchMethodException
        java.lang.reflect.InvocationTargetException
        java.lang.IllegalAccessException
      • getPropertyType

        public static java.lang.Class getPropertyType​(java.lang.Object target,
                                                      java.lang.String fieldName)
                                               throws java.lang.NoSuchFieldException,
                                                      java.lang.NoSuchMethodException,
                                                      java.lang.reflect.InvocationTargetException,
                                                      java.lang.IllegalAccessException
        Get the type for the given field name in the given target object.
        Parameters:
        target -
        fieldName -
        Returns:
        Throws:
        java.lang.NoSuchFieldException
        java.lang.NoSuchMethodException
        java.lang.reflect.InvocationTargetException
        java.lang.IllegalAccessException
      • getPropertiesIgnoringExceptions

        public static java.util.Map<java.lang.String,​java.lang.Object> getPropertiesIgnoringExceptions​(java.lang.Object target)
        Get all non-static field names and values for the given target object. Any failures to access fields or methods are logged, but not thrown.
        Parameters:
        target -
        Returns:
      • setProperty

        public static void setProperty​(java.lang.Object target,
                                       java.lang.String fieldName,
                                       java.lang.Object value)
                                throws java.lang.NoSuchFieldException,
                                       java.lang.NoSuchMethodException,
                                       java.lang.reflect.InvocationTargetException,
                                       java.lang.IllegalAccessException
        Set property on target object based on the field name and given value.
        Parameters:
        target -
        fieldName -
        value -
        Throws:
        java.lang.NoSuchFieldException
        java.lang.NoSuchMethodException
        java.lang.reflect.InvocationTargetException
        java.lang.IllegalAccessException
      • getGetterMethod

        public static java.lang.reflect.Method getGetterMethod​(java.lang.Class cls,
                                                               java.lang.reflect.Field field)
                                                        throws java.lang.NoSuchMethodException
        Return a getter method instance in the given class for the specified field. NOTE if no valid method is found, including adherence to the JavaBean standards, then throw a NoSuchMethodException.
        Parameters:
        cls -
        field -
        Returns:
        Throws:
        java.lang.NoSuchMethodException
      • getDeclaredFields

        protected static java.util.Map<java.lang.String,​java.lang.reflect.Field> getDeclaredFields​(java.lang.Class cls)
        Get all fields of the given class and all parent classes regardless of access (public, protected, private, ..).
        Parameters:
        cls -
        Returns:
        Map