net.wotonomy.foundation.internal
Class Introspector

java.lang.Object
  extended by net.wotonomy.foundation.internal.Introspector

public class Introspector
extends java.lang.Object

This Introspector is a static utility class written to work around limitations in PropertyDescriptor and Introspector.

Of particular note are the get() and set() methods, which will attempt to get and set artibrary values on arbitrary objects to the best of its ability, converting values as appropriate. Properties of the form "property.nestedproperty.anotherproperty" are supported to get and set values on property values directly.

Note that for naming getter methods, this class supports "get", "is", and also the property name itself, which supports NeXT-style properties. Introspector supports Maps by treating the keys a property names, supports Lists by treating the indexes as property names.

Numeric and boolean types can be inverted by prepending a "!" before the name of the property, like "manager.!active" or "task.!lag".

Version:
$Revision: 893 $
Author:
michael@mpowers.net, $Author: cgruber $

Field Summary
static java.lang.String SEPARATOR
           
static boolean strict
           
static java.lang.Class WILD
           
 
Constructor Summary
Introspector()
           
 
Method Summary
static java.lang.Object get(java.lang.Object anObject, java.lang.String aProperty)
          Gets a value from an object or any of its child objects.
static java.lang.reflect.Method getPropertyReadMethod(java.lang.Class objectClass, java.lang.String aProperty, java.lang.Class[] paramTypes)
          Utility method to get the read method for a property belonging to a class.
static java.lang.reflect.Method getPropertyReadMethod(java.lang.Class objectClass, java.lang.String aProperty, java.lang.Object[] params)
          Utility method to get a method for a property belonging to a class.
static java.lang.reflect.Method getPropertyWriteMethod(java.lang.Class objectClass, java.lang.String aProperty, java.lang.Class[] paramTypes)
          Utility method to get the write method for a property belonging to a class.
static java.lang.reflect.Method getPropertyWriteMethod(java.lang.Class objectClass, java.lang.String aProperty, java.lang.Object[] params)
          Utility method to get a method for a property belonging to a class.
static java.lang.String[] getReadPropertiesForClass(java.lang.Class objectClass)
          Gets a list of the readable properties for the given class.
static java.lang.String[] getReadPropertiesForObject(java.lang.Object anObject)
          Gets a list of the readable properties for the given object, which may not be null.
static java.lang.Object getValueForObject(java.lang.Object anObject, java.lang.String aProperty)
          Convenience to get a value for a property from an object.
static java.lang.String[] getWritePropertiesForClass(java.lang.Class objectClass)
          Gets a list of the writable properties for the given class.
static java.lang.String[] getWritePropertiesForObject(java.lang.Object anObject)
          Gets a list of the writable properties for the given object, which may not be null.
static java.lang.Object set(java.lang.Object anObject, java.lang.String aProperty, java.lang.Object aValue)
          Sets a value in an object or any of its child objects.
 void setDebug(boolean isDebug)
          If set to true, exceptions printed to System.out.println.
static java.lang.Object setValueForObject(java.lang.Object anObject, java.lang.String aProperty, java.lang.Object aValue)
          Convenience to set a value for a property from an object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

strict

public static boolean strict

SEPARATOR

public static final java.lang.String SEPARATOR
See Also:
Constant Field Values

WILD

public static java.lang.Class WILD
Constructor Detail

Introspector

public Introspector()
Method Detail

getPropertyReadMethod

public static java.lang.reflect.Method getPropertyReadMethod(java.lang.Class objectClass,
                                                             java.lang.String aProperty,
                                                             java.lang.Class[] paramTypes)
Utility method to get the read method for a property belonging to a class. Will search for methods in the form of "getProperty" and failing that "isProperty" (to handle booleans).

Parameters:
objectClass - the class whose property methods will be retrieved.
aProperty - The property whose method will be retrieved.
paramTypes - An array of class objects representing the types of parameters.
Returns:
The appropriate method for the class, or null if not found.

getPropertyWriteMethod

public static java.lang.reflect.Method getPropertyWriteMethod(java.lang.Class objectClass,
                                                              java.lang.String aProperty,
                                                              java.lang.Class[] paramTypes)
Utility method to get the write method for a property belonging to a class. Will search for methods in the form of "setProperty".

Parameters:
objectClass - the class whose property methods will be retrieved.
aProperty - The property whose method will be retrieved.
paramTypes - An array of class objects representing the types of parameters.
Returns:
The appropriate method for the class, or null if not found.

getPropertyReadMethod

public static java.lang.reflect.Method getPropertyReadMethod(java.lang.Class objectClass,
                                                             java.lang.String aProperty,
                                                             java.lang.Object[] params)
Utility method to get a method for a property belonging to a class. Use this if you don't feel like making the Class array from the parameters you will be using - pass in the parameters themselves.

Parameters:
objectClass - the Class whose property methods will be retrieved.
aProperty - The property whose method will be retrieved.
params - An array of parameters to be used.
Returns:
The appropriate method for the class, or null if not found.

getPropertyWriteMethod

public static java.lang.reflect.Method getPropertyWriteMethod(java.lang.Class objectClass,
                                                              java.lang.String aProperty,
                                                              java.lang.Object[] params)
Utility method to get a method for a property belonging to a class. Use this if you don't feel like making the Class array from the parameters you will be using - pass in the parameters themselves.

Parameters:
objectClass - the Class whose property methods will be retrieved.
aProperty - The property whose method will be retrieved.
params - An array of parameters to be used.
Returns:
The appropriate method for the class, or null if not found.

getReadPropertiesForClass

public static java.lang.String[] getReadPropertiesForClass(java.lang.Class objectClass)
Gets a list of the readable properties for the given class. Note that readable properties may not be writable - see getWriteProperties().

Returns:
An array of property names in no particular order where each name is a string with the first character in lower case.

getWritePropertiesForClass

public static java.lang.String[] getWritePropertiesForClass(java.lang.Class objectClass)
Gets a list of the writable properties for the given class. Note that writable properties may not be writable - see getReadProperties().

Returns:
An array of property names in no particular order where each name is a string with the first character in lower case.

getReadPropertiesForObject

public static java.lang.String[] getReadPropertiesForObject(java.lang.Object anObject)
Gets a list of the readable properties for the given object, which may not be null. This method is more useful than getReadPropertiesForClass in that Maps will return their keys as properties and Lists will return their element indices as properties. Note that readable properties may not be writable - see getWriteProperties().

Returns:
An array of property names in no particular order where each name is a string with the first character in lower case.

getWritePropertiesForObject

public static java.lang.String[] getWritePropertiesForObject(java.lang.Object anObject)
Gets a list of the writable properties for the given object, which may not be null. This method is more useful than getWritePropertiesForClass in that Maps will return their keys as properties and Lists will return their element indices as properties. Note that writable properties may not be writable - see getReadProperties().

Returns:
An array of property names in no particular order where each name is a string with the first character in lower case.

getValueForObject

public static java.lang.Object getValueForObject(java.lang.Object anObject,
                                                 java.lang.String aProperty)
Convenience to get a value for a property from an object. An empty property string is considered the identity property and simply returns the object.

Throws:
MissingPropertyException - if the property cannot be found on the object.

setValueForObject

public static java.lang.Object setValueForObject(java.lang.Object anObject,
                                                 java.lang.String aProperty,
                                                 java.lang.Object aValue)
Convenience to set a value for a property from an object. Returns the return value from executing the specified method, or null if the method returns type void.

Throws:
MissingPropertyException - if the property cannot be found on the object.
NullPrimitiveException - if the property is of primitive type and the value is null.

get

public static java.lang.Object get(java.lang.Object anObject,
                                   java.lang.String aProperty)
Gets a value from an object or any of its child objects. This will parse the property string for "."'s and get values for each successive object's property in the path. An empty property string is considered the identity property and simply returns the object.


set

public static java.lang.Object set(java.lang.Object anObject,
                                   java.lang.String aProperty,
                                   java.lang.Object aValue)
Sets a value in an object or any of its child objects. This will parse the property string for "."'s and set values for each successive object's property in the path.

If a property is not found, this method will try to implicitly create hash maps (if possible) to fill out the path. This is useful when dealing with trees of nested maps.


setDebug

public void setDebug(boolean isDebug)
If set to true, exceptions printed to System.out.println. Defaults to true.



Copyright © 2006 null. All Rights Reserved.