org.pfsw.reflect
Class ObjectOrMapAccessWrapper

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

public class ObjectOrMapAccessWrapper
extends ObjectAccessWrapper

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. However, if the underlying object is an implementor of the interface java.util.Map then getter and setter method access is simulated to access entries in the Map and attribute access by name is also done by accessing Map entries.
The only constraint is, that the keys in the Map are all of type String.


Field Summary
 
Fields inherited from class org.pfsw.reflect.ObjectAccessWrapper
EMPTY_STRING_ARRAY, RU
 
Constructor Summary
ObjectOrMapAccessWrapper(java.lang.Object object)
          Initialize the new instance with an object to wrap.
 
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.
protected  java.util.Map getMap()
           
 java.lang.Object getValueOfField(java.lang.String name)
          Returns the value of the field with the given field name or null in any case of reflection error.
 boolean isMap()
           
 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.
protected  void setIsMap(boolean newValue)
           
 void setObject(java.lang.Object object)
          Set the underlying object.
 void setValueOfField(java.lang.String name, java.lang.Object value)
          Modifies the field with the given name directly to the specified value without calling the setter.
 
Methods inherited from class org.pfsw.reflect.ObjectAccessWrapper
getObject, handleException, makeAccessMethodName, makeGetterName, makeSetterName
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectOrMapAccessWrapper

public ObjectOrMapAccessWrapper(java.lang.Object object)
Initialize the new instance with an object to wrap.

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

isMap

public boolean isMap()

setIsMap

protected void setIsMap(boolean newValue)

getAttributeNames

public java.lang.String[] getAttributeNames()
Returns the names of all attributes that can be accessed by the method getAttributeValue().
If the underlying object is a Map then the names of all current entries are returned.

Specified by:
getAttributeNames in interface AttributeReadAccess
Overrides:
getAttributeNames in class ObjectAccessWrapper

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.
If the underlying object is a Map it returns the value of the entry with the fieldName as key. If no such entry exists, null will be returned.

Overrides:
get in class ObjectAccessWrapper

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.
If the underlying object is a Map it sets the value of the entry with the fieldName as key. If no such entry exists, it will be created.

Overrides:
set in class ObjectAccessWrapper

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
Overrides:
getAttributeValue in class ObjectAccessWrapper
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
Overrides:
setAttributeValue in class ObjectAccessWrapper
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

getValueOfField

public java.lang.Object getValueOfField(java.lang.String name)
Returns the value of the field with the given field name or null in any case of reflection error.
If the underlying object is a Map then this method returns the value of the map entry corresponding to the given name key. If the map does not contain the given name then null will be returned.

Overrides:
getValueOfField in class ObjectAccessWrapper

setValueOfField

public void setValueOfField(java.lang.String name,
                            java.lang.Object value)
Modifies the field with the given name directly to the specified value without calling the setter.
If the underlying object is a Map then this method sets the value of the map entry regardless whether or not the given name key already exists.

Overrides:
setValueOfField in class ObjectAccessWrapper

setObject

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

Overrides:
setObject in class ObjectAccessWrapper
Parameters:
object - The object to be accessed by this wrapper (must not be null)

getMap

protected java.util.Map getMap()