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.
BeanUtilsBean
Constructor and Description |
---|
BeanUtils() |
Modifier and Type | Method and Description |
---|---|
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.
|
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.
|
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> |
createCache()
Create a cache.
|
static Map<String,String> |
describe(Object bean)
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 boolean |
getCacheFast(Map<?,?> map)
Return whether a Map is fast
|
static int |
getDebug()
Deprecated.
BeanUtils now uses commons-logging for all log messages.
Use your favorite logging tool to configure logging for
this class.
|
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)
Return 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 boolean |
initCause(Throwable throwable,
Throwable cause)
If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
|
static void |
populate(Object bean,
Map<String,? extends Object> properties)
Populate the JavaBeans properties of the specified bean, based on
the specified name/value pairs.
|
static void |
setCacheFast(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.
Use your favorite logging tool to configure logging for
this class.
|
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.
|
public BeanUtils()
@Deprecated public static int getDebug()
debug
static property is no longer used@Deprecated public static void setDebug(int newDebug)
debug
static property is no longer usednewDebug
- debug propertypublic static Object cloneBean(Object bean) throws IllegalAccessException, InstantiationException, InvocationTargetException, 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
.
bean
- Bean to be clonedIllegalAccessException
- 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 foundBeanUtilsBean.cloneBean(java.lang.Object)
public static void copyProperties(Object dest, Object orig) throws IllegalAccessException, 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
.
dest
- Destination bean whose properties are modifiedorig
- Origin bean whose properties are retrievedIllegalAccessException
- if the caller does not have
access to the property accessor methodIllegalArgumentException
- 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 exceptionBeanUtilsBean.copyProperties(java.lang.Object, java.lang.Object)
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
.
bean
- Bean on which setting is to be performedname
- Property name (can be nested/indexed/mapped/combo)value
- Value to be setIllegalAccessException
- if the caller does not have
access to the property accessor methodInvocationTargetException
- if the property accessor method
throws an exceptionBeanUtilsBean.copyProperty(java.lang.Object, java.lang.String, java.lang.Object)
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
.
bean
- Bean whose properties are to be extractedIllegalAccessException
- 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 foundBeanUtilsBean.describe(java.lang.Object)
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
.
bean
- Bean whose property is to be extractedname
- Name of the property to be extractedIllegalAccessException
- 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 foundBeanUtilsBean.getArrayProperty(java.lang.Object, java.lang.String)
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
.
bean
- Bean whose property is to be extractedname
- propertyname[index]
of the property value
to be extractedIllegalAccessException
- 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 foundBeanUtilsBean.getIndexedProperty(Object, String)
public static String getIndexedProperty(Object bean, String name, int index) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
For more details see BeanUtilsBean
.
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 extractedIllegalAccessException
- 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 foundBeanUtilsBean.getIndexedProperty(Object, String, int)
public static String getMappedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
For more details see BeanUtilsBean
.
bean
- Bean whose property is to be extractedname
- propertyname(index)
of the property value
to be extractedIllegalAccessException
- 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 foundBeanUtilsBean.getMappedProperty(Object, String)
public static String getMappedProperty(Object bean, String name, String key) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
For more details see BeanUtilsBean
.
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 extractedIllegalAccessException
- 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 foundBeanUtilsBean.getMappedProperty(Object, String, String)
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
.
bean
- Bean whose property is to be extractedname
- Possibly nested name of the property to be extractedIllegalAccessException
- 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 foundBeanUtilsBean.getNestedProperty(java.lang.Object, java.lang.String)
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
.
bean
- Bean whose property is to be extractedname
- Possibly indexed and/or nested name of the property
to be extractedIllegalAccessException
- 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 foundBeanUtilsBean.getProperty(java.lang.Object, java.lang.String)
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
.
bean
- Bean whose property is to be extractedname
- Name of the property to be extractedIllegalAccessException
- 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 foundBeanUtilsBean.getSimpleProperty(java.lang.Object, java.lang.String)
public static void populate(Object bean, Map<String,? extends Object> properties) throws IllegalAccessException, InvocationTargetException
Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs.
For more details see BeanUtilsBean
.
bean
- JavaBean whose properties are being populatedproperties
- Map keyed by property name, with the
corresponding (String or String[]) value(s) to be setIllegalAccessException
- if the caller does not have
access to the property accessor methodInvocationTargetException
- if the property accessor method
throws an exceptionBeanUtilsBean.populate(java.lang.Object, java.util.Map<java.lang.String, ? extends java.lang.Object>)
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
.
bean
- Bean on which setting is to be performedname
- Property name (can be nested/indexed/mapped/combo)value
- Value to be setIllegalAccessException
- if the caller does not have
access to the property accessor methodInvocationTargetException
- if the property accessor method
throws an exceptionBeanUtilsBean.setProperty(java.lang.Object, java.lang.String, java.lang.Object)
public static boolean initCause(Throwable throwable, Throwable cause)
throwable
- The throwable.cause
- The cause of the throwable.public static <K,V> Map<K,V> createCache()
K
- the key type of the cacheV
- the value type of the cachepublic static boolean getCacheFast(Map<?,?> map)
map
- The mappublic static void setCacheFast(Map<?,?> map, boolean fast)
map
- The mapfast
- Whether it should be fast or not.Copyright © 2000–2019 The Apache Software Foundation. All rights reserved.