Class BeanUtils
- Direct Known Subclasses:
LocaleBeanUtils
Utility methods for populating JavaBeans properties via reflection.
The implementations are provided by BeanUtilsBean
. These static utility methods use the default instance. More sophisticated behavior can be provided
by using a BeanUtilsBean
instance.
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic Object
Clones a bean based on the available property getters and setters, even if the bean class itself does not implement Cloneable.static void
copyProperties
(Object dest, Object orig) Copies property values from the origin bean to the destination bean for all cases where the property names are the same.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.static <K,
V> Map<K, V> Creates a cache.Return the entire set of properties for which the specified bean provides a read method.static String[]
getArrayProperty
(Object bean, String name) Return the value of the specified array property of the specified bean, as a String array.static String
getIndexedProperty
(Object bean, String name) Return the value of the specified indexed property of the specified bean, as a String.static String
getIndexedProperty
(Object bean, String name, int index) Gets the value of the specified indexed property of the specified bean, as a String.static String
getMappedProperty
(Object bean, String name) Return the value of the specified indexed property of the specified bean, as a String.static String
getMappedProperty
(Object bean, String name, String key) Return the value of the specified mapped property of the specified bean, as a String.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.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.static String
getSimpleProperty
(Object bean, String name) Return the value of the specified simple property of the specified bean, converted to a String.static void
Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs.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.
-
Constructor Details
-
BeanUtils
public BeanUtils()
-
-
Method Details
-
cloneBean
public static Object cloneBean(Object bean) throws IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException Clones 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 methodInstantiationException
- if a new instance of the bean's class cannot be instantiatedInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found- See Also:
-
copyProperties
public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException Copies 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 modifiedorig
- Origin bean whose properties are retrieved- Throws:
IllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- if thedest
ororig</code> argument is null or if the <code>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:
-
copyProperty
public static void copyProperty(Object bean, String name, Object value) throws IllegalAccessException, 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 performedname
- 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 methodInvocationTargetException
- if the property accessor method throws an exception- See Also:
-
createCache
Creates a cache.- Type Parameters:
K
- the key type of the cacheV
- the value type of the cache- Returns:
- a new cache
- Since:
- 1.8.0
-
describe
public static Map<String,String> describe(Object bean) throws IllegalAccessException, InvocationTargetException, 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:
IllegalAccessException
- if the caller does not have access to the property accessor methodInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found- See Also:
-
getArrayProperty
public static String[] getArrayProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, 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 extractedname
- 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 methodInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found- See Also:
-
getIndexedProperty
public static String getIndexedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, 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 extractedname
-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 methodInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found- See Also:
-
getIndexedProperty
public static String getIndexedProperty(Object bean, String name, int index) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Gets 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 expressionFor more details see
BeanUtilsBean
.- Parameters:
bean
- Bean whose property is to be extractedname
- Simple property name of the property value to be extractedindex
- 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 methodInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found- See Also:
-
getMappedProperty
public static String getMappedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, 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 extractedname
-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 methodInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found- See Also:
-
getMappedProperty
public static String getMappedProperty(Object bean, String name, String key) throws IllegalAccessException, InvocationTargetException, 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 extractedname
- Simple property name of the property value to be extractedkey
- 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 methodInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found- See Also:
-
getNestedProperty
public static String getNestedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, 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 extractedname
- 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 methodIllegalArgumentException
- if a nested reference to a property returns nullInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found- See Also:
-
getProperty
public static String getProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, 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 extractedname
- 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 methodInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found- See Also:
-
getSimpleProperty
public static String getSimpleProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, 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 extractedname
- 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 methodInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found- See Also:
-
populate
public static void populate(Object bean, Map<String, ? extends Object> properties) throws IllegalAccessException, InvocationTargetExceptionPopulate 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 populatedproperties
- 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 methodInvocationTargetException
- if the property accessor method throws an exception- See Also:
-
setProperty
public static void setProperty(Object bean, String name, Object value) throws IllegalAccessException, 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 performedname
- 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 methodInvocationTargetException
- if the property accessor method throws an exception- See Also:
-