com.taursys.model
Class PropertyAccessor

java.lang.Object
  extended bycom.taursys.model.PropertyAccessor

public class PropertyAccessor
extends java.lang.Object

This is a helper class which provides access to value object properties. This class is constructed by with the class of the target value object and the name of the target property within the value object. As part of construction, introspection sets up the methods for reading and writing the property.

This class provides two primary methods, getPropertyValue and setPropertyValue which provide access to the property in the given value object. The valueObject passed to these methods must be the same class that was used to create this property accessor.

The readMethods or writeMethod may be null if the valueObject does not have a cooresponding get or set method. This method uses reflection and draws information from the valueObject's BeanInfo and PropertyDescriptors.

The property name should follow JavaBean naming conventions. If the property you want to access is in a nested class, then separate the property names by periods.

Example: propertyName="color": then it expects getColor and setColor will be the method names in the valueObject.

Example: propertyName="address.city": Assumes your value object contains an address property which in turn contains a city property. Accessing the property will result in a call: getAddress().getCity() or getAddress().setCity(...)


Constructor Summary
PropertyAccessor(java.lang.Class valueObjectClass, java.lang.String propertyName)
           
 
Method Summary
 int getJavaDateType()
          Returns the JavaDataType of the property in the valueObject.
protected  java.beans.PropertyDescriptor getProperty(java.lang.Class c, java.lang.String propName)
          Searches the given class for the given property name and returns the PropertyDescriptor.
 java.lang.String getPropertyName()
          Returns the name of the property to access in the valueObjectClass.
 java.lang.Object getPropertyValue(java.lang.Object valueObject)
          Returns a property value from the valueObject based on the propertyName.
protected  java.lang.reflect.Method[] getReadMethods()
          Returns the Methods used to get/read the target property.
 java.lang.Class getValueObjectClass()
          Returns the class of the ValueObject(or parent ValueObject) where the value resides.
protected  java.lang.reflect.Method getWriteMethod()
          Returns the Method used to get/read the target property.
protected  java.lang.Object invokeReadMethod(int i, java.lang.Object target)
          Invokes the i(th) read method on the given target and returns results.
protected  void invokeWriteMethod(java.lang.Object target, java.lang.Object value)
          Invokes the write method on the given target.
protected  boolean isPrimative()
          Indicates that the property is a primative data type.
protected  void setAccessorMethods()
          Sets the javaDataType, readMethods and writeMethod properties.
 void setPropertyValue(java.lang.Object valueObject, java.lang.Object value)
          Sets a property of the valueObject to the given value based on the propertyName.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

PropertyAccessor

public PropertyAccessor(java.lang.Class valueObjectClass,
                        java.lang.String propertyName)
                 throws ModelException
Method Detail

setAccessorMethods

protected void setAccessorMethods()
                           throws ModelException
Sets the javaDataType, readMethods and writeMethod properties. The valueObject and propertyName must be valid before invoking this method. The readMethods or writeMethod may be null if the valueObject does not have a cooresponding get or set method. This method uses reflection and draws information from the valueObject's BeanInfo and PropertyDescriptors.

Throws:
ModelException - if valueObjectClass is null, propertyName is null/blank, property not found in class, or an IntrospectionException occurs.

getProperty

protected java.beans.PropertyDescriptor getProperty(java.lang.Class c,
                                                    java.lang.String propName)
                                             throws ModelPropertyAccessorException
Searches the given class for the given property name and returns the PropertyDescriptor.

Throws:
ModelPropertyAccessorException - if property is not found, or if an IntrospectionException occurs.

getPropertyValue

public java.lang.Object getPropertyValue(java.lang.Object valueObject)
                                  throws ModelException
Returns a property value from the valueObject based on the propertyName.

Returns:
Object obtained from invocation of getter method
Throws:
ModelException - if property has no read method defined or if an IllegalAccessException or IllegalArgumentException occurs.
ModelInvocationTargetException - if the invoked read method throws an Exception.

setPropertyValue

public void setPropertyValue(java.lang.Object valueObject,
                             java.lang.Object value)
                      throws ModelException
Sets a property of the valueObject to the given value based on the propertyName.

Throws:
ModelException - if property has no write method defined or if an IllegalAccessException or IllegalArgumentException occurs.
ModelInvocationTargetException - if the invoked read method throws an Exception.

invokeReadMethod

protected java.lang.Object invokeReadMethod(int i,
                                            java.lang.Object target)
                                     throws ModelException
Invokes the i(th) read method on the given target and returns results.

Returns:
Object obtained from invocation of getter method
Throws:
ModelException - if property has no read method defined or if an IllegalAccessException occurs.
ModelInvocationTargetException - if the invoked read method throws an Exception.

invokeWriteMethod

protected void invokeWriteMethod(java.lang.Object target,
                                 java.lang.Object value)
                          throws ModelException
Invokes the write method on the given target.

Throws:
ModelException - if property has no write method defined or if an IllegalAccessException or IllegalArgumentException occurs.
ModelInvocationTargetException - if the invoked read method throws an Exception.

getJavaDateType

public int getJavaDateType()
Returns the JavaDataType of the property in the valueObject.


getPropertyName

public java.lang.String getPropertyName()
Returns the name of the property to access in the valueObjectClass. This is the property that get/setPropertyValue methods will access. The property name should follow JavaBean naming conventions. If the property you want to access is in a nested class, then separate the property names by periods.

Example: propertyName="color": then it expects getColor and setColor will be the method names in the valueObject.

Example: propertyName="address.city": Assumes your value object contains an address property which in turn contains a city property. Accessing the property will result in a call: getAddress().getCity() or getAddress().setCity(...)


getReadMethods

protected java.lang.reflect.Method[] getReadMethods()
Returns the Methods used to get/read the target property. This value is set by setAccessorMethods which is invoked automatically when this object is created.


getWriteMethod

protected java.lang.reflect.Method getWriteMethod()
Returns the Method used to get/read the target property. This value is set by setAccessorMethods which is invoked automatically when this object is created.


getValueObjectClass

public java.lang.Class getValueObjectClass()
Returns the class of the ValueObject(or parent ValueObject) where the value resides. This is the class that get/setPropertyValue methods will access.


isPrimative

protected boolean isPrimative()
Indicates that the property is a primative data type.

Returns:
true if the property is a primative data type.


Copyright © 2007 Martin T Phelan. All Rights Reserved.