texsoft.java.object
Class XsClassInspector

java.lang.Object
  extended bytexsoft.java.object.XsClassInspector

public class XsClassInspector
extends java.lang.Object

Inspects reflection for class/interface and its properties.

Given a class via reflection retrieve properties' types, classes, getters and setters.

Using the getPropertyType(), set() and get() methods it's easy to use reflection to manipulate instance of the class managed by the inspector.

The inspector manage only public setters and getter of the class. If a property has no setter it's considered readonly, if it has no getter it's considered writeonly. If a property has more than one setter for a property only the setter with an argument that exactly match the return type of the getter for that property is considered. The class type of a property is always determined by the return type ot the getter for that property.

This is the core class of the whole texsoft.java.object package.

Creation date: (26/11/2000 19.00.19)


Constructor Summary
XsClassInspector(java.lang.Class type)
          Create a new inspector for class type.
XsClassInspector(java.lang.Object obj)
          Create a new inspector for class to which object obj belongs.
XsClassInspector(java.lang.String name)
          Creates a new inspector for class which name is name.
 
Method Summary
 java.lang.Object get(java.lang.Object obj, java.lang.String prop)
          Returns the value of the property prop of the object obj.
 java.lang.Class getClassType()
          Returns the Class object for the class type handled by the inspector.
 java.lang.String[] getProperties()
          Returns an array of the properties' names.
 int getPropertiesCount()
          Returns the number of properties of the class inspected.
 java.lang.Class getPropertyType(java.lang.String prop)
          Returns the class of the property named prop.
 java.lang.Class[] getSuperClasses()
          Returns an array of the base classes of the class being inspected.
 boolean isReadable(java.lang.String prop)
          Returns true if the property is readable, and can be get.
 boolean isWriteable(java.lang.String prop)
          Returns true if the property is writeable, and can be set.
 java.lang.Object newInstance()
          Create and returns a new instance for the class beeing inspected.
 void set(java.lang.Object obj, java.lang.String prop, boolean v)
          Wrapper method that it's usefull to set a property from a primitive Java type.
 void set(java.lang.Object obj, java.lang.String prop, byte v)
          Wrapper method that it's usefull to set a property from a primitive Java type.
 void set(java.lang.Object obj, java.lang.String prop, char v)
          Wrapper method that it's usefull to set a property from a primitive Java type.
 void set(java.lang.Object obj, java.lang.String prop, double v)
          Wrapper method that it's usefull to set a property from a primitive Java type.
 void set(java.lang.Object obj, java.lang.String prop, float v)
          Wrapper method that it's usefull to set a property from a primitive Java type.
 void set(java.lang.Object obj, java.lang.String prop, int v)
          Wrapper method that it's usefull to set a property from a primitive Java type.
 void set(java.lang.Object obj, java.lang.String prop, long v)
          Wrapper method that it's usefull to set a property from a primitive Java type.
 void set(java.lang.Object obj, java.lang.String prop, java.lang.Object value)
          Set the value of the property prop of the object obj to value.
 void set(java.lang.Object obj, java.lang.String prop, short v)
          Wrapper method that it's usefull to set a property from a primitive Java type.
 java.lang.String toString()
          Returns a string showing the properties of the inspected class (name, type, access).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

XsClassInspector

public XsClassInspector(java.lang.Class type)
Create a new inspector for class type.

Parameters:
type - class to be inspected.

XsClassInspector

public XsClassInspector(java.lang.Object obj)
Create a new inspector for class to which object obj belongs.

Parameters:
obj - object whose class type has to be inspected.

XsClassInspector

public XsClassInspector(java.lang.String name)
                 throws java.lang.ClassNotFoundException
Creates a new inspector for class which name is name.

This constructor should not be used, the others that use a Class or an Object to initialize the inspector must be prefered.

Parameters:
name - Name of the class to be inspected.
Throws:
java.lang.ClassNotFoundException - The exception is thrown if a class with the passed name does not exists.
Method Detail

getClassType

public java.lang.Class getClassType()
Returns the Class object for the class type handled by the inspector.

Returns:
the class inspected by this inspector.

getPropertiesCount

public int getPropertiesCount()
Returns the number of properties of the class inspected.

Returns:
the number of properties of the inspected class.

getProperties

public java.lang.String[] getProperties()
Returns an array of the properties' names.

Returns:
array of property names.

getPropertyType

public final java.lang.Class getPropertyType(java.lang.String prop)
                                      throws XsObjectException
Returns the class of the property named prop.

Parameters:
prop - name of the property.
Returns:
a class object corresponding to the type of the property.
Throws:
XsObjectException - thrown if the property name passed does not match any class' property.

newInstance

public java.lang.Object newInstance()
                             throws XsObjectException
Create and returns a new instance for the class beeing inspected.

Returns:
a new instance of the class inspected.
Throws:
XsObjectException - thrown if a reflection error occurs creating the new instance.

get

public java.lang.Object get(java.lang.Object obj,
                            java.lang.String prop)
                     throws XsObjectException
Returns the value of the property prop of the object obj.

The object obj must be an instace of the class handled by the inspector or an instance of a class that extends the class handled by the inspector.

If the property prop does not esxist or can't be accessed for reading (because it's not public, or has no public getter) an exception will be thrown.

Parameters:
obj - object whose property has to be read.
prop - name of the property to be read.
Returns:
the value of the property being read.
Throws:
XsObjectException - thrown if a reflection exception occurs getting the property value, if the object being read is not or does not extends the inspected class, if the inspected class has not a property with the given name, or if the property can't be read (because it has not a getter or has a not public one).

set

public void set(java.lang.Object obj,
                java.lang.String prop,
                java.lang.Object value)
         throws XsObjectException
Set the value of the property prop of the object obj to value.

The object obj must be an instace of the class handled by the inspector or an instance of a class that extends the class handled by the inspector.

If the property prop does not esxist or can't be accessed for writing (because it's not public, or has no public setter) an exception will be thrown.

This function correctly handle null value, even when the prop is a primitive type: numeric are set to 0, char to '\0' and boolean to false.

If obj is a String and the property type is not a String, the function try to do a conversion from String to the type of the property, using a conversion to primitive type or using constructor with String argument. This allows easy setting of property from String rapresentation of values.

Parameters:
obj - object whose property has to be set.
prop - name of the property to be set.
value - specified the value to be set to the property, can be null.
Throws:
XsObjectException - thrown if a reflection exception occurs setting the property value, if the object being set is not or does not extends the inspected class, if the inspected class has not a property with the given name, or if the property can't be set (because it has not a setter or has a not public one).

set

public void set(java.lang.Object obj,
                java.lang.String prop,
                byte v)
         throws XsObjectException
Wrapper method that it's usefull to set a property from a primitive Java type.

Parameters:
obj - object whose property has to be set.
prop - name of the property to set.
v - value to be assigned to the property.
Throws:
XsObjectException - thrown if a reflection exception occurs setting the property value.
See Also:
set(Object, String, Object)

set

public void set(java.lang.Object obj,
                java.lang.String prop,
                short v)
         throws XsObjectException
Wrapper method that it's usefull to set a property from a primitive Java type.

Parameters:
obj - object whose property has to be set.
prop - name of the property to set.
v - value to be assigned to the property.
Throws:
XsObjectException - thrown if a reflection exception occurs setting the property value.
See Also:
set(Object, String, Object)

set

public void set(java.lang.Object obj,
                java.lang.String prop,
                int v)
         throws XsObjectException
Wrapper method that it's usefull to set a property from a primitive Java type.

Parameters:
obj - object whose property has to be set.
prop - name of the property to set.
v - value to be assigned to the property.
Throws:
XsObjectException - thrown if a reflection exception occurs setting the property value.
See Also:
set(Object, String, Object)

set

public void set(java.lang.Object obj,
                java.lang.String prop,
                long v)
         throws XsObjectException
Wrapper method that it's usefull to set a property from a primitive Java type.

Parameters:
obj - object whose property has to be set.
prop - name of the property to set.
v - value to be assigned to the property.
Throws:
XsObjectException - thrown if a reflection exception occurs setting the property value.
See Also:
set(Object, String, Object)

set

public void set(java.lang.Object obj,
                java.lang.String prop,
                float v)
         throws XsObjectException
Wrapper method that it's usefull to set a property from a primitive Java type.

Parameters:
obj - object whose property has to be set.
prop - name of the property to set.
v - value to be assigned to the property.
Throws:
XsObjectException - thrown if a reflection exception occurs setting the property value.
See Also:
set(Object, String, Object)

set

public void set(java.lang.Object obj,
                java.lang.String prop,
                double v)
         throws XsObjectException
Wrapper method that it's usefull to set a property from a primitive Java type.

Parameters:
obj - object whose property has to be set.
prop - name of the property to set.
v - value to be assigned to the property.
Throws:
XsObjectException - thrown if a reflection exception occurs setting the property value.
See Also:
set(Object, String, Object)

set

public void set(java.lang.Object obj,
                java.lang.String prop,
                char v)
         throws XsObjectException
Wrapper method that it's usefull to set a property from a primitive Java type.

Parameters:
obj - object whose property has to be set.
prop - name of the property to set.
v - value to be assigned to the property.
Throws:
XsObjectException - thrown if a reflection exception occurs setting the property value.
See Also:
set(Object, String, Object)

set

public void set(java.lang.Object obj,
                java.lang.String prop,
                boolean v)
         throws XsObjectException
Wrapper method that it's usefull to set a property from a primitive Java type.

Parameters:
obj - object whose property has to be set.
prop - name of the property to set.
v - value to be assigned to the property.
Throws:
XsObjectException - thrown if a reflection exception occurs setting the property value.
See Also:
set(Object, String, Object)

isReadable

public boolean isReadable(java.lang.String prop)
                   throws XsObjectException
Returns true if the property is readable, and can be get.

Parameters:
prop - name of the property.
Returns:
true if the property is readable, and can be get.
Throws:
XsObjectException - thrown if the property does not exists for the inspected class.

isWriteable

public boolean isWriteable(java.lang.String prop)
                    throws XsObjectException
Returns true if the property is writeable, and can be set.

Parameters:
prop - name of the property.
Returns:
true if the property is writeable, and can be set.
Throws:
XsObjectException - thrown if the property does not exists for the inspected class.

getSuperClasses

public java.lang.Class[] getSuperClasses()
Returns an array of the base classes of the class being inspected.

The classes into the array are ordered from the direct superclass of the class (index 0) to the Object class (last element of the array).

Returns:
array of base classes of the inspected class.

toString

public java.lang.String toString()
Returns a string showing the properties of the inspected class (name, type, access).

Returns:
a string showing the properties of the inspected class (name, type, access).