org.springframework.beans
Class BeanWrapperImpl

java.lang.Object
  extended byorg.springframework.beans.BeanWrapperImpl
All Implemented Interfaces:
BeanWrapper

public class BeanWrapperImpl
extends java.lang.Object
implements BeanWrapper

Default implementation of the BeanWrapper interface that should be sufficient for all normal uses. Caches introspection results for efficiency.

Note: This class never tries to load a class by name, as this can pose class loading problems in J2EE applications with multiple deployment modules. The caller is responsible for loading a target class.

Note: Auto-registers all default property editors (not the custom ones) in the org.springframework.beans.propertyeditors package. Applications can either use a standard PropertyEditorManager to register a custom editor before using a BeanWrapperImpl instance, or call the instance's registerCustomEditor method to register an editor for the particular instance.

BeanWrapperImpl will convert List and array values to the corresponding target arrays, if necessary. Custom property editors that deal with Lists or arrays can be written against a comma delimited String as String arrays are converted in such a format if the array itself is not assignable.

Since:
15 April 2001
Version:
$Id: BeanWrapperImpl.java,v 1.33 2004/03/19 16:09:16 jhoeller Exp $
Author:
Rod Johnson, Juergen Hoeller, Jean-Pierre Pawlak
See Also:
registerCustomEditor(java.lang.Class, java.beans.PropertyEditor), PropertyEditorManager, ClassEditor, FileEditor, LocaleEditor, PropertiesEditor, StringArrayPropertyEditor, URLEditor

Field Summary
 
Fields inherited from interface org.springframework.beans.BeanWrapper
NESTED_PROPERTY_SEPARATOR
 
Constructor Summary
BeanWrapperImpl()
          Create new empty BeanWrapperImpl.
BeanWrapperImpl(java.lang.Class clazz)
          Create new BeanWrapperImpl, wrapping a new instance of the specified class.
BeanWrapperImpl(java.lang.Object object)
          Create new BeanWrapperImpl for the given object.
BeanWrapperImpl(java.lang.Object object, java.lang.String nestedPath)
          Create new BeanWrapperImpl for the given object, registering a nested path that the object is in.
 
Method Summary
 java.lang.Object doTypeConversionIfNecessary(java.lang.Object newValue, java.lang.Class requiredType)
          Convert the value to the required type (if necessary from a String).
protected  java.lang.Object doTypeConversionIfNecessary(java.lang.String propertyName, java.lang.String propertyDescriptor, java.lang.Object oldValue, java.lang.Object newValue, java.lang.Class requiredType)
          Convert the value to the required type (if necessary from a String), for the specified property.
 java.beans.PropertyEditor findCustomEditor(java.lang.Class requiredType, java.lang.String propertyPath)
          Find a custom property editor for the given type and property.
 java.beans.PropertyDescriptor getPropertyDescriptor(java.lang.String propertyName)
          Get the property descriptor for a particular property.
 java.beans.PropertyDescriptor[] getPropertyDescriptors()
          Get the PropertyDescriptors identified on this object (standard JavaBeans introspection).
 java.lang.Object getPropertyValue(java.lang.String propertyName)
          Get the value of a property.
 java.lang.Class getWrappedClass()
          Convenience method to return the class of the wrapped object.
 java.lang.Object getWrappedInstance()
          Return the bean wrapped by this object (cannot be null).
 boolean isReadableProperty(java.lang.String propertyName)
          Return whether this property is readable.
 boolean isWritableProperty(java.lang.String propertyName)
          Return whether this property is writable.
 void registerCustomEditor(java.lang.Class requiredType, java.beans.PropertyEditor propertyEditor)
          Register the given custom property editor for all properties of the given type.
 void registerCustomEditor(java.lang.Class requiredType, java.lang.String propertyPath, java.beans.PropertyEditor propertyEditor)
          Register the given custom property editor for the given type and property, or for all properties of the given type.
 void setPropertyValue(PropertyValue pv)
          Update a property value.
 void setPropertyValue(java.lang.String propertyName, java.lang.Object value)
          Set a property value.
 void setPropertyValues(java.util.Map map)
          Bulk update from a Map.
 void setPropertyValues(PropertyValues pvs)
          The preferred way to perform a bulk update.
 void setPropertyValues(PropertyValues propertyValues, boolean ignoreUnknown)
          Perform a bulk update with full control over behavior.
 void setWrappedInstance(java.lang.Object object)
          Switches the target object, replacing the cached introspection results only if the class of the new object is different to that of the replaced object.
 java.lang.String toString()
          This method is expensive! Only call for diagnostics and debugging reasons, not in production.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

BeanWrapperImpl

public BeanWrapperImpl()
Create new empty BeanWrapperImpl. Wrapped instance needs to be set afterwards.

See Also:
setWrappedInstance(java.lang.Object)

BeanWrapperImpl

public BeanWrapperImpl(java.lang.Object object)
                throws BeansException
Create new BeanWrapperImpl for the given object.

Parameters:
object - object wrapped by this BeanWrapper.
Throws:
BeansException - if the object cannot be wrapped by a BeanWrapper

BeanWrapperImpl

public BeanWrapperImpl(java.lang.Object object,
                       java.lang.String nestedPath)
                throws BeansException
Create new BeanWrapperImpl for the given object, registering a nested path that the object is in.

Parameters:
object - object wrapped by this BeanWrapper.
nestedPath - the nested path of the object
Throws:
BeansException - if the object cannot be wrapped by a BeanWrapper

BeanWrapperImpl

public BeanWrapperImpl(java.lang.Class clazz)
                throws BeansException
Create new BeanWrapperImpl, wrapping a new instance of the specified class.

Parameters:
clazz - class to instantiate and wrap
Throws:
BeansException - if the class cannot be wrapped by a BeanWrapper
Method Detail

setWrappedInstance

public void setWrappedInstance(java.lang.Object object)
                        throws BeansException
Switches the target object, replacing the cached introspection results only if the class of the new object is different to that of the replaced object.

Specified by:
setWrappedInstance in interface BeanWrapper
Parameters:
object - new target
Throws:
BeansException - if the object cannot be changed

getWrappedClass

public java.lang.Class getWrappedClass()
Description copied from interface: BeanWrapper
Convenience method to return the class of the wrapped object.

Specified by:
getWrappedClass in interface BeanWrapper
Returns:
the class of the wrapped object

getWrappedInstance

public java.lang.Object getWrappedInstance()
Description copied from interface: BeanWrapper
Return the bean wrapped by this object (cannot be null).

Specified by:
getWrappedInstance in interface BeanWrapper
Returns:
the bean wrapped by this object

registerCustomEditor

public void registerCustomEditor(java.lang.Class requiredType,
                                 java.beans.PropertyEditor propertyEditor)
Description copied from interface: BeanWrapper
Register the given custom property editor for all properties of the given type.

Specified by:
registerCustomEditor in interface BeanWrapper
Parameters:
requiredType - type of the property
propertyEditor - editor to register

registerCustomEditor

public void registerCustomEditor(java.lang.Class requiredType,
                                 java.lang.String propertyPath,
                                 java.beans.PropertyEditor propertyEditor)
Description copied from interface: BeanWrapper
Register the given custom property editor for the given type and property, or for all properties of the given type.

Specified by:
registerCustomEditor in interface BeanWrapper
Parameters:
requiredType - type of the property, can be null if a property is given but should be specified in any case for consistency checking
propertyPath - path of the property (name or nested path), or null if registering an editor for all properties of the given type
propertyEditor - editor to register

findCustomEditor

public java.beans.PropertyEditor findCustomEditor(java.lang.Class requiredType,
                                                  java.lang.String propertyPath)
Description copied from interface: BeanWrapper
Find a custom property editor for the given type and property.

Specified by:
findCustomEditor in interface BeanWrapper
Parameters:
requiredType - type of the property, can be null if a property is given but should be specified in any case for consistency checking
propertyPath - path of the property (name or nested path), or null if looking for an editor for all properties of the given type
Returns:
the registered editor, or null if none

getPropertyValue

public java.lang.Object getPropertyValue(java.lang.String propertyName)
                                  throws BeansException
Description copied from interface: BeanWrapper
Get the value of a property.

Specified by:
getPropertyValue in interface BeanWrapper
Parameters:
propertyName - name of the property to get the value of
Returns:
the value of the property.
Throws:
BeansException

setPropertyValue

public void setPropertyValue(java.lang.String propertyName,
                             java.lang.Object value)
                      throws BeansException
Description copied from interface: BeanWrapper
Set a property value. This method is provided for convenience only. The setPropertyValue(PropertyValue) method is more powerful.

Specified by:
setPropertyValue in interface BeanWrapper
Parameters:
propertyName - name of the property to set value of
value - the new value
Throws:
BeansException

setPropertyValue

public void setPropertyValue(PropertyValue pv)
                      throws BeansException
Description copied from interface: BeanWrapper
Update a property value. This is the preferred way to update an individual property.

Specified by:
setPropertyValue in interface BeanWrapper
Parameters:
pv - object containing new property value
Throws:
BeansException

setPropertyValues

public void setPropertyValues(java.util.Map map)
                       throws BeansException
Bulk update from a Map. Bulk updates from PropertyValues are more powerful: this method is provided for convenience.

Specified by:
setPropertyValues in interface BeanWrapper
Parameters:
map - map containing properties to set, as name-value pairs. The map may include nested properties.
Throws:
BeansException - if there's a fatal, low-level exception

setPropertyValues

public void setPropertyValues(PropertyValues pvs)
                       throws BeansException
Description copied from interface: BeanWrapper
The preferred way to perform a bulk update.

Note that performing a bulk update differs from performing a single update, in that an implementation of this class will continue to update properties if a recoverable error (such as a vetoed property change or a type mismatch, but not an invalid fieldname or the like) is encountered, throwing a PropertyAccessExceptionsException containing all the individual errors. This exception can be examined later to see all binding errors. Properties that were successfully updated stay changed.

Does not allow unknown fields. Equivalent to setPropertyValues(pvs, false, null).

Specified by:
setPropertyValues in interface BeanWrapper
Parameters:
pvs - PropertyValues to set on the target object
Throws:
BeansException

setPropertyValues

public void setPropertyValues(PropertyValues propertyValues,
                              boolean ignoreUnknown)
                       throws BeansException
Description copied from interface: BeanWrapper
Perform a bulk update with full control over behavior. Note that performing a bulk update differs from performing a single update, in that an implementation of this class will continue to update properties if a recoverable error (such as a vetoed property change or a type mismatch, but not an invalid fieldname or the like) is encountered, throwing a PropertyAccessExceptionsException containing all the individual errors. This exception can be examined later to see all binding errors. Properties that were successfully updated stay changed.

Does not allow unknown fields.

Specified by:
setPropertyValues in interface BeanWrapper
Parameters:
propertyValues - PropertyValues to set on the target object
ignoreUnknown - should we ignore unknown values (not found in the bean!?)
Throws:
BeansException

doTypeConversionIfNecessary

public java.lang.Object doTypeConversionIfNecessary(java.lang.Object newValue,
                                                    java.lang.Class requiredType)
                                             throws BeansException
Convert the value to the required type (if necessary from a String). Conversions from String to any type use the setAsText() method of the PropertyEditor class. Note that a PropertyEditor must be registered for this class for this to work. This is a standard Java Beans API. A number of property editors are automatically registered by this class.

Parameters:
newValue - proposed change value.
requiredType - type we must convert to
Returns:
new value, possibly the result of type convertion
Throws:
BeansException - if there is an internal error

doTypeConversionIfNecessary

protected java.lang.Object doTypeConversionIfNecessary(java.lang.String propertyName,
                                                       java.lang.String propertyDescriptor,
                                                       java.lang.Object oldValue,
                                                       java.lang.Object newValue,
                                                       java.lang.Class requiredType)
                                                throws BeansException
Convert the value to the required type (if necessary from a String), for the specified property.

Parameters:
propertyName - name of the property
oldValue - previous value, if available (may be null)
newValue - proposed change value.
requiredType - type we must convert to
Returns:
new value, possibly the result of type convertion
Throws:
BeansException - if there is an internal error

getPropertyDescriptors

public java.beans.PropertyDescriptor[] getPropertyDescriptors()
Description copied from interface: BeanWrapper
Get the PropertyDescriptors identified on this object (standard JavaBeans introspection).

Specified by:
getPropertyDescriptors in interface BeanWrapper
Returns:
the PropertyDescriptors identified on this object

getPropertyDescriptor

public java.beans.PropertyDescriptor getPropertyDescriptor(java.lang.String propertyName)
                                                    throws BeansException
Description copied from interface: BeanWrapper
Get the property descriptor for a particular property.

Specified by:
getPropertyDescriptor in interface BeanWrapper
Parameters:
propertyName - property to check status for
Returns:
the property descriptor for a particular property
Throws:
BeansException

isReadableProperty

public boolean isReadableProperty(java.lang.String propertyName)
Description copied from interface: BeanWrapper
Return whether this property is readable. Returns false if the property doesn't exist.

Specified by:
isReadableProperty in interface BeanWrapper
Parameters:
propertyName - property to check status for
Returns:
whether this property is readable

isWritableProperty

public boolean isWritableProperty(java.lang.String propertyName)
Description copied from interface: BeanWrapper
Return whether this property is writable. Returns false if the property doesn't exist.

Specified by:
isWritableProperty in interface BeanWrapper
Parameters:
propertyName - property to check status for
Returns:
whether this property is writable

toString

public java.lang.String toString()
This method is expensive! Only call for diagnostics and debugging reasons, not in production.

Returns:
a string describing the state of this object


Copyright (C) 2003-2004 The Spring Framework Project.