org.pfsw.reflect
Class ObjectAccessWrapper

java.lang.Object
  extended by org.pfsw.reflect.ObjectAccessWrapper
All Implemented Interfaces:
AttributeReadAccess, AttributeReadWriteAccess
Direct Known Subclasses:
ObjectOrMapAccessWrapper

public class ObjectAccessWrapper
extends java.lang.Object
implements AttributeReadWriteAccess

A wrapper that is capable to read and write object fields via getter and setter methods or directly. The access is possible for all visibilities.


Field Summary
protected static java.lang.String[] EMPTY_STRING_ARRAY
           
protected static ReflectUtil RU
           
 
Constructor Summary
ObjectAccessWrapper(java.lang.Object object)
          Initialize the new instance with an object.
 
Method Summary
 java.lang.Object get(java.lang.String fieldName)
          Returns the return value of the getter method of the given field name or null in any case of reflection problem.
 java.lang.String[] getAttributeNames()
          Returns the names of all attributes that can be accessed by the method getAttributeValue().
 java.lang.Object getAttributeValue(java.lang.String name)
          Returns the current value of the attribute (field) with the given name.
 java.lang.Object getObject()
           
 java.lang.Object getValueOfField(java.lang.String fieldName)
          Returns the value of the field with the given field name or null in any case of reflection error.
protected  void handleException(java.lang.Exception e)
          Handles all exceptions that might occur due to reflection access.
protected  java.lang.String makeAccessMethodName(java.lang.String prefix, java.lang.String fieldName)
           
protected  java.lang.String makeGetterName(java.lang.String fieldName)
           
protected  java.lang.String makeSetterName(java.lang.String fieldName)
           
 void set(java.lang.String fieldName, java.lang.Object value)
          Invokes the setter method of the given field name and passes the specified value as parameter to it.
 void setAttributeValue(java.lang.String name, java.lang.Object value)
          Sets the current value of the attribute (field) with the given name.
 void setObject(java.lang.Object object)
          Set the underlying object.
 void setValueOfField(java.lang.String fieldName, java.lang.Object value)
          Modifies the field with the given name directly to the specified value without calling the setter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_STRING_ARRAY

protected static final java.lang.String[] EMPTY_STRING_ARRAY

RU

protected static final ReflectUtil RU
Constructor Detail

ObjectAccessWrapper

public ObjectAccessWrapper(java.lang.Object object)
Initialize the new instance with an object.

Parameters:
object - The object to be accessed by this wrapper (must not be null)
Method Detail

get

public java.lang.Object get(java.lang.String fieldName)
Returns the return value of the getter method of the given field name or null in any case of reflection problem.


set

public void set(java.lang.String fieldName,
                java.lang.Object value)
Invokes the setter method of the given field name and passes the specified value as parameter to it.


getValueOfField

public java.lang.Object getValueOfField(java.lang.String fieldName)
Returns the value of the field with the given field name or null in any case of reflection error.


setValueOfField

public void setValueOfField(java.lang.String fieldName,
                            java.lang.Object value)
Modifies the field with the given name directly to the specified value without calling the setter.


getAttributeValue

public java.lang.Object getAttributeValue(java.lang.String name)
                                   throws java.lang.NoSuchFieldException
Returns the current value of the attribute (field) with the given name.

Specified by:
getAttributeValue in interface AttributeReadAccess
Parameters:
name - The attribute's name ( case sensitive )
Throws:
java.lang.NoSuchFieldException - If there is no attribute with the given name

setAttributeValue

public void setAttributeValue(java.lang.String name,
                              java.lang.Object value)
                       throws java.lang.NoSuchFieldException
Sets the current value of the attribute (field) with the given name.

Specified by:
setAttributeValue in interface AttributeReadWriteAccess
Parameters:
name - The attribute's name ( case sensitive )
value - The value to be put into the attribute's 'slot'
Throws:
java.lang.NoSuchFieldException - If there is no attribute with the given name

getAttributeNames

public java.lang.String[] getAttributeNames()
Returns the names of all attributes that can be accessed by the method getAttributeValue().

Specified by:
getAttributeNames in interface AttributeReadAccess

getObject

public java.lang.Object getObject()

setObject

public void setObject(java.lang.Object object)
Set the underlying object.

Parameters:
object - The object to be accessed by this wrapper (must not be null)

makeGetterName

protected java.lang.String makeGetterName(java.lang.String fieldName)

makeSetterName

protected java.lang.String makeSetterName(java.lang.String fieldName)

makeAccessMethodName

protected java.lang.String makeAccessMethodName(java.lang.String prefix,
                                                java.lang.String fieldName)

handleException

protected void handleException(java.lang.Exception e)
Handles all exceptions that might occur due to reflection access. Subclasses may override this method to do some logging.