org.springframework.beans
Class BeanWrapperImpl


public class BeanWrapperImpl
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. For example, loading a class by name won't work in some application servers if the class is used in a WAR but was loaded by the EJB class loader and the class to be loaded is in the WAR. (This class would use the EJB class loader, which couldn't see the required class.) We don't attempt to solve such problems by obtaining the classloader at runtime, because this violates the EJB programming restrictions.

Note: Regards property editors in org.springframework.beans.propertyeditors. Also explictly register the default ones to care for JREs that do not use the thread context class loader for editor search paths. 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.

Collections custom property editors can be written against comma delimited String as String arrays are converted in such a format if the array itself is not assignable.

Author:
Rod Johnson, Juergen Hoeller, Jean-Pierre Pawlak
Version: $Id: BeanWrapperImpl.java,v 1.24 2004/02/04 17:36:32 jhoeller Exp $
Since: 15 April 2001
See Also: registerCustomEditor(java.lang.Class,java.beans.PropertyEditor), PropertyEditorManager

Constructor Summary
BeanWrapperImpl()
          Create new empty BeanWrapperImpl.
BeanWrapperImpl(Object object)
          Create new BeanWrapperImpl for the given object.
BeanWrapperImpl(Object object, String nestedPath)
          Create new BeanWrapperImpl for the given object, registering a nested path that the object is in.
BeanWrapperImpl(Class clazz)
          Create new BeanWrapperImpl, wrapping a new instance of the specified class.

Method Summary
 ObjectdoTypeConversionIfNecessary(Object newValue, Class requiredType)
          Convert the value to the required type (if necessary from a String).
 PropertyEditorfindCustomEditor(Class requiredType, String propertyPath)
           
 PropertyDescriptorgetPropertyDescriptor(String propertyName)
           
 PropertyDescriptor[]getPropertyDescriptors()
           
 ObjectgetPropertyValue(String propertyName)
           
 ClassgetWrappedClass()
           
 ObjectgetWrappedInstance()
           
 Objectinvoke(String methodName, Object[] args)
           
 booleanisReadableProperty(String propertyName)
           
 booleanisWritableProperty(String propertyName)
           
 voidregisterCustomEditor(Class requiredType, PropertyEditor propertyEditor)
           
 voidregisterCustomEditor(Class requiredType, String propertyPath, PropertyEditor propertyEditor)
           
 voidsetPropertyValue(String propertyName, Object value)
           
 voidsetPropertyValue(PropertyValue pv)
           
 voidsetPropertyValues(Map map)
          Bulk update from a Map.
 voidsetPropertyValues(PropertyValues pvs)
           
 voidsetPropertyValues(PropertyValues propertyValues, boolean ignoreUnknown)
           
 voidsetWrappedInstance(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.
 StringtoString()
          This method is expensive! Only call for diagnostics and debugging reasons, not in production.

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(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(Object object, 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(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

doTypeConversionIfNecessary

public Object doTypeConversionIfNecessary(Object newValue, 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

findCustomEditor

public PropertyEditor findCustomEditor(Class requiredType, String propertyPath)

getPropertyDescriptor

public PropertyDescriptor getPropertyDescriptor(String propertyName)
throws BeansException

getPropertyDescriptors

public PropertyDescriptor[] getPropertyDescriptors()

getPropertyValue

public Object getPropertyValue(String propertyName)
throws BeansException

getWrappedClass

public Class getWrappedClass()

getWrappedInstance

public Object getWrappedInstance()

invoke

public Object invoke(String methodName, Object[] args)
throws BeansException

isReadableProperty

public boolean isReadableProperty(String propertyName)

isWritableProperty

public boolean isWritableProperty(String propertyName)

registerCustomEditor

public void registerCustomEditor(Class requiredType, PropertyEditor propertyEditor)

registerCustomEditor

public void registerCustomEditor(Class requiredType, String propertyPath, PropertyEditor propertyEditor)

setPropertyValue

public void setPropertyValue(String propertyName, Object value)
throws BeansException

setPropertyValue

public void setPropertyValue(PropertyValue pv)
throws BeansException

setPropertyValues

public void setPropertyValues(Map map)
throws BeansException
Bulk update from a Map. Bulk updates from PropertyValues are more powerful: this method is provided for convenience.
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

setPropertyValues

public void setPropertyValues(PropertyValues propertyValues, boolean ignoreUnknown)
throws BeansException

setWrappedInstance

public void setWrappedInstance(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.
Parameters:
object - new target
Throws:
BeansException - if the object cannot be changed

toString

public 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

Association Links

to Class java.util.Map

Registry for default PropertyEditors

to Class java.lang.String

The nested path of the object

to Class java.util.Map

to Class java.util.Map

Map with custom PropertyEditor instances

to Class org.springframework.beans.CachedIntrospectionResults

Cached introspections results for this object, to prevent encountering the cost of JavaBeans introspection every time.