Class BeanUtils


  • public class BeanUtils
    extends java.lang.Object
    BeanUtils provides methods that allow simple, reflective access to JavaBeans style properties. Methods are provided for all simple types as well as object types.

    Created: 2008. 04. 22 PM 3:47:15

    • Constructor Summary

      Constructors 
      Constructor Description
      BeanUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Class<?> getClassPropertyTypeForGetter​(java.lang.Class<?> type, java.lang.String name)
      Returns the class that the getter will return when reading a property value.
      static java.lang.Class<?> getClassPropertyTypeForSetter​(java.lang.Class<?> type, java.lang.String name)
      Returns the class that the setter expects to receive as a parameter when setting a property value.
      static java.lang.Object getIndexedProperty​(java.lang.Object bean, java.lang.String indexedName)  
      static java.lang.Class<?> getIndexedType​(java.lang.Object bean, java.lang.String indexedName)  
      static java.lang.Object getProperty​(java.lang.Class<?> beanClass, java.lang.String name)
      Invokes the static method of the specified class to get the bean property value.
      static java.lang.Object getProperty​(java.lang.Object bean, java.lang.String name)
      Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
      static java.lang.Class<?> getPropertyTypeForGetter​(java.lang.Object bean, java.lang.String name)
      Returns the class that the getter will return when reading a property value.
      static java.lang.Class<?> getPropertyTypeForSetter​(java.lang.Object bean, java.lang.String name)
      Returns the class that the setter expects to receive as a parameter when setting a property value.
      static java.lang.String[] getReadablePropertyNames​(java.lang.Object bean)
      Returns an array of the readable properties exposed by a bean.
      static java.lang.String[] getReadablePropertyNamesWithoutNonSerializable​(java.lang.Object bean)
      Returns an array of readable properties exposed by the bean, except those specified by NonSerializable.
      static java.lang.Object getSimpleProperty​(java.lang.Object bean, java.lang.String name)
      Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
      static java.lang.String[] getWritablePropertyNames​(java.lang.Object bean)
      Returns an array of the writable properties exposed by a bean.
      static boolean hasReadableProperty​(java.lang.Class<?> beanClass, java.lang.String name)  
      static boolean hasReadableProperty​(java.lang.Object bean, java.lang.String name)
      Checks to see if a bean has a readable property be a given name.
      static boolean hasStaticReadableProperty​(java.lang.Class<?> beanClass, java.lang.String name)  
      static boolean hasStaticWritableProperty​(java.lang.Class<?> beanClass, java.lang.String name)  
      static boolean hasWritableProperty​(java.lang.Class<?> beanClass, java.lang.String name)  
      static boolean hasWritableProperty​(java.lang.Object bean, java.lang.String name)
      Checks to see if a bean has a writable property be a given name.
      static void setIndexedProperty​(java.lang.Object bean, java.lang.String indexedName, java.lang.Object value)  
      static void setProperty​(java.lang.Class<?> beanClass, java.lang.String name, java.lang.Object value)
      Invokes the static method of the specified class to get the bean property value.
      static void setProperty​(java.lang.Object bean, java.lang.String name, java.lang.Object value)
      Sets the value of the specified property of the specified bean.
      static void setSimpleProperty​(java.lang.Object bean, java.lang.String name, java.lang.Object value)
      Sets the value of the specified property of the specified bean.
      • Methods inherited from class java.lang.Object

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

      • BeanUtils

        public BeanUtils()
    • Method Detail

      • getProperty

        public static java.lang.Object getProperty​(java.lang.Class<?> beanClass,
                                                   java.lang.String name)
                                            throws java.lang.reflect.InvocationTargetException
        Invokes the static method of the specified class to get the bean property value.
        Parameters:
        beanClass - the class for which to lookup
        name - the property name
        Returns:
        the property value (as an Object)
        Throws:
        java.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
      • setProperty

        public static void setProperty​(java.lang.Class<?> beanClass,
                                       java.lang.String name,
                                       java.lang.Object value)
                                throws java.lang.reflect.InvocationTargetException
        Invokes the static method of the specified class to get the bean property value.
        Parameters:
        beanClass - the class for which to lookup
        name - the property name
        value - the value to which this property is to be set
        Throws:
        java.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
      • hasReadableProperty

        public static boolean hasReadableProperty​(java.lang.Class<?> beanClass,
                                                  java.lang.String name)
      • hasWritableProperty

        public static boolean hasWritableProperty​(java.lang.Class<?> beanClass,
                                                  java.lang.String name)
      • hasStaticReadableProperty

        public static boolean hasStaticReadableProperty​(java.lang.Class<?> beanClass,
                                                        java.lang.String name)
      • hasStaticWritableProperty

        public static boolean hasStaticWritableProperty​(java.lang.Class<?> beanClass,
                                                        java.lang.String name)
      • getProperty

        public static java.lang.Object getProperty​(java.lang.Object bean,
                                                   java.lang.String name)
                                            throws java.lang.reflect.InvocationTargetException
        Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
        Parameters:
        bean - the bean whose property is to be extracted
        name - possibly indexed and/or nested name of the property to be extracted
        Returns:
        the property value (as an Object)
        Throws:
        java.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
      • getSimpleProperty

        public static java.lang.Object getSimpleProperty​(java.lang.Object bean,
                                                         java.lang.String name)
                                                  throws java.lang.reflect.InvocationTargetException
        Return the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.
        Parameters:
        bean - the bean whose property is to be extracted
        name - the name of the property to be extracted that does not allow nesting
        Returns:
        the property value (as an Object)
        Throws:
        java.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
      • setProperty

        public static void setProperty​(java.lang.Object bean,
                                       java.lang.String name,
                                       java.lang.Object value)
                                throws java.lang.reflect.InvocationTargetException,
                                       java.lang.NoSuchMethodException
        Sets the value of the specified property of the specified bean.
        Parameters:
        bean - the bean whose property is to be modified
        name - possibly indexed and/or nested name of the property to be modified
        value - the value to which this property is to be set
        Throws:
        java.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
        java.lang.NoSuchMethodException - if an accessor method for this property cannot be found
      • setSimpleProperty

        public static void setSimpleProperty​(java.lang.Object bean,
                                             java.lang.String name,
                                             java.lang.Object value)
                                      throws java.lang.reflect.InvocationTargetException
        Sets the value of the specified property of the specified bean.
        Parameters:
        bean - the bean whose property is to be modified
        name - the name of the property to be modified that does not allow nesting
        value - the value to which this property is to be set
        Throws:
        java.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
      • getIndexedProperty

        public static java.lang.Object getIndexedProperty​(java.lang.Object bean,
                                                          java.lang.String indexedName)
                                                   throws java.lang.reflect.InvocationTargetException
        Throws:
        java.lang.reflect.InvocationTargetException
      • getIndexedType

        public static java.lang.Class<?> getIndexedType​(java.lang.Object bean,
                                                        java.lang.String indexedName)
                                                 throws java.lang.reflect.InvocationTargetException
        Throws:
        java.lang.reflect.InvocationTargetException
      • setIndexedProperty

        public static void setIndexedProperty​(java.lang.Object bean,
                                              java.lang.String indexedName,
                                              java.lang.Object value)
                                       throws java.lang.reflect.InvocationTargetException
        Throws:
        java.lang.reflect.InvocationTargetException
      • hasWritableProperty

        public static boolean hasWritableProperty​(java.lang.Object bean,
                                                  java.lang.String name)
                                           throws java.lang.NoSuchMethodException
        Checks to see if a bean has a writable property be a given name.
        Parameters:
        bean - the bean to check
        name - the property name to check for
        Returns:
        true if the property exists and is writable
        Throws:
        java.lang.NoSuchMethodException - if an accessor method for this property cannot be found
      • hasReadableProperty

        public static boolean hasReadableProperty​(java.lang.Object bean,
                                                  java.lang.String name)
                                           throws java.lang.NoSuchMethodException
        Checks to see if a bean has a readable property be a given name.
        Parameters:
        bean - the bean to check
        name - the property name to check for
        Returns:
        true if the property exists and is readable
        Throws:
        java.lang.NoSuchMethodException - if an accessor method for this property cannot be found
      • getPropertyTypeForSetter

        public static java.lang.Class<?> getPropertyTypeForSetter​(java.lang.Object bean,
                                                                  java.lang.String name)
                                                           throws java.lang.NoSuchMethodException
        Returns the class that the setter expects to receive as a parameter when setting a property value.
        Parameters:
        bean - the bean to check
        name - the name of the property
        Returns:
        the type of the property
        Throws:
        java.lang.NoSuchMethodException - if an accessor method for this property cannot be found
      • getPropertyTypeForGetter

        public static java.lang.Class<?> getPropertyTypeForGetter​(java.lang.Object bean,
                                                                  java.lang.String name)
                                                           throws java.lang.NoSuchMethodException
        Returns the class that the getter will return when reading a property value.
        Parameters:
        bean - the bean to check
        name - the name of the property
        Returns:
        the type of the property
        Throws:
        java.lang.NoSuchMethodException - if an accessor method for this property cannot be found
      • getClassPropertyTypeForGetter

        public static java.lang.Class<?> getClassPropertyTypeForGetter​(java.lang.Class<?> type,
                                                                       java.lang.String name)
                                                                throws java.lang.NoSuchMethodException
        Returns the class that the getter will return when reading a property value.
        Parameters:
        type - the class to check
        name - the name of the property
        Returns:
        the type of the property
        Throws:
        java.lang.NoSuchMethodException - if an accessor method for this property cannot be found
      • getClassPropertyTypeForSetter

        public static java.lang.Class<?> getClassPropertyTypeForSetter​(java.lang.Class<?> type,
                                                                       java.lang.String name)
                                                                throws java.lang.NoSuchMethodException
        Returns the class that the setter expects to receive as a parameter when setting a property value.
        Parameters:
        type - The class to check
        name - the name of the property
        Returns:
        the type of the property
        Throws:
        java.lang.NoSuchMethodException - if an accessor method for this property cannot be found
      • getReadablePropertyNames

        public static java.lang.String[] getReadablePropertyNames​(java.lang.Object bean)
        Returns an array of the readable properties exposed by a bean.
        Parameters:
        bean - the bean
        Returns:
        the readable properties
      • getReadablePropertyNamesWithoutNonSerializable

        public static java.lang.String[] getReadablePropertyNamesWithoutNonSerializable​(java.lang.Object bean)
        Returns an array of readable properties exposed by the bean, except those specified by NonSerializable.
        Parameters:
        bean - the bean
        Returns:
        the readable properties without non serializable
      • getWritablePropertyNames

        public static java.lang.String[] getWritablePropertyNames​(java.lang.Object bean)
        Returns an array of the writable properties exposed by a bean.
        Parameters:
        bean - the bean
        Returns:
        the properties