Class FieldHandlerImpl<T>

All Implemented Interfaces:
ConfigurableFieldHandler<T>, FieldHandler<T>

public final class FieldHandlerImpl<T> extends AbstractFieldHandler<T>
A field handler that knows how to get/set the values of a field directly or through the get/set methods. Uses reflection.

Note: the field Java type is obtained from TypeInfo.getFieldType(), but if the field is a collection, the actual field/accessor type is obtained from TypeInfo.getCollectionHandler() and the object to create (with newInstance(Object)) is the former field type.

Version:
$Revision: 9062 $ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
Author:
Assaf Arkin
  • Constructor Details

    • FieldHandlerImpl

      public FieldHandlerImpl(FieldHandler<T> handler, TypeInfo typeInfo)
      Construct a new field handler for the specified field. The field must be public, and may not be static or transient. The field name is determined from the Java field, the type from the type information.
      Parameters:
      handler -
      typeInfo - Type information
    • FieldHandlerImpl

      public FieldHandlerImpl(Field field, TypeInfo typeInfo) throws MappingException
      Construct a new field handler for the specified field. The field must be public, and may not be static or transient. The field name is determined from the Java field, the type from the type information.
      Parameters:
      field - The field being described
      typeInfo - Type information
      Throws:
      MappingException - If the field is not public, is static or transient
    • FieldHandlerImpl

      public FieldHandlerImpl(String fieldName, Method[] getSequence, Method[] setSequence, Method getMethod, Method setMethod, TypeInfo typeInfo) throws MappingException
      Construct a new field handler for the specified field that is accessed through the accessor methods (get/set). The accessor methods must be public and not static. The field name is required for descriptive purposes. The field type must match the return value of the get method and the single parameter of the set method. Either get or set methods are optional.
      Parameters:
      fieldName - The field being described
      getMethod - The method used to retrieve the field value, must accept no parameters and have a return type castable to the field type
      setMethod - The method used to set the field value, must accept a single parameter that is castable to the field type
      typeInfo - Type information
      Throws:
      MappingException - If the get or set method are not public, are static, or do not specify the proper types
  • Method Details

    • getConvertFrom

      public TypeConvertor getConvertFrom()
    • getConvertTo

      public TypeConvertor getConvertTo()
    • getValue

      public T getValue(Object object)
      Returns the value of the field from the object.
      Parameters:
      object - The object
      Returns:
      The value of the field
      See Also:
    • setValue

      public void setValue(Object object, T value)
      Sets the value of the field on the object.
      Parameters:
      object - The object.
      value - The new value.
      See Also:
    • resetValue

      public void resetValue(Object object)
      Description copied from interface: FieldHandler
      Sets the value of the field to a default value.

      Reference fields are set to null, primitive fields are set to their default value, collection fields are emptied of all elements.

      Parameters:
      object - The object.
    • newInstance

      public T newInstance(Object parent) throws IllegalStateException
      Creates a new instance of the object described by this field.
      Parameters:
      parent - The object for which the field is created
      Returns:
      A new instance of the field's value
      Throws:
      IllegalStateException - This field is a simple type and cannot be instantiated
    • newInstance

      public T newInstance(Object parent, Object[] args) throws IllegalStateException
      Creates a new instance of the object described by this field.
      Specified by:
      newInstance in class ExtendedFieldHandler<T>
      Parameters:
      parent - The object for which the field is created
      args - the set of constructor arguments
      Returns:
      A new instance of the field's value
      Throws:
      IllegalStateException - This field is a simple type and cannot be instantiated
    • setConvertFrom

      public void setConvertFrom(TypeConvertor convertor)
      Sets the TypeConvertor used during calls to getValue
      Parameters:
      convertor - the TypeConvertor to use during calls to getValue
    • setConvertTo

      public void setConvertTo(TypeConvertor convertor)
      Sets the TypeConvertor used during calls to setValue
      Parameters:
      convertor - the TypeConvertor to use during calls to setValue
    • setCreateMethod

      public void setCreateMethod(Method method) throws MappingException
      Mutator method used by AbstractMappingLoader and Introspector. Please understand how this method is used before you start playing with it! :-)
      Throws:
      MappingException
    • setHasDeleteMethod

      public void setHasDeleteMethod(Method hasMethod, Method deleteMethod) throws MappingException
      Mutator method used by AbstractMappingLoader and Introspector. Please understand how this method is used before you start playing with it! :-)
      Throws:
      MappingException
    • setReadMethod

      public void setReadMethod(Method method) throws MappingException
      Mutator method used by Introspector. Please understand how this method is used before you start playing with it! :-)
      Throws:
      MappingException
    • setWriteMethod

      public void setWriteMethod(Method method) throws MappingException
      Mutator method used by Introspector. Please understand how this method is used before you start playing with it! :-)
      Throws:
      MappingException
    • setAddMethod

      public void setAddMethod(Method method) throws MappingException
      Mutator method used by Introspector. Please understand how this method is used before you start playing with it! :-)
      Throws:
      MappingException
    • setEnumMethod

      public void setEnumMethod(Method method) throws MappingException
      Sets the enumeration method.
      Throws:
      MappingException
    • setIterMethod

      public void setIterMethod(Method method) throws MappingException
      Sets the iteration method.
      Throws:
      MappingException
    • isCollection

      public boolean isCollection()
      Return true if the field is a collection.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • setFieldDescriptor

      public void setFieldDescriptor(FieldDescriptor fieldDesc)
      Sets the FieldDescriptor that this FieldHander is responsibile for. By setting the FieldDescriptor, it allows the implementation of the FieldHandler methods to obtain information about the field itself. This allows a particular implementation to become more generic and reusable.
      Overrides:
      setFieldDescriptor in class AbstractFieldHandler<T>
      Parameters:
      fieldDesc - the FieldDescriptor to set