org.apache.commons.beanutils

Class BeanUtils

public class BeanUtils extends 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: $Revision: 690380 $ $Date: 2008-08-29 21:04:38 +0100 (Fri, 29 Aug 2008) $

Author: Craig R. McClanahan Ralph Schaer Chris Audley Rey Francois Gregor Rayman

See Also: BeanUtilsBean

Method Summary
static ObjectcloneBean(Object bean)

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.

static voidcopyProperties(Object dest, Object orig)

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.

static voidcopyProperty(Object bean, String name, Object value)

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

For more details see BeanUtilsBean.

static MapcreateCache()
Create a cache.
static Mapdescribe(Object bean)

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

For more details see BeanUtilsBean.

static String[]getArrayProperty(Object bean, String name)

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

For more details see BeanUtilsBean.

static booleangetCacheFast(Map map)
Return whether a Map is fast
static intgetDebug()
The debug static property is no longer used
static StringgetIndexedProperty(Object bean, String name)

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

For more details see BeanUtilsBean.

static StringgetIndexedProperty(Object bean, String name, int index)
Return the value of the specified indexed property of the specified bean, as a String.
static StringgetMappedProperty(Object bean, String name)

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

For more details see BeanUtilsBean.

static StringgetMappedProperty(Object bean, String name, String key)

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

For more details see BeanUtilsBean.

static StringgetNestedProperty(Object bean, String name)

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

For more details see BeanUtilsBean.

static StringgetProperty(Object bean, String name)

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.

static StringgetSimpleProperty(Object bean, String name)

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

For more details see BeanUtilsBean.

static booleaninitCause(Throwable throwable, Throwable cause)
If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
static voidpopulate(Object bean, Map properties)

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

For more details see BeanUtilsBean.

static voidsetCacheFast(Map map, boolean fast)
Set whether fast on a Map
static voidsetDebug(int newDebug)
The debug static property is no longer used
static voidsetProperty(Object bean, String name, Object value)

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

For more details see BeanUtilsBean.

Method Detail

cloneBean

public static Object cloneBean(Object bean)

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: IllegalAccessException if the caller does not have access to the property accessor method InstantiationException if a new instance of the bean's class cannot be instantiated InvocationTargetException if the property accessor method throws an exception NoSuchMethodException if an accessor method for this property cannot be found

See Also: BeanUtilsBean

copyProperties

public static void copyProperties(Object dest, Object orig)

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: IllegalAccessException if the caller does not have access to the property accessor method 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. InvocationTargetException if the property accessor method throws an exception

See Also: BeanUtilsBean

copyProperty

public static void copyProperty(Object bean, String name, Object value)

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: IllegalAccessException if the caller does not have access to the property accessor method InvocationTargetException if the property accessor method throws an exception

See Also: BeanUtilsBean

createCache

public static Map createCache()
Create a cache.

Returns: a new cache

Since: 1.8.0

describe

public static Map describe(Object bean)

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: IllegalAccessException if the caller does not have access to the property accessor method InvocationTargetException if the property accessor method throws an exception NoSuchMethodException if an accessor method for this property cannot be found

See Also: BeanUtilsBean

getArrayProperty

public static String[] getArrayProperty(Object bean, String name)

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: IllegalAccessException if the caller does not have access to the property accessor method InvocationTargetException if the property accessor method throws an exception NoSuchMethodException if an accessor method for this property cannot be found

See Also: BeanUtilsBean

getCacheFast

public static boolean getCacheFast(Map map)
Return whether a Map is fast

Parameters: map The map

Returns: Whether it is fast or not.

Since: 1.8.0

getDebug

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

getIndexedProperty

public static String getIndexedProperty(Object bean, String name)

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: IllegalAccessException if the caller does not have access to the property accessor method InvocationTargetException if the property accessor method throws an exception NoSuchMethodException if an accessor method for this property cannot be found

See Also: BeanUtilsBean

getIndexedProperty

public static String getIndexedProperty(Object bean, String name, int index)
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: IllegalAccessException if the caller does not have access to the property accessor method InvocationTargetException if the property accessor method throws an exception NoSuchMethodException if an accessor method for this property cannot be found

See Also: BeanUtilsBean

getMappedProperty

public static String getMappedProperty(Object bean, String name)

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: IllegalAccessException if the caller does not have access to the property accessor method InvocationTargetException if the property accessor method throws an exception NoSuchMethodException if an accessor method for this property cannot be found

See Also: BeanUtilsBean

getMappedProperty

public static String getMappedProperty(Object bean, String name, String key)

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: IllegalAccessException if the caller does not have access to the property accessor method InvocationTargetException if the property accessor method throws an exception NoSuchMethodException if an accessor method for this property cannot be found

See Also: BeanUtilsBean

getNestedProperty

public static String getNestedProperty(Object bean, String name)

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: IllegalAccessException if the caller does not have access to the property accessor method IllegalArgumentException if a nested reference to a property returns null InvocationTargetException if the property accessor method throws an exception NoSuchMethodException if an accessor method for this property cannot be found

See Also: BeanUtilsBean

getProperty

public static String getProperty(Object bean, String name)

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: IllegalAccessException if the caller does not have access to the property accessor method InvocationTargetException if the property accessor method throws an exception NoSuchMethodException if an accessor method for this property cannot be found

See Also: BeanUtilsBean

getSimpleProperty

public static String getSimpleProperty(Object bean, String name)

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: IllegalAccessException if the caller does not have access to the property accessor method InvocationTargetException if the property accessor method throws an exception NoSuchMethodException if an accessor method for this property cannot be found

See Also: BeanUtilsBean

initCause

public static boolean initCause(Throwable throwable, 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

populate

public static void populate(Object bean, Map properties)

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: IllegalAccessException if the caller does not have access to the property accessor method InvocationTargetException if the property accessor method throws an exception

See Also: BeanUtilsBean

setCacheFast

public static void setCacheFast(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

setDebug

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

setProperty

public static void setProperty(Object bean, String name, Object value)

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: IllegalAccessException if the caller does not have access to the property accessor method InvocationTargetException if the property accessor method throws an exception

See Also: BeanUtilsBean

Copyright © 2000-2010 Apache Software Foundation. All Rights Reserved.