Class BeanUtils

  • Direct Known Subclasses:
    LocaleBeanUtils

    public class BeanUtils
    extends java.lang.Object

    Utility methods for populating JavaBeans properties via reflection.

    The implementations are provided by BeanUtilsBean. These static utility methods use the default instance. More sophisticated behaviour can be provided by using a BeanUtilsBean instance.

    Version:
    $Id$
    See Also:
    BeanUtilsBean
    • Constructor Summary

      Constructors 
      Constructor Description
      BeanUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static java.lang.Object cloneBean​(java.lang.Object bean)
      Clone a bean based on the available property getters and setters, even if the bean class itself does not implement Cloneable.
      static void copyProperties​(java.lang.Object dest, java.lang.Object orig)
      Copy property values from the origin bean to the destination bean for all cases where the property names are the same.
      static void copyProperty​(java.lang.Object bean, java.lang.String name, java.lang.Object value)
      Copy the specified property value to the specified destination bean, performing any type conversion that is required.
      static <K,​V>
      java.util.Map<K,​V>
      createCache()
      Create a cache.
      static java.util.Map<java.lang.String,​java.lang.String> describe​(java.lang.Object bean)
      Return the entire set of properties for which the specified bean provides a read method.
      static java.lang.String[] getArrayProperty​(java.lang.Object bean, java.lang.String name)
      Return the value of the specified array property of the specified bean, as a String array.
      static boolean getCacheFast​(java.util.Map<?,​?> map)
      Return whether a Map is fast
      static int getDebug()
      Deprecated.
      BeanUtils now uses commons-logging for all log messages.
      static java.lang.String getIndexedProperty​(java.lang.Object bean, java.lang.String name)
      Return the value of the specified indexed property of the specified bean, as a String.
      static java.lang.String getIndexedProperty​(java.lang.Object bean, java.lang.String name, int index)
      Return the value of the specified indexed property of the specified bean, as a String.
      static java.lang.String getMappedProperty​(java.lang.Object bean, java.lang.String name)
      Return the value of the specified indexed property of the specified bean, as a String.
      static java.lang.String getMappedProperty​(java.lang.Object bean, java.lang.String name, java.lang.String key)
      Return the value of the specified mapped property of the specified bean, as a String.
      static java.lang.String getNestedProperty​(java.lang.Object bean, java.lang.String name)
      Return the value of the (possibly nested) property of the specified name, for the specified bean, as a String.
      static java.lang.String 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, as a String.
      static java.lang.String getSimpleProperty​(java.lang.Object bean, java.lang.String name)
      Return the value of the specified simple property of the specified bean, converted to a String.
      static boolean initCause​(java.lang.Throwable throwable, java.lang.Throwable cause)
      If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
      static void populate​(java.lang.Object bean, java.util.Map<java.lang.String,​? extends java.lang.Object> properties)
      Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs.
      static void setCacheFast​(java.util.Map<?,​?> map, boolean fast)
      Set whether fast on a Map
      static void setDebug​(int newDebug)
      Deprecated.
      BeanUtils now uses commons-logging for all log messages.
      static void setProperty​(java.lang.Object bean, java.lang.String name, java.lang.Object value)
      Set the specified property value, performing type conversions as required to conform to the type of the destination property.
      • 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

      • getDebug

        @Deprecated
        public static int getDebug()
        Deprecated.
        BeanUtils now uses commons-logging for all log messages. Use your favorite logging tool to configure logging for this class.
        The debug static property is no longer used
        Returns:
        debug property
      • setDebug

        @Deprecated
        public static void setDebug​(int newDebug)
        Deprecated.
        BeanUtils now uses commons-logging for all log messages. Use your favorite logging tool to configure logging for this class.
        The debug static property is no longer used
        Parameters:
        newDebug - debug property
      • cloneBean

        public static java.lang.Object cloneBean​(java.lang.Object bean)
                                          throws java.lang.IllegalAccessException,
                                                 java.lang.InstantiationException,
                                                 java.lang.reflect.InvocationTargetException,
                                                 java.lang.NoSuchMethodException

        Clone a bean based on the available property getters and setters, even if the bean class itself does not implement Cloneable.

        For more details see BeanUtilsBean.

        Parameters:
        bean - Bean to be cloned
        Returns:
        the cloned bean
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        java.lang.InstantiationException - if a new instance of the bean's class cannot be instantiated
        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
        See Also:
        BeanUtilsBean.cloneBean(java.lang.Object)
      • copyProperties

        public static void copyProperties​(java.lang.Object dest,
                                          java.lang.Object orig)
                                   throws java.lang.IllegalAccessException,
                                          java.lang.reflect.InvocationTargetException

        Copy property values from the origin bean to the destination bean for all cases where the property names are the same.

        For more details see BeanUtilsBean.

        Parameters:
        dest - Destination bean whose properties are modified
        orig - Origin bean whose properties are retrieved
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        java.lang.IllegalArgumentException - if the dest or orig argument is null or if the dest property type is different from the source type and the relevant converter has not been registered.
        java.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
        See Also:
        BeanUtilsBean.copyProperties(java.lang.Object, java.lang.Object)
      • copyProperty

        public static void copyProperty​(java.lang.Object bean,
                                        java.lang.String name,
                                        java.lang.Object value)
                                 throws java.lang.IllegalAccessException,
                                        java.lang.reflect.InvocationTargetException

        Copy the specified property value to the specified destination bean, performing any type conversion that is required.

        For more details see BeanUtilsBean.

        Parameters:
        bean - Bean on which setting is to be performed
        name - Property name (can be nested/indexed/mapped/combo)
        value - Value to be set
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        java.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
        See Also:
        BeanUtilsBean.copyProperty(java.lang.Object, java.lang.String, java.lang.Object)
      • describe

        public static java.util.Map<java.lang.String,​java.lang.String> describe​(java.lang.Object bean)
                                                                               throws java.lang.IllegalAccessException,
                                                                                      java.lang.reflect.InvocationTargetException,
                                                                                      java.lang.NoSuchMethodException

        Return the entire set of properties for which the specified bean provides a read method.

        For more details see BeanUtilsBean.

        Parameters:
        bean - Bean whose properties are to be extracted
        Returns:
        Map of property descriptors
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        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
        See Also:
        BeanUtilsBean.describe(java.lang.Object)
      • getArrayProperty

        public static java.lang.String[] getArrayProperty​(java.lang.Object bean,
                                                          java.lang.String name)
                                                   throws java.lang.IllegalAccessException,
                                                          java.lang.reflect.InvocationTargetException,
                                                          java.lang.NoSuchMethodException

        Return the value of the specified array property of the specified bean, as a String array.

        For more details see BeanUtilsBean.

        Parameters:
        bean - Bean whose property is to be extracted
        name - Name of the property to be extracted
        Returns:
        The array property value
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        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
        See Also:
        BeanUtilsBean.getArrayProperty(java.lang.Object, java.lang.String)
      • getIndexedProperty

        public static java.lang.String getIndexedProperty​(java.lang.Object bean,
                                                          java.lang.String name)
                                                   throws java.lang.IllegalAccessException,
                                                          java.lang.reflect.InvocationTargetException,
                                                          java.lang.NoSuchMethodException

        Return the value of the specified indexed property of the specified bean, as a String.

        For more details see BeanUtilsBean.

        Parameters:
        bean - Bean whose property is to be extracted
        name - propertyname[index] of the property value to be extracted
        Returns:
        The indexed property's value, converted to a String
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        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
        See Also:
        BeanUtilsBean.getIndexedProperty(Object, String)
      • getIndexedProperty

        public static java.lang.String getIndexedProperty​(java.lang.Object bean,
                                                          java.lang.String name,
                                                          int index)
                                                   throws java.lang.IllegalAccessException,
                                                          java.lang.reflect.InvocationTargetException,
                                                          java.lang.NoSuchMethodException
        Return the value of the specified indexed property of the specified bean, as a String. The index is specified as a method parameter and must *not* be included in the property name expression

        For more details see BeanUtilsBean.

        Parameters:
        bean - Bean whose property is to be extracted
        name - Simple property name of the property value to be extracted
        index - Index of the property value to be extracted
        Returns:
        The indexed property's value, converted to a String
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        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
        See Also:
        BeanUtilsBean.getIndexedProperty(Object, String, int)
      • getMappedProperty

        public static java.lang.String getMappedProperty​(java.lang.Object bean,
                                                         java.lang.String name)
                                                  throws java.lang.IllegalAccessException,
                                                         java.lang.reflect.InvocationTargetException,
                                                         java.lang.NoSuchMethodException

        Return the value of the specified indexed property of the specified bean, as a String.

        For more details see BeanUtilsBean.

        Parameters:
        bean - Bean whose property is to be extracted
        name - propertyname(index) of the property value to be extracted
        Returns:
        The mapped property's value, converted to a String
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        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
        See Also:
        BeanUtilsBean.getMappedProperty(Object, String)
      • getMappedProperty

        public static java.lang.String getMappedProperty​(java.lang.Object bean,
                                                         java.lang.String name,
                                                         java.lang.String key)
                                                  throws java.lang.IllegalAccessException,
                                                         java.lang.reflect.InvocationTargetException,
                                                         java.lang.NoSuchMethodException

        Return the value of the specified mapped property of the specified bean, as a String.

        For more details see BeanUtilsBean.

        Parameters:
        bean - Bean whose property is to be extracted
        name - Simple property name of the property value to be extracted
        key - Lookup key of the property value to be extracted
        Returns:
        The mapped property's value, converted to a String
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        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
        See Also:
        BeanUtilsBean.getMappedProperty(Object, String, String)
      • getNestedProperty

        public static java.lang.String getNestedProperty​(java.lang.Object bean,
                                                         java.lang.String name)
                                                  throws java.lang.IllegalAccessException,
                                                         java.lang.reflect.InvocationTargetException,
                                                         java.lang.NoSuchMethodException

        Return the value of the (possibly nested) property of the specified name, for the specified bean, as a String.

        For more details see BeanUtilsBean.

        Parameters:
        bean - Bean whose property is to be extracted
        name - Possibly nested name of the property to be extracted
        Returns:
        The nested property's value, converted to a String
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        java.lang.IllegalArgumentException - if a nested reference to a property returns null
        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
        See Also:
        BeanUtilsBean.getNestedProperty(java.lang.Object, java.lang.String)
      • getProperty

        public static java.lang.String getProperty​(java.lang.Object bean,
                                                   java.lang.String name)
                                            throws java.lang.IllegalAccessException,
                                                   java.lang.reflect.InvocationTargetException,
                                                   java.lang.NoSuchMethodException

        Return the value of the specified property of the specified bean, no matter which property reference format is used, as a String.

        For more details see BeanUtilsBean.

        Parameters:
        bean - Bean whose property is to be extracted
        name - Possibly indexed and/or nested name of the property to be extracted
        Returns:
        The property's value, converted to a String
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        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
        See Also:
        BeanUtilsBean.getProperty(java.lang.Object, java.lang.String)
      • getSimpleProperty

        public static java.lang.String getSimpleProperty​(java.lang.Object bean,
                                                         java.lang.String name)
                                                  throws java.lang.IllegalAccessException,
                                                         java.lang.reflect.InvocationTargetException,
                                                         java.lang.NoSuchMethodException

        Return the value of the specified simple property of the specified bean, converted to a String.

        For more details see BeanUtilsBean.

        Parameters:
        bean - Bean whose property is to be extracted
        name - Name of the property to be extracted
        Returns:
        The property's value, converted to a String
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        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
        See Also:
        BeanUtilsBean.getSimpleProperty(java.lang.Object, java.lang.String)
      • populate

        public static void populate​(java.lang.Object bean,
                                    java.util.Map<java.lang.String,​? extends java.lang.Object> properties)
                             throws java.lang.IllegalAccessException,
                                    java.lang.reflect.InvocationTargetException

        Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs.

        For more details see BeanUtilsBean.

        Parameters:
        bean - JavaBean whose properties are being populated
        properties - Map keyed by property name, with the corresponding (String or String[]) value(s) to be set
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        java.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
        See Also:
        BeanUtilsBean.populate(java.lang.Object, java.util.Map<java.lang.String, ? extends java.lang.Object>)
      • setProperty

        public static void setProperty​(java.lang.Object bean,
                                       java.lang.String name,
                                       java.lang.Object value)
                                throws java.lang.IllegalAccessException,
                                       java.lang.reflect.InvocationTargetException

        Set the specified property value, performing type conversions as required to conform to the type of the destination property.

        For more details see BeanUtilsBean.

        Parameters:
        bean - Bean on which setting is to be performed
        name - Property name (can be nested/indexed/mapped/combo)
        value - Value to be set
        Throws:
        java.lang.IllegalAccessException - if the caller does not have access to the property accessor method
        java.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
        See Also:
        BeanUtilsBean.setProperty(java.lang.Object, java.lang.String, java.lang.Object)
      • initCause

        public static boolean initCause​(java.lang.Throwable throwable,
                                        java.lang.Throwable cause)
        If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
        Parameters:
        throwable - The throwable.
        cause - The cause of the throwable.
        Returns:
        true if the cause was initialized, otherwise false.
        Since:
        1.8.0
      • createCache

        public static <K,​V> java.util.Map<K,​V> createCache()
        Create a cache.
        Type Parameters:
        K - the key type of the cache
        V - the value type of the cache
        Returns:
        a new cache
        Since:
        1.8.0
      • getCacheFast

        public static boolean getCacheFast​(java.util.Map<?,​?> map)
        Return whether a Map is fast
        Parameters:
        map - The map
        Returns:
        Whether it is fast or not.
        Since:
        1.8.0
      • setCacheFast

        public static void setCacheFast​(java.util.Map<?,​?> map,
                                        boolean fast)
        Set whether fast on a Map
        Parameters:
        map - The map
        fast - Whether it should be fast or not.
        Since:
        1.8.0