org.springframework.beans
Class DirectFieldAccessor

java.lang.Object
  extended by org.springframework.beans.PropertyEditorRegistrySupport
      extended by org.springframework.beans.AbstractPropertyAccessor
          extended by org.springframework.beans.DirectFieldAccessor
All Implemented Interfaces:
ConfigurablePropertyAccessor, PropertyAccessor, PropertyEditorRegistry, TypeConverter

public class DirectFieldAccessor
extends AbstractPropertyAccessor

PropertyAccessor implementation that directly accesses instance fields. Allows for direct binding to fields instead of going through JavaBean setters.

This implementation just supports fields in the actual target object. It is not able to traverse nested fields.

A DirectFieldAccessor's default for the "extractOldValueForEditor" setting is "true", since a field can always be read without side effects.

Since:
2.0
Author:
Juergen Hoeller
See Also:
AbstractPropertyAccessor.setExtractOldValueForEditor(boolean), BeanWrapper, DirectFieldBindingResult, DataBinder.initDirectFieldAccess()

Field Summary
private  java.util.Map<java.lang.String,java.lang.reflect.Field> fieldMap
           
private  java.lang.Object target
           
private  TypeConverterDelegate typeConverterDelegate
           
 
Fields inherited from interface org.springframework.beans.PropertyAccessor
NESTED_PROPERTY_SEPARATOR, NESTED_PROPERTY_SEPARATOR_CHAR, PROPERTY_KEY_PREFIX, PROPERTY_KEY_PREFIX_CHAR, PROPERTY_KEY_SUFFIX, PROPERTY_KEY_SUFFIX_CHAR
 
Constructor Summary
DirectFieldAccessor(java.lang.Object target)
          Create a new DirectFieldAccessor for the given target object.
 
Method Summary
<T> T
convertIfNecessary(java.lang.Object value, java.lang.Class<T> requiredType, MethodParameter methodParam)
          Convert the value to the required type (if necessary from a String).
 java.lang.Class<?> getPropertyType(java.lang.String propertyName)
          Determine the property type for the given property path.
 TypeDescriptor getPropertyTypeDescriptor(java.lang.String propertyName)
          Return a type descriptor for the specified property: preferably from the read method, falling back to the write method.
 java.lang.Object getPropertyValue(java.lang.String propertyName)
          Actually get the value of a property.
 boolean isReadableProperty(java.lang.String propertyName)
          Determine whether the specified property is readable.
 boolean isWritableProperty(java.lang.String propertyName)
          Determine whether the specified property is writable.
 void setPropertyValue(java.lang.String propertyName, java.lang.Object newValue)
          Actually set a property value.
 
Methods inherited from class org.springframework.beans.AbstractPropertyAccessor
convertIfNecessary, isExtractOldValueForEditor, setExtractOldValueForEditor, setPropertyValue, setPropertyValues, setPropertyValues, setPropertyValues, setPropertyValues
 
Methods inherited from class org.springframework.beans.PropertyEditorRegistrySupport
copyCustomEditorsTo, copyDefaultEditorsTo, findCustomEditor, getConversionService, getDefaultEditor, guessPropertyTypeFromEditors, hasCustomEditorForElement, isSharedEditor, overrideDefaultEditor, registerCustomEditor, registerCustomEditor, registerDefaultEditors, registerSharedEditor, setConversionService, useConfigValueEditors
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.springframework.beans.ConfigurablePropertyAccessor
getConversionService, setConversionService
 
Methods inherited from interface org.springframework.beans.PropertyEditorRegistry
findCustomEditor, registerCustomEditor, registerCustomEditor
 

Field Detail

target

private final java.lang.Object target

fieldMap

private final java.util.Map<java.lang.String,java.lang.reflect.Field> fieldMap

typeConverterDelegate

private final TypeConverterDelegate typeConverterDelegate
Constructor Detail

DirectFieldAccessor

public DirectFieldAccessor(java.lang.Object target)
Create a new DirectFieldAccessor for the given target object.

Parameters:
target - the target object to access
Method Detail

isReadableProperty

public boolean isReadableProperty(java.lang.String propertyName)
                           throws BeansException
Description copied from interface: PropertyAccessor
Determine whether the specified property is readable.

Returns false if the property doesn't exist.

Parameters:
propertyName - the property to check (may be a nested path and/or an indexed/mapped property)
Returns:
whether the property is readable
Throws:
BeansException

isWritableProperty

public boolean isWritableProperty(java.lang.String propertyName)
                           throws BeansException
Description copied from interface: PropertyAccessor
Determine whether the specified property is writable.

Returns false if the property doesn't exist.

Parameters:
propertyName - the property to check (may be a nested path and/or an indexed/mapped property)
Returns:
whether the property is writable
Throws:
BeansException

getPropertyType

public java.lang.Class<?> getPropertyType(java.lang.String propertyName)
                                   throws BeansException
Description copied from class: PropertyEditorRegistrySupport
Determine the property type for the given property path.

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.

Specified by:
getPropertyType in interface PropertyAccessor
Overrides:
getPropertyType in class AbstractPropertyAccessor
Parameters:
propertyName - the property path to determine the type for
Returns:
the type of the property, or null if not determinable
Throws:
InvalidPropertyException - if there is no such property or if the property isn't readable
PropertyAccessException - if the property was valid but the accessor method failed
BeansException
See Also:
PropertyAccessor.getPropertyType(String)

getPropertyTypeDescriptor

public TypeDescriptor getPropertyTypeDescriptor(java.lang.String propertyName)
                                         throws BeansException
Description copied from interface: PropertyAccessor
Return a type descriptor for the specified property: preferably from the read method, falling back to the write method.

Parameters:
propertyName - the property to check (may be a nested path and/or an indexed/mapped property)
Returns:
the property type for the particular property, or null if not determinable
Throws:
InvalidPropertyException - if there is no such property or if the property isn't readable
BeansException

getPropertyValue

public java.lang.Object getPropertyValue(java.lang.String propertyName)
                                  throws BeansException
Description copied from class: AbstractPropertyAccessor
Actually get the value of a property.

Specified by:
getPropertyValue in interface PropertyAccessor
Specified by:
getPropertyValue in class AbstractPropertyAccessor
Parameters:
propertyName - name of the property to get the value of
Returns:
the value of the property
Throws:
InvalidPropertyException - if there is no such property or if the property isn't readable
PropertyAccessException - if the property was valid but the accessor method failed
BeansException

setPropertyValue

public void setPropertyValue(java.lang.String propertyName,
                             java.lang.Object newValue)
                      throws BeansException
Description copied from class: AbstractPropertyAccessor
Actually set a property value.

Specified by:
setPropertyValue in interface PropertyAccessor
Specified by:
setPropertyValue in class AbstractPropertyAccessor
Parameters:
propertyName - name of the property to set value of
newValue - the new value
Throws:
InvalidPropertyException - if there is no such property or if the property isn't writable
PropertyAccessException - if the property was valid but the accessor method failed or a type mismatch occured
BeansException

convertIfNecessary

public <T> T convertIfNecessary(java.lang.Object value,
                                java.lang.Class<T> requiredType,
                                MethodParameter methodParam)
                     throws TypeMismatchException
Description copied from interface: TypeConverter
Convert the value to the required type (if necessary from a String).

Conversions from String to any type will typically use the setAsText method of the PropertyEditor class. Note that a PropertyEditor must be registered for the given class for this to work; this is a standard JavaBeans API. A number of PropertyEditors are automatically registered.

Parameters:
value - the value to convert
requiredType - the type we must convert to (or null if not known, for example in case of a collection element)
methodParam - the method parameter that is the target of the conversion (for analysis of generic types; may be null)
Returns:
the new value, possibly the result of type conversion
Throws:
TypeMismatchException - if type conversion failed
See Also:
PropertyEditor.setAsText(String), PropertyEditor.getValue()