public class BeanWrapperImpl extends AbstractPropertyAccessor implements BeanWrapper
BeanWrapper
implementation that should be sufficient
for all typical use cases. Caches introspection results for efficiency.
Note: Auto-registers default property editors from the
org.springframework.beans.propertyeditors
package, which apply
in addition to the JDK's standard PropertyEditors. Applications can call
the PropertyEditorRegistrySupport.registerCustomEditor(Class, java.beans.PropertyEditor)
method
to register an editor for a particular instance (i.e. they are not shared
across the application). See the base class
PropertyEditorRegistrySupport
for details.
BeanWrapperImpl
will convert collection and array values
to the corresponding target collections or arrays, if necessary. Custom
property editors that deal with collections or arrays can either be
written via PropertyEditor's setValue
, or against a
comma-delimited String via setAsText
, as String arrays are
converted in such a format if the array itself is not assignable.
NOTE: As of Spring 2.5, this is - for almost all purposes - an
internal class. It is just public in order to allow for access from
other framework packages. For standard application access purposes, use the
PropertyAccessorFactory.forBeanPropertyAccess(java.lang.Object)
factory method instead.
PropertyEditorRegistrySupport.registerCustomEditor(java.lang.Class<?>, java.beans.PropertyEditor)
,
AbstractPropertyAccessor.setPropertyValues(java.util.Map<?, ?>)
,
setPropertyValue(java.lang.String, java.lang.Object)
,
getPropertyValue(java.lang.String)
,
getPropertyType(java.lang.String)
,
BeanWrapper
,
PropertyEditorRegistrySupport
NESTED_PROPERTY_SEPARATOR, NESTED_PROPERTY_SEPARATOR_CHAR, PROPERTY_KEY_PREFIX, PROPERTY_KEY_PREFIX_CHAR, PROPERTY_KEY_SUFFIX, PROPERTY_KEY_SUFFIX_CHAR
Constructor and Description |
---|
BeanWrapperImpl()
Create new empty BeanWrapperImpl.
|
BeanWrapperImpl(boolean registerDefaultEditors)
Create new empty BeanWrapperImpl.
|
BeanWrapperImpl(Class<?> clazz)
Create new BeanWrapperImpl, wrapping a new instance of the specified class.
|
BeanWrapperImpl(Object object)
Create new BeanWrapperImpl for the given object.
|
BeanWrapperImpl(Object object,
String nestedPath,
Object rootObject)
Create new BeanWrapperImpl for the given object,
registering a nested path that the object is in.
|
Modifier and Type | Method and Description |
---|---|
Object |
convertForProperty(Object value,
String propertyName)
Convert the given value for the specified property to the latter's type.
|
int |
getAutoGrowCollectionLimit()
Return the limit for array and collection auto-growing.
|
protected BeanWrapperImpl |
getBeanWrapperForPropertyPath(String propertyPath)
Recursively navigate to return a BeanWrapper for the nested property path.
|
String |
getNestedPath()
Return the nested path of the object wrapped by this BeanWrapper.
|
PropertyDescriptor |
getPropertyDescriptor(String propertyName)
Obtain the property descriptor for a specific property
of the wrapped object.
|
protected PropertyDescriptor |
getPropertyDescriptorInternal(String propertyName)
Internal version of
getPropertyDescriptor(java.lang.String) :
Returns null if not found rather than throwing an exception. |
PropertyDescriptor[] |
getPropertyDescriptors()
Obtain the PropertyDescriptors for the wrapped object
(as determined by standard JavaBeans introspection).
|
Class<?> |
getPropertyType(String propertyName)
Determine the property type for the given property path.
|
TypeDescriptor |
getPropertyTypeDescriptor(String propertyName)
Return a type descriptor for the specified property:
preferably from the read method, falling back to the write method.
|
Object |
getPropertyValue(String propertyName)
Actually get the value of a property.
|
Class<?> |
getRootClass()
Return the class of the root object at the top of the path of this BeanWrapper.
|
Object |
getRootInstance()
Return the root object at the top of the path of this BeanWrapper.
|
AccessControlContext |
getSecurityContext()
Return the security context used during the invocation of the wrapped instance methods.
|
Class<?> |
getWrappedClass()
Return the type of the wrapped JavaBean object.
|
Object |
getWrappedInstance()
Return the bean instance wrapped by this object, if any.
|
boolean |
isAutoGrowNestedPaths()
Return whether "auto-growing" of nested paths has been activated.
|
boolean |
isReadableProperty(String propertyName)
Determine whether the specified property is readable.
|
boolean |
isWritableProperty(String propertyName)
Determine whether the specified property is writable.
|
protected BeanWrapperImpl |
newNestedBeanWrapper(Object object,
String nestedPath)
Create a new nested BeanWrapper instance.
|
void |
setAutoGrowCollectionLimit(int autoGrowCollectionLimit)
Specify a limit for array and collection auto-growing.
|
void |
setAutoGrowNestedPaths(boolean autoGrowNestedPaths)
Set whether this BeanWrapper should attempt to "auto-grow" a nested path that contains a null value.
|
protected void |
setIntrospectionClass(Class<?> clazz)
Set the class to introspect.
|
void |
setPropertyValue(PropertyValue pv)
Set the specified value as current property value.
|
void |
setPropertyValue(String propertyName,
Object value)
Actually set a property value.
|
void |
setSecurityContext(AccessControlContext acc)
Set the security context used during the invocation of the wrapped instance methods.
|
void |
setWrappedInstance(Object object)
Switch the target object, replacing the cached introspection results only
if the class of the new object is different to that of the replaced object.
|
void |
setWrappedInstance(Object object,
String nestedPath,
Object rootObject)
Switch the target object, replacing the cached introspection results only
if the class of the new object is different to that of the replaced object.
|
String |
toString() |
isExtractOldValueForEditor, setExtractOldValueForEditor, setPropertyValues, setPropertyValues, setPropertyValues, setPropertyValues
convertIfNecessary, convertIfNecessary, convertIfNecessary
copyCustomEditorsTo, copyDefaultEditorsTo, findCustomEditor, getConversionService, getDefaultEditor, guessPropertyTypeFromEditors, hasCustomEditorForElement, isSharedEditor, overrideDefaultEditor, registerCustomEditor, registerCustomEditor, registerDefaultEditors, registerSharedEditor, setConversionService, useConfigValueEditors
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getConversionService, isExtractOldValueForEditor, setConversionService, setExtractOldValueForEditor
setPropertyValues, setPropertyValues, setPropertyValues, setPropertyValues
findCustomEditor, registerCustomEditor, registerCustomEditor
convertIfNecessary, convertIfNecessary, convertIfNecessary
public BeanWrapperImpl()
setWrappedInstance(java.lang.Object)
public BeanWrapperImpl(boolean registerDefaultEditors)
registerDefaultEditors
- whether to register default editors
(can be suppressed if the BeanWrapper won't need any type conversion)setWrappedInstance(java.lang.Object)
public BeanWrapperImpl(Object object)
object
- object wrapped by this BeanWrapperpublic BeanWrapperImpl(Class<?> clazz)
clazz
- class to instantiate and wrappublic BeanWrapperImpl(Object object, String nestedPath, Object rootObject)
object
- object wrapped by this BeanWrappernestedPath
- the nested path of the objectrootObject
- the root object at the top of the pathpublic void setWrappedInstance(Object object)
object
- the new target objectpublic void setWrappedInstance(Object object, String nestedPath, Object rootObject)
object
- the new target objectnestedPath
- the nested path of the objectrootObject
- the root object at the top of the pathpublic final Object getWrappedInstance()
BeanWrapper
getWrappedInstance
in interface BeanWrapper
null
if none setpublic final Class<?> getWrappedClass()
BeanWrapper
getWrappedClass
in interface BeanWrapper
null
if no wrapped object has been setpublic final String getNestedPath()
public final Object getRootInstance()
getNestedPath()
public final Class<?> getRootClass()
getNestedPath()
public void setAutoGrowNestedPaths(boolean autoGrowNestedPaths)
If "true", a null path location will be populated with a default object value and traversed
instead of resulting in a NullValueInNestedPathException
. Turning this flag on also
enables auto-growth of collection elements when accessing an out-of-bounds index.
Default is "false" on a plain BeanWrapper.
setAutoGrowNestedPaths
in interface BeanWrapper
public boolean isAutoGrowNestedPaths()
isAutoGrowNestedPaths
in interface BeanWrapper
public void setAutoGrowCollectionLimit(int autoGrowCollectionLimit)
Default is unlimited on a plain BeanWrapper.
setAutoGrowCollectionLimit
in interface BeanWrapper
public int getAutoGrowCollectionLimit()
getAutoGrowCollectionLimit
in interface BeanWrapper
public void setSecurityContext(AccessControlContext acc)
public AccessControlContext getSecurityContext()
protected void setIntrospectionClass(Class<?> clazz)
clazz
- the class to introspectpublic PropertyDescriptor[] getPropertyDescriptors()
BeanWrapper
getPropertyDescriptors
in interface BeanWrapper
public PropertyDescriptor getPropertyDescriptor(String propertyName) throws BeansException
BeanWrapper
getPropertyDescriptor
in interface BeanWrapper
propertyName
- the property to obtain the descriptor for
(may be a nested path, but no indexed/mapped property)InvalidPropertyException
- if there is no such propertyBeansException
protected PropertyDescriptor getPropertyDescriptorInternal(String propertyName) throws BeansException
getPropertyDescriptor(java.lang.String)
:
Returns null
if not found rather than throwing an exception.propertyName
- the property to obtain the descriptor fornull
if not foundBeansException
- in case of introspection failurepublic Class<?> getPropertyType(String propertyName) throws BeansException
PropertyEditorRegistrySupport
Called by PropertyEditorRegistrySupport.findCustomEditor(java.lang.Class<?>, java.lang.String)
if no required type has been specified,
to be able to find a type-specific editor even if just given a property path.
The default implementation always returns null
.
BeanWrapperImpl overrides this with the standard getPropertyType
method as defined by the BeanWrapper interface.
getPropertyType
in interface PropertyAccessor
getPropertyType
in class AbstractPropertyAccessor
propertyName
- the property path to determine the type fornull
if not determinableInvalidPropertyException
- if there is no such property or
if the property isn't readablePropertyAccessException
- if the property was valid but the
accessor method failedBeansException
PropertyAccessor.getPropertyType(String)
public TypeDescriptor getPropertyTypeDescriptor(String propertyName) throws BeansException
PropertyAccessor
getPropertyTypeDescriptor
in interface PropertyAccessor
propertyName
- the property to check
(may be a nested path and/or an indexed/mapped property)null
if not determinableInvalidPropertyException
- if there is no such property or
if the property isn't readableBeansException
public boolean isReadableProperty(String propertyName)
PropertyAccessor
Returns false
if the property doesn't exist.
isReadableProperty
in interface PropertyAccessor
propertyName
- the property to check
(may be a nested path and/or an indexed/mapped property)public boolean isWritableProperty(String propertyName)
PropertyAccessor
Returns false
if the property doesn't exist.
isWritableProperty
in interface PropertyAccessor
propertyName
- the property to check
(may be a nested path and/or an indexed/mapped property)public Object convertForProperty(Object value, String propertyName) throws TypeMismatchException
This method is only intended for optimizations in a BeanFactory.
Use the convertIfNecessary
methods for programmatic conversion.
value
- the value to convertpropertyName
- the target property
(note that nested or indexed properties are not supported here)TypeMismatchException
- if type conversion failedprotected BeanWrapperImpl getBeanWrapperForPropertyPath(String propertyPath)
propertyPath
- property property path, which may be nestedprotected BeanWrapperImpl newNestedBeanWrapper(Object object, String nestedPath)
Default implementation creates a BeanWrapperImpl instance. Can be overridden in subclasses to create a BeanWrapperImpl subclass.
object
- object wrapped by this BeanWrappernestedPath
- the nested path of the objectpublic Object getPropertyValue(String propertyName) throws BeansException
AbstractPropertyAccessor
getPropertyValue
in interface PropertyAccessor
getPropertyValue
in class AbstractPropertyAccessor
propertyName
- name of the property to get the value ofInvalidPropertyException
- if there is no such property or
if the property isn't readablePropertyAccessException
- if the property was valid but the
accessor method failedBeansException
public void setPropertyValue(String propertyName, Object value) throws BeansException
AbstractPropertyAccessor
setPropertyValue
in interface PropertyAccessor
setPropertyValue
in class AbstractPropertyAccessor
propertyName
- name of the property to set value ofvalue
- the new valueInvalidPropertyException
- if there is no such property or
if the property isn't writablePropertyAccessException
- if the property was valid but the
accessor method failed or a type mismatch occurredBeansException
public void setPropertyValue(PropertyValue pv) throws BeansException
PropertyAccessor
setPropertyValue
in interface PropertyAccessor
setPropertyValue
in class AbstractPropertyAccessor
pv
- an object containing the new property valueInvalidPropertyException
- if there is no such property or
if the property isn't writablePropertyAccessException
- if the property was valid but the
accessor method failed or a type mismatch occurredBeansException