Class PropertyUtilsBean
BeanUtils
, but has been separated because of the volume of code involved.
In general, the objects that are examined and modified using these methods are expected to conform to the property getter and setter method naming
conventions described in the JavaBeans Specification (Version 1.0.1). No data type conversions are performed, and there are no usage of any
PropertyEditor
classes that have been registered, although a convenient way to access the registered classes themselves is included.
For the purposes of this class, five formats for referencing a particular property value of a bean are defined, with the default layout of an
identifying String in parentheses. However the notation for these formats and how they are resolved is now (since BeanUtils 1.8.0) controlled by the
configured Resolver
implementation:
- Simple (
name
) - The specifiedname
identifies an individual property of a particular JavaBean. The name of the actual getter or setter method to be used is determined using standard JavaBeans introspection, so that (unless overridden by aBeanInfo
class, a property named "xyz" will have a getter method namedgetXyz()
or (for boolean properties only)isXyz()
, and a setter method namedsetXyz()
. - Nested (
name1.name2.name3
) The first name element is used to select a property getter, as for simple references above. The object returned for this property is then consulted, using the same approach, for a property getter for a property namedname2
, and so on. The property value that is ultimately retrieved or modified is the one identified by the last name element. - Indexed (
name[index]
) - The underlying property value is assumed to be an array, or this JavaBean is assumed to have indexed property getter and setter methods. The appropriate (zero-relative) entry in the array is selected.List
objects are now also supported for read/write. You simply need to define a getter that returns theList
- Mapped (
name(key)
) - The JavaBean is assumed to have an property getter and setter methods with an additional attribute of typeString
. - Combined (
name1.name2[index].name3(key)
) - Combining mapped, nested, and indexed references is also supported.
- Since:
- 1.7
- See Also:
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addBeanIntrospector
(BeanIntrospector introspector) Adds aBeanIntrospector
.void
Clear any cached property descriptors information for all classes loaded by any class loaders.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 (even though the actual getter and setter methods might have been customized viaBeanInfo
classes).Return the entire set of properties for which the specified bean provides a read method.getIndexedProperty
(Object bean, String name) Gets the value of the specified indexed property of the specified bean, with no type conversions.getIndexedProperty
(Object bean, String name, int index) Gets the value of the specified indexed property of the specified bean, with no type conversions.protected static PropertyUtilsBean
Gets the PropertyUtils bean instance.getMappedProperty
(Object bean, String name) Gets the value of the specified mapped property of the specified bean, with no type conversions.getMappedProperty
(Object bean, String name, String key) Gets the value of the specified mapped property of the specified bean, with no type conversions.getNestedProperty
(Object bean, String name) Gets the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.getProperty
(Object bean, String name) Gets the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.getPropertyDescriptor
(Object bean, String name) Retrieve the property descriptor for the specified property of the specified bean, or returnnull
if there is no such descriptor.getPropertyDescriptors
(Class<?> beanClass) Retrieve the property descriptors for the specified class, introspecting and caching them the first time a particular bean class is encountered.getPropertyDescriptors
(Object bean) Retrieve the property descriptors for the specified bean, introspecting and caching them the first time a particular bean class is encountered.Class<?>
getPropertyEditorClass
(Object bean, String name) Return the Java Class repesenting the property editor class that has been registered for this property (if any).protected Object
getPropertyOfMapBean
(Map<?, ?> bean, String propertyName) This method is called by getNestedProperty and setNestedProperty to define what it means to get a property from an object which implements Map.Class<?>
getPropertyType
(Object bean, String name) Gets the Java Class representing the property type of the specified property, ornull
if there is no such property for the specified bean.getReadMethod
(PropertyDescriptor descriptor) Return an accessible property getter method for this property, if there is one; otherwise returnnull
.getReadMethod
(Class<?> clazz, PropertyDescriptor descriptor) Return the property getter method for this property if accessible from givenclazz
(and if there is one at all); otherwise returnnull
.Gets the configuredResolver
implementation used by BeanUtils.getSimpleProperty
(Object bean, String name) Gets the value of the specified simple property of the specified bean, with no type conversions.getWriteMethod
(PropertyDescriptor descriptor) Return an accessible property setter method for this property, if there is one; otherwise returnnull
.getWriteMethod
(Class<?> clazz, PropertyDescriptor descriptor) Return the property setter method for this property if accessible from givenclazz
(and if there is one at all); otherwise returnnull
.boolean
isReadable
(Object bean, String name) Returntrue
if the specified property name identifies a readable property on the specified bean; otherwise, returnfalse
.boolean
isWriteable
(Object bean, String name) Returntrue
if the specified property name identifies a writable property on the specified bean; otherwise, returnfalse
.boolean
removeBeanIntrospector
(BeanIntrospector introspector) Removes the specifiedBeanIntrospector
.final void
Resets theBeanIntrospector
objects registered at this instance.void
setIndexedProperty
(Object bean, String name, int index, Object value) Sets the value of the specified indexed property of the specified bean, with no type conversions.void
setIndexedProperty
(Object bean, String name, Object value) Sets the value of the specified indexed property of the specified bean, with no type conversions.void
setMappedProperty
(Object bean, String name, Object value) Sets the value of the specified mapped property of the specified bean, with no type conversions.void
setMappedProperty
(Object bean, String name, String key, Object value) Sets the value of the specified mapped property of the specified bean, with no type conversions.void
setNestedProperty
(Object bean, String name, Object value) Sets the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.void
setProperty
(Object bean, String name, Object value) Sets the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.protected void
This method is called by method setNestedProperty when the current bean is found to be a Map object, and defines how to deal with setting a property on a Map.void
setResolver
(Resolver resolver) Configure theResolver
implementation used by BeanUtils.void
setSimpleProperty
(Object bean, String name, Object value) Sets the value of the specified simple property of the specified bean, with no type conversions.
-
Constructor Details
-
PropertyUtilsBean
public PropertyUtilsBean()Base constructor
-
-
Method Details
-
getInstance
Gets the PropertyUtils bean instance.- Returns:
- The PropertyUtils bean instance
-
addBeanIntrospector
Adds aBeanIntrospector
. This object is invoked when the property descriptors of a class need to be obtained.- Parameters:
introspector
- theBeanIntrospector
to be added (must not be null- Throws:
IllegalArgumentException
- if the argument is null- Since:
- 1.9
-
clearDescriptors
Clear any cached property descriptors information for all classes loaded by any class loaders. This is useful in cases where class loaders are thrown away to implement class reloading. -
copyProperties
public void copyProperties(Object dest, Object orig) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Copy property values from the "origin" bean to the "destination" bean for all cases where the property names are the same (even though the actual getter and setter methods might have been customized via
BeanInfo
classes). No conversions are performed on the actual property values -- it is assumed that the values retrieved from the origin bean are assignment-compatible with the types expected by the destination bean.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 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.Note, that this method will not copy a List to a List, or an Object[] to an Object[]. It's specifically for copying JavaBean properties.
- 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
argument is nullInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found
-
describe
public Map<String,Object> describe(Object bean) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Return the entire set of properties for which the specified bean provides a read method. This map contains the unconverted property values for all properties for which a read method is provided (i.e. where the
getReadMethod()
returns non-null).FIXME - Does not account for mapped properties.
- Parameters:
bean
- Bean whose properties are to be extracted- Returns:
- The set of properties for the bean
- Throws:
IllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
is nullInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found
-
getIndexedProperty
public Object getIndexedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Gets the value of the specified indexed property of the specified bean, with no type conversions. The zero-relative index of the required value must be included (in square brackets) as a suffix to the property name, orIllegalArgumentException
will be thrown. In addition to supporting the JavaBeans specification, this method has been extended to supportList
objects as well.- Parameters:
bean
- Bean whose property is to be extractedname
-propertyname[index]
of the property value to be extracted- Returns:
- the indexed property value
- Throws:
IndexOutOfBoundsException
- if the specified index is outside the valid range for the underlying array or ListIllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found
-
getIndexedProperty
public Object getIndexedProperty(Object bean, String name, int index) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Gets the value of the specified indexed property of the specified bean, with no type conversions. In addition to supporting the JavaBeans specification, this method has been extended to supportList
objects as well.- 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 value
- Throws:
IndexOutOfBoundsException
- if the specified index is outside the valid range for the underlying propertyIllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found
-
getMappedProperty
public Object getMappedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Gets the value of the specified mapped property of the specified bean, with no type conversions. The key of the required value must be included (in brackets) as a suffix to the property name, orIllegalArgumentException
will be thrown.- Parameters:
bean
- Bean whose property is to be extractedname
-propertyname(key)
of the property value to be extracted- Returns:
- the mapped 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
-
getMappedProperty
public Object getMappedProperty(Object bean, String name, String key) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Gets the value of the specified mapped property of the specified bean, with no type conversions.- Parameters:
bean
- Bean whose property is to be extractedname
- Mapped property name of the property value to be extractedkey
- Key of the property value to be extracted- Returns:
- the mapped 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
-
getNestedProperty
public Object getNestedProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Gets the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.- Parameters:
bean
- Bean whose property is to be extractedname
- Possibly nested name of the property to be extracted- Returns:
- the nested property value
- Throws:
IllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullNestedNullException
- 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
-
getProperty
public Object getProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Gets the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.- Parameters:
bean
- Bean whose property is to be extractedname
- Possibly indexed and/or nested name of the property to be extracted- Returns:
- the property value
- Throws:
IllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found
-
getPropertyDescriptor
public PropertyDescriptor getPropertyDescriptor(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Retrieve the property descriptor for the specified property of the specified bean, or return
null
if there is no such descriptor. This method resolves indexed and nested property references in the same manner as other methods in this class, except that if the last (or only) name element is indexed, the descriptor for the last resolved property itself is returned.FIXME - Does not work with DynaBeans.
Note that for Java 8 and above, this method no longer return IndexedPropertyDescriptor for
List
-typed properties, only for properties typed as native array. (BEANUTILS-492).- Parameters:
bean
- Bean for which a property descriptor is requestedname
- Possibly indexed and/or nested name of the property for which a property descriptor is requested- Returns:
- the property descriptor
- Throws:
IllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullIllegalArgumentException
- 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
-
getPropertyDescriptors
Retrieve the property descriptors for the specified class, introspecting and caching them the first time a particular bean class is encountered.
FIXME - Does not work with DynaBeans.
- Parameters:
beanClass
- Bean class for which property descriptors are requested- Returns:
- the property descriptors
- Throws:
IllegalArgumentException
- ifbeanClass
is null
-
getPropertyDescriptors
Retrieve the property descriptors for the specified bean, introspecting and caching them the first time a particular bean class is encountered.
FIXME - Does not work with DynaBeans.
- Parameters:
bean
- Bean for which property descriptors are requested- Returns:
- the property descriptors
- Throws:
IllegalArgumentException
- ifbean
is null
-
getPropertyEditorClass
public Class<?> getPropertyEditorClass(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Return the Java Class repesenting the property editor class that has been registered for this property (if any). This method follows the same name resolution rules used by
getPropertyDescriptor()
, so if the last element of a name reference is indexed, the property editor for the underlying property's class is returned.Note that
null
will be returned if there is no property, or if there is no registered property editor class. Because this return value is ambiguous, you should determine the existence of the property itself by other means.FIXME - Does not work with DynaBeans.
- Parameters:
bean
- Bean for which a property descriptor is requestedname
- Possibly indexed and/or nested name of the property for which a property descriptor is requested- Returns:
- the property editor class
- Throws:
IllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullIllegalArgumentException
- 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
-
getPropertyOfMapBean
protected Object getPropertyOfMapBean(Map<?, ?> bean, String propertyName) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodExceptionThis method is called by getNestedProperty and setNestedProperty to define what it means to get a property from an object which implements Map. See setPropertyOfMapBean for more information.- Parameters:
bean
- Map beanpropertyName
- The property name- Returns:
- the property value
- Throws:
IllegalArgumentException
- when the propertyName is regarded as being invalid.IllegalAccessException
- just in case subclasses override this method to try to access real getter methods and find permission is denied.InvocationTargetException
- just in case subclasses override this method to try to access real getter methods, and find it throws an exception when invoked.NoSuchMethodException
- just in case subclasses override this method to try to access real getter methods, and want to fail if no simple method is available.- Since:
- 1.8.0
-
getPropertyType
public Class<?> getPropertyType(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Gets the Java Class representing the property type of the specified property, ornull
if there is no such property for the specified bean. This method follows the same name resolution rules used bygetPropertyDescriptor()
, so if the last element of a name reference is indexed, the type of the property itself will be returned. If the last (or only) element has no property with the specified name,null
is returned.If the property is an indexed property (for example
String[]
), this method will return the type of the items within that array. Note that from Java 8 and newer, this method do not support such index types from items within an Collection, and will instead return the collection type (for example java.util.List) from the getter method.- Parameters:
bean
- Bean for which a property descriptor is requestedname
- Possibly indexed and/or nested name of the property for which a property descriptor is requested- Returns:
- The property type
- Throws:
IllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullIllegalArgumentException
- 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
-
getReadMethod
Return the property getter method for this property if accessible from given
clazz
(and if there is one at all); otherwise returnnull
.FIXME - Does not work with DynaBeans.
This fairly low-level method shouldn't be needed for most usecases. However, if you do have to implement something extra, you can improve consistency with the standard code (for example that of
getProperty()
) by calling this method instead of usingdescriptor.getReadMethod()
directly.- Parameters:
clazz
- The class of the read method will be invoked ondescriptor
- Property descriptor to return a getter for- Returns:
- The read method
- Since:
- 2.0.0
-
getReadMethod
Return an accessible property getter method for this property, if there is one; otherwise return
null
.FIXME - Does not work with DynaBeans.
- Parameters:
descriptor
- Property descriptor to return a getter for- Returns:
- The read method
-
getResolver
Gets the configuredResolver
implementation used by BeanUtils.The
Resolver
handles the property name expressions and the implementation in use effectively controls the dialect of the expression language that BeanUtils recognizes.DefaultResolver
is the default implementation used.- Returns:
- resolver The property expression resolver.
- Since:
- 1.8.0
-
getSimpleProperty
public Object getSimpleProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Gets the value of the specified simple property of the specified bean, with no type conversions.- Parameters:
bean
- Bean whose property is to be extractedname
- Name of the property to be extracted- Returns:
- The property value
- Throws:
IllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullIllegalArgumentException
- if the property name is nested or indexedInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found
-
getWriteMethod
Return the property setter method for this property if accessible from given
clazz
(and if there is one at all); otherwise returnnull
.FIXME - Does not work with DynaBeans.
This fairly low-level method shouldn't be needed for most usecases. However, if you do have to implement something extra, you can improve consistency with the standard code (for example that of
setProperty()
) by calling this method instead of usingdescriptor.getWriteMethod()
directly.- Parameters:
clazz
- The class of the read method will be invoked ondescriptor
- Property descriptor to return a setter for- Returns:
- The write method
- Since:
- 1.9.1
-
getWriteMethod
Return an accessible property setter method for this property, if there is one; otherwise return
null
.Note: This method does not work correctly with custom bean introspection under certain circumstances. It may return
null
even if a write method is defined for the property in question. UsegetWriteMethod(Class, PropertyDescriptor)
to be sure that the correct result is returned.FIXME - Does not work with DynaBeans.
- Parameters:
descriptor
- Property descriptor to return a setter for- Returns:
- The write method
-
isReadable
Returntrue
if the specified property name identifies a readable property on the specified bean; otherwise, returnfalse
.- Parameters:
bean
- Bean to be examined (may be aDynaBean
name
- Property name to be evaluated- Returns:
true
if the property is readable, otherwisefalse
- Throws:
IllegalArgumentException
- ifbean
orname</code> is <code>null
- Since:
- 1.6
-
isWriteable
Returntrue
if the specified property name identifies a writable property on the specified bean; otherwise, returnfalse
.- Parameters:
bean
- Bean to be examined (may be aDynaBean
name
- Property name to be evaluated- Returns:
true
if the property is writable, otherwisefalse
- Throws:
IllegalArgumentException
- ifbean
orname</code> is <code>null
- Since:
- 1.6
-
removeBeanIntrospector
Removes the specifiedBeanIntrospector
.- Parameters:
introspector
- theBeanIntrospector
to be removed- Returns:
- true if the
BeanIntrospector
existed and could be removed, false otherwise - Since:
- 1.9
-
resetBeanIntrospectors
Resets theBeanIntrospector
objects registered at this instance. After this method was called, only the defaultBeanIntrospector
is registered.- Since:
- 1.9
-
setIndexedProperty
public void setIndexedProperty(Object bean, String name, int index, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Sets the value of the specified indexed property of the specified bean, with no type conversions. In addition to supporting the JavaBeans specification, this method has been extended to supportList
objects as well.- Parameters:
bean
- Bean whose property is to be setname
- Simple property name of the property value to be setindex
- Index of the property value to be setvalue
- Value to which the indexed property element is to be set- Throws:
IndexOutOfBoundsException
- if the specified index is outside the valid range for the underlying propertyIllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found
-
setIndexedProperty
public void setIndexedProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Sets the value of the specified indexed property of the specified bean, with no type conversions. The zero-relative index of the required value must be included (in square brackets) as a suffix to the property name, orIllegalArgumentException
will be thrown. In addition to supporting the JavaBeans specification, this method has been extended to supportList
objects as well.- Parameters:
bean
- Bean whose property is to be modifiedname
-propertyname[index]
of the property value to be modifiedvalue
- Value to which the specified property element should be set- Throws:
IndexOutOfBoundsException
- if the specified index is outside the valid range for the underlying propertyIllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found
-
setMappedProperty
public void setMappedProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Sets the value of the specified mapped property of the specified bean, with no type conversions. The key of the value to set must be included (in brackets) as a suffix to the property name, orIllegalArgumentException
will be thrown.- Parameters:
bean
- Bean whose property is to be setname
-propertyname(key)
of the property value to be setvalue
- The property 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 exceptionNoSuchMethodException
- if an accessor method for this property cannot be found
-
setMappedProperty
public void setMappedProperty(Object bean, String name, String key, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Sets the value of the specified mapped property of the specified bean, with no type conversions.- Parameters:
bean
- Bean whose property is to be setname
- Mapped property name of the property value to be setkey
- Key of the property value to be setvalue
- The property 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 exceptionNoSuchMethodException
- if an accessor method for this property cannot be found
-
setNestedProperty
public void setNestedProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Sets the value of the (possibly nested) property of the specified name, for the specified bean, with no type conversions.Example values for parameter "name" are:
- "a" -- sets the value of property a of the specified bean
- "a.b" -- gets the value of property a of the specified bean, then on that object sets the value of property b.
- "a(key)" -- sets a value of mapped-property a on the specified bean. This effectively means bean.setA("key").
- "a[3]" -- sets a value of indexed-property a on the specified bean. This effectively means bean.setA(3).
- Parameters:
bean
- Bean whose property is to be modifiedname
- Possibly nested name of the property to be modifiedvalue
- Value to which the property is to be set- Throws:
IllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullIllegalArgumentException
- 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
-
setProperty
public void setProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Sets the value of the specified property of the specified bean, no matter which property reference format is used, with no type conversions.- Parameters:
bean
- Bean whose property is to be modifiedname
- Possibly indexed and/or nested name of the property to be modifiedvalue
- Value to which this property is to be set- Throws:
IllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found
-
setPropertyOfMapBean
protected void setPropertyOfMapBean(Map<String, Object> bean, String propertyName, Object value) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodExceptionThis method is called by method setNestedProperty when the current bean is found to be a Map object, and defines how to deal with setting a property on a Map.The standard implementation here is to:
- call bean.set(propertyName) for all propertyName values.
- throw an IllegalArgumentException if the property specifier contains MAPPED_DELIM or INDEXED_DELIM, as Map entries are essentially simple properties; mapping and indexing operations do not make sense when accessing a map (even thought the returned object may be a Map or an Array).
The default behavior of BeanUtils 1.7.1 or later is for assigning to "a.b" to mean a.put(b, obj) always. However the behavior of BeanUtils version 1.6.0, 1.6.1, 1.7.0 was for "a.b" to mean a.setB(obj) if such a method existed, and a.put(b, obj) otherwise. In version 1.5 it meant a.put(b, obj) always (ie the same as the behavior in the current version). In versions prior to 1.5 it meant a.setB(obj) always. [yes, this is all very unfortunate]
Users who would like to customize the meaning of "a.b" in method setNestedProperty when a is a Map can create a custom subclass of this class and override this method to implement the behavior of their choice, such as restoring the pre-1.4 behavior of this class if they wish. When overriding this method, do not forget to deal with MAPPED_DELIM and INDEXED_DELIM characters in the propertyName.
Note, however, that the recommended solution for objects that implement Map but want their simple properties to come first is for those objects to override their get/put methods to implement that behavior, and not to solve the problem by modifying the default behavior of the PropertyUtilsBean class by overriding this method.
- Parameters:
bean
- Map beanpropertyName
- The property namevalue
- the property value- Throws:
IllegalArgumentException
- when the propertyName is regarded as being invalid.IllegalAccessException
- just in case subclasses override this method to try to access real setter methods and find permission is denied.InvocationTargetException
- just in case subclasses override this method to try to access real setter methods, and find it throws an exception when invoked.NoSuchMethodException
- just in case subclasses override this method to try to access real setter methods, and want to fail if no simple method is available.- Since:
- 1.8.0
-
setResolver
Configure theResolver
implementation used by BeanUtils.The
Resolver
handles the property name expressions and the implementation in use effectively controls the dialect of the expression language that BeanUtils recognizes.DefaultResolver
is the default implementation used.- Parameters:
resolver
- The property expression resolver.- Since:
- 1.8.0
-
setSimpleProperty
public void setSimpleProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException Sets the value of the specified simple property of the specified bean, with no type conversions.- Parameters:
bean
- Bean whose property is to be modifiedname
- Name of the property to be modifiedvalue
- Value to which the property should be set- Throws:
IllegalAccessException
- if the caller does not have access to the property accessor methodIllegalArgumentException
- ifbean
orname
is nullIllegalArgumentException
- if the property name is nested or indexedInvocationTargetException
- if the property accessor method throws an exceptionNoSuchMethodException
- if an accessor method for this property cannot be found
-