texsoft.java.object
Class XsObjectWithReflection

java.lang.Object
  extended bytexsoft.java.object.XsObjectWithReflection
Direct Known Subclasses:
XsObjectSerializable

public class XsObjectWithReflection
extends java.lang.Object

Add easy reflection support to derived classes.

Classes that extends XsObjectWithReflection can access their properties via reflection easily and without need to write complex code that use java.lang.reflect .

doGet(String prop) returns the value of property prop.

doSet(String prop, Object v) sets the value of property prop to v.

doNew() creates and returns a new instance for the class.

There is also a group of wrapper methods doSet(String prop, ...) that accept as value primitive types such as int, long, boolean, etc ...

The inspect() method return the XsClassInspector object for the class. It is usefull to obtain more detailed information on properties.

Using the applyInterfaceFilter(Class) method the object reflection methods doSet(), doGet() and inspect() are filtered to the public setter/getter of the interface. The interface passed must be implemented by the class. The doNew() method creates an instance of the class, ignoring the filter applied.

The constructor that has a Class parameters allow to apply an interface for filtering as using the applyInterfaceFilter(Class) method.

See Also:
XsClassInspector

Constructor Summary
XsObjectWithReflection()
          Create a new instance of the class.
XsObjectWithReflection(java.lang.Class iface)
          Create a new instance of the class, filtering inspected properties on the passed interface.
 
Method Summary
 void applyInterfaceFilter(java.lang.Class iface)
          Apply an interface that acts as a filter for inpected properties.
 void applyInterfaceFilter(java.lang.String ifaceName)
          Apply an interface that acts as a filter for inpected properties.
 java.lang.Object doGet(java.lang.String p)
          Returns the value of the property whose name is p.
 XsObjectWithReflection doNew()
          This method create and return a new instance for the class.
 void doSet(java.lang.String p, boolean v)
          Sets the property named p to the value v.
 void doSet(java.lang.String p, byte v)
          Sets the property named p to the value v.
 void doSet(java.lang.String p, char v)
          Sets the property named p to the value v.
 void doSet(java.lang.String p, double v)
          Sets the property named p to the value v.
 void doSet(java.lang.String p, float v)
          Sets the property named p to the value v.
 void doSet(java.lang.String p, int v)
          Sets the property named p to the value v.
 void doSet(java.lang.String p, long v)
          Sets the property named p to the value v.
 void doSet(java.lang.String p, java.lang.Object v)
          Assigns v to the property whose name is p.
 void doSet(java.lang.String p, short v)
          Sets the property named p to the value v.
 XsClassInspector inspect()
          Returns the class inspector of this object.
 void removeInterfaceFilter()
          Removes the interface filter for properties previously applied.
 java.lang.String toString()
          Override default toString method to return classname and properties mapped by the inspector for this class.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XsObjectWithReflection

public XsObjectWithReflection()
                       throws XsObjectException
Create a new instance of the class.

Transparently acquires from XsInspectionFactory the inspector for the class beeing instantiated.

Throws:
XsObjectException - thrown if can't acquire the inspector for the class

XsObjectWithReflection

public XsObjectWithReflection(java.lang.Class iface)
                       throws XsObjectException
Create a new instance of the class, filtering inspected properties on the passed interface.

Query from XsInspectionFactory the inspector for the interface passed. The interface must be implemented by the class. This constructor is used to reduce the visibility of properties accessed by the inspection system based on an interface that the class implements. Currently the class properties can be filtered only on one interface.

Parameters:
iface - specifies the interface on which the class inspected properties are to be filtered on
Throws:
XsObjectException - thrown if can't acquire the inspector for the class or the interface
Method Detail

applyInterfaceFilter

public void applyInterfaceFilter(java.lang.Class iface)
                          throws XsObjectException
Apply an interface that acts as a filter for inpected properties.

The interface passed must be implemented by this class. This method can be called many time, with different interfaces as argument, if it's needed to filter the inspected properties on different interfaces of the calss in different moments.

Parameters:
iface - specifies the interface on which the class inspected properties are to be filtered on
Throws:
XsObjectException - thrown if can't acquire the inspector for the class or the interface

applyInterfaceFilter

public void applyInterfaceFilter(java.lang.String ifaceName)
                          throws XsObjectException
Apply an interface that acts as a filter for inpected properties.

The interface passed must be implemented by this class. This method can be called many time, with different interfaces as argument, if it's needed to filter the inspected properties on different interfaces of the calss in different moments.

Parameters:
ifaceName - specifies the name of the interface on which the class inspected properties are to be filtered on
Throws:
XsObjectException - thrown if can't acquire the inspector for the class or the interface or if no interface match the passed interface name

removeInterfaceFilter

public void removeInterfaceFilter()
Removes the interface filter for properties previously applied. If the object has not an interface filter does nothing.


doGet

public final java.lang.Object doGet(java.lang.String p)
                             throws XsObjectException
Returns the value of the property whose name is p. Invoke the getter with the class inspector. See XsClassInspector.get(Object, String).

Parameters:
p - name of the property whose value has to be returned.
Returns:
Object - value of property named p.
Throws:
XsObjectException - thrown if a reflection or class type error occurs while getting the property value

doSet

public final void doSet(java.lang.String p,
                        java.lang.Object v)
                 throws XsObjectException
Assigns v to the property whose name is p.

Invokes the setter with class inspector for property p, setting value v.

Parameters:
p - name of the property to be set.
v - value to be assigned to the property.
Throws:
XsObjectException - thrown if a reflection or class type error occurs while setting the property value
See Also:
XsClassInspector.set(Object, String, Object)

doNew

public final XsObjectWithReflection doNew()
                                   throws XsObjectException
This method create and return a new instance for the class.

Returns:
A new instance of the class.
Throws:
XsObjectException - Thrown if a reflection error occurs while creating the new instance.

inspect

public final XsClassInspector inspect()
Returns the class inspector of this object.

Usefull for requesting properties list, and other reflection services.

Returns:
XsClassInspector of this object.

doSet

public final void doSet(java.lang.String p,
                        byte v)
                 throws XsObjectException
Sets the property named p to the value v.

Parameters:
p - name of the property
v - value to be assigned to the property
Throws:
XsObjectException - thrown if a reflection error occurs while setting the property value.
See Also:
doSet(String, Object)

doSet

public final void doSet(java.lang.String p,
                        short v)
                 throws XsObjectException
Sets the property named p to the value v.

Parameters:
p - name of the property
v - value to be assigned to the property
Throws:
XsObjectException - thrown if a reflection error occurs while setting the property value.
See Also:
doSet(String, Object)

doSet

public final void doSet(java.lang.String p,
                        int v)
                 throws XsObjectException
Sets the property named p to the value v.

Parameters:
p - name of the property
v - value to be assigned to the property
Throws:
XsObjectException - thrown if a reflection error occurs while setting the property value.
See Also:
doSet(String, Object)

doSet

public final void doSet(java.lang.String p,
                        long v)
                 throws XsObjectException
Sets the property named p to the value v.

Parameters:
p - name of the property
v - value to be assigned to the property
Throws:
XsObjectException - thrown if a reflection error occurs while setting the property value.
See Also:
doSet(String, Object)

doSet

public final void doSet(java.lang.String p,
                        float v)
                 throws XsObjectException
Sets the property named p to the value v.

Parameters:
p - name of the property
v - value to be assigned to the property
Throws:
XsObjectException - thrown if a reflection error occurs while setting the property value.
See Also:
doSet(String, Object)

doSet

public final void doSet(java.lang.String p,
                        double v)
                 throws XsObjectException
Sets the property named p to the value v.

Parameters:
p - name of the property
v - value to be assigned to the property
Throws:
XsObjectException - thrown if a reflection error occurs while setting the property value.
See Also:
doSet(String, Object)

doSet

public final void doSet(java.lang.String p,
                        char v)
                 throws XsObjectException
Sets the property named p to the value v.

Parameters:
p - name of the property
v - value to be assigned to the property
Throws:
XsObjectException - thrown if a reflection error occurs while setting the property value.
See Also:
doSet(String, Object)

doSet

public final void doSet(java.lang.String p,
                        boolean v)
                 throws XsObjectException
Sets the property named p to the value v.

Parameters:
p - name of the property
v - value to be assigned to the property
Throws:
XsObjectException - thrown if a reflection error occurs while setting the property value.
See Also:
doSet(String, Object)

toString

public java.lang.String toString()
Override default toString method to return classname and properties mapped by the inspector for this class.

Returns:
string rapresentation fo the object