|
Keel 2.1RC2 | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectorg.apache.commons.beanutils.BeanUtils
Utility methods for populating JavaBeans properties via reflection.
| Field Summary | |
private static int |
debugDeprecated. BeanUtils now uses commons-logging for all log messages. Use your favorite logging tool to configure logging for this class. |
private static FastHashMap |
dummyDummy collection from the Commons Collections API, to force a ClassNotFoundException if commons-collections.jar is not present in the runtime classpath, and this class is the first one referenced. |
private static Log |
logAll logging goes through this logger |
| Constructor Summary | |
BeanUtils() |
|
| Method Summary | |
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 java.util.Map |
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 int |
getDebug()Deprecated. BeanUtils now uses commons-logging for all log messages. Use your favorite logging tool to configure logging for this class. |
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 void |
populate(java.lang.Object bean, java.util.Map properties)Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs. |
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(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 |
| Field Detail |
private static FastHashMap dummy
private static Log log
private static int debug
| Constructor Detail |
public BeanUtils()
| Method Detail |
public static int getDebug()
public static void setDebug(int newDebug)
public static java.lang.Object cloneBean(java.lang.Object bean)
throws java.lang.IllegalAccessException,
java.lang.InstantiationException,
java.lang.reflect.InvocationTargetException,
java.lang.NoSuchMethodException
bean - Bean to be clonedjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.InstantiationException - if a new instance of the bean's class cannot be instantiatedjava.lang.reflect.InvocationTargetException - if the property accessor method throws an exceptionjava.lang.NoSuchMethodException - if an accessor method for this property cannot be found
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 each property, a conversion is attempted as necessary. All combinations of standard JavaBeans and DynaBeans as origin and destination are supported. Properties that exist in the origin bean, but do not exist in the destination bean (or are read-only in the destination bean) are silently ignored.
If the origin "bean" is actually a Map, it is assumed to contain String-valued simple property names as the keys, pointing at the corresponding property values that will be converted (if necessary) and set in the destination bean. Note that this method is intended to perform a "shallow copy" of the properties and so complex properties (for example, nested ones) will not be copied.
This method differs from populate(), which was primarily designed for populating JavaBeans from the map of request parameters retrieved on an HTTP request, is that no scalar->indexed or indexed->scalar manipulations are performed. If the origin property is indexed, the destination property must be also.
If you know that no type conversions are required, the copyProperties() method in PropertyUtils will execute faster than this method.
FIXME - Indexed and mapped properties that do not have getter and setter methods for the underlying array or Map are not copied by this method.
dest - Destination bean whose properties are modifiedorig - Origin bean whose properties are retrievedjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.IllegalArgumentException - if the dest or orig argument is nulljava.lang.reflect.InvocationTargetException - if the property accessor method throws an exceptionjava.lang.NullPointerException - if orig or dest is null
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. If the specified bean does not have a property of the specified name, or the property is read only on the destination bean, return without doing anything. If you have custom destination property types, register Converters for them by calling the register() method of ConvertUtils.
IMPLEMENTATION RESTRICTIONS:
bean - Bean on which setting is to be performedname - Property name (can be nested/indexed/mapped/combo)value - Value to be setjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
public static java.util.Map describe(java.lang.Object bean)
throws java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException,
java.lang.NoSuchMethodException
BeanUtils.populate() to reconsitute the same set of properties, modulo differences for read-only and write-only properties, but only if there are no indexed properties.
bean - Bean whose properties are to be extractedjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.reflect.InvocationTargetException - if the property accessor method throws an exceptionjava.lang.NoSuchMethodException - if an accessor method for this property cannot be found
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
bean - Bean whose property is to be extractedname - Name of the property to be extractedjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.reflect.InvocationTargetException - if the property accessor method throws an exceptionjava.lang.NoSuchMethodException - if an accessor method for this property cannot be found
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
IllegalArgumentException will be thrown.
bean - Bean whose property is to be extractedname - propertyname[index] of the property value to be extractedjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.reflect.InvocationTargetException - if the property accessor method throws an exceptionjava.lang.NoSuchMethodException - if an accessor method for this property cannot be found
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
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 extractedjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.reflect.InvocationTargetException - if the property accessor method throws an exceptionjava.lang.NoSuchMethodException - if an accessor method for this property cannot be found
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
IllegalArgumentException will be thrown.
bean - Bean whose property is to be extractedname - propertyname(index) of the property value to be extractedjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.reflect.InvocationTargetException - if the property accessor method throws an exceptionjava.lang.NoSuchMethodException - if an accessor method for this property cannot be found
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
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 extractedjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.reflect.InvocationTargetException - if the property accessor method throws an exceptionjava.lang.NoSuchMethodException - if an accessor method for this property cannot be found
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
bean - Bean whose property is to be extractedname - Possibly nested name of the property to be extractedjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.IllegalArgumentException - if a nested reference to a property returns nulljava.lang.reflect.InvocationTargetException - if the property accessor method throws an exceptionjava.lang.NoSuchMethodException - if an accessor method for this property cannot be found
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
bean - Bean whose property is to be extractedname - Possibly indexed and/or nested name of the property to be extractedjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.reflect.InvocationTargetException - if the property accessor method throws an exceptionjava.lang.NoSuchMethodException - if an accessor method for this property cannot be found
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
bean - Bean whose property is to be extractedname - Name of the property to be extractedjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.reflect.InvocationTargetException - if the property accessor method throws an exceptionjava.lang.NoSuchMethodException - if an accessor method for this property cannot be found
public static void populate(java.lang.Object bean,
java.util.Map properties)
throws java.lang.IllegalAccessException,
java.lang.reflect.InvocationTargetException
Populate the JavaBeans properties of the specified bean, based on the specified name/value pairs. This method uses Java reflection APIs to identify corresponding "property setter" method names, and deals with setter arguments of type String, boolean, int, long, float, and double. In addition, array setters for these types (or the corresponding primitive types) can also be identified.
The particular setter method to be called for each property is determined using the usual JavaBeans introspection mechanisms. Thus, you may identify custom setter methods using a BeanInfo class that is associated with the class of the bean itself. If no such BeanInfo class is available, the standard method name conversion ("set" plus the capitalized name of the property in question) is used.
NOTE: It is contrary to the JavaBeans Specification to have more than one setter method (with different argument signatures) for the same property.
WARNING - The logic of this method is customized for extracting String-based request parameters from an HTTP request. It is probably not what you want for general property copying with type conversion. For that purpose, check out the copyProperties() method instead.
bean - JavaBean whose properties are being populatedproperties - Map keyed by property name, with the corresponding (String or String[]) value(s) to be setjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
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.
If the property is read only then the method returns without throwing an exception.
If null is passed into a property expecting a primitive value, then this will be converted as if it were a null string.
WARNING - The logic of this method is customized to meet the needs of populate(), and is probably not what you want for general property copying with type conversion. For that purpose, check out the copyProperty() method instead.
WARNING - PLEASE do not modify the behavior of this method without consulting with the Struts developer community. There are some subtleties to its functionality that are not documented in the Javadoc description above, yet are vital to the way that Struts utilizes this method.
bean - Bean on which setting is to be performedname - Property name (can be nested/indexed/mapped/combo)value - Value to be setjava.lang.IllegalAccessException - if the caller does not have access to the property accessor methodjava.lang.reflect.InvocationTargetException - if the property accessor method throws an exception
|
Keel 2.1RC2 | ||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||