com.c2_tech.reflect
Class PropertyManager

java.lang.Object
  |
  +--com.c2_tech.reflect.PropertyManager

public class PropertyManager
extends java.lang.Object

This class provides facilities for managing runtime property settings on objects, using the BeanInfo notion of properties. The Introspector class is used to evaluate which properties an object has. The two methods, setViaReflection() and getPropertiesForObject() provide the interfaces to set and get, respectively, property values.

See Also:
Introspector, The information contained in this document is Copyright 2001-2003, Gregg Wonderly, all rights reserved.

Field Summary
protected  boolean cacheBeanInfos
          setCacheBeanInfos property value
protected  java.util.Hashtable infos
          Cached BeanInfos
protected  boolean numAsString
          setNumAsString property value
protected  java.io.PrintWriter out
          setExceptionReportPrintWriter property value
protected  boolean retAllObjects
          setReturnAllObjects property value
 
Constructor Summary
PropertyManager()
           
 
Method Summary
 boolean getCacheBeanInfos()
          Returns the current setting of the cacheBeanInfos property.
 java.io.PrintWriter getExceptionReportPrintWriter()
          Gets the PrintWriter currently be used for exception reporting.
 java.util.Properties getPropertiesForObject(java.lang.Object cls)
          This method uses introspection to find the properties for the passed object and turn them into a Properties object.
 boolean getReturnAllObjects()
          Returns the setting of the returnAllObjects property
 boolean getReturnNumericsAsStrings()
          Returns the setting of the returnNumericsAsStrings property
protected  void reportException(java.lang.reflect.InvocationTargetException ex)
          Return an exception by printing out the stack dump
protected  void reportException(java.lang.RuntimeException ex)
          Return an exception by printing out the stack dump
 void setCacheBeanInfos(boolean how)
          Establishes whether BeanInfo instances returned from the Introspector are cached.
 void setExceptionReportPrintWriter(java.io.PrintWriter repOut)
          If set to a non-null value, then exceptions will be reported via the passed Writer rather than thrown back to the caller.
 void setReturnAllObjects(boolean how)
          Establishes with the returned Property objects will just have Strings (false) or the actual Objects (true).
 void setReturnNumericsAsStrings(boolean how)
          Establishes whether Number.class implementing classes and the native numeric typed property values are converted to strings on return from getPropertiesForObject(), or if they are left as the original Object value returned.
 void setViaReflection(java.lang.Object cls, java.util.Properties props)
          This method takes the passed object and a set of properties and uses reflection to set the property values for the object to the values inside the property object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

numAsString

protected boolean numAsString
setNumAsString property value


retAllObjects

protected boolean retAllObjects
setReturnAllObjects property value


out

protected java.io.PrintWriter out
setExceptionReportPrintWriter property value


cacheBeanInfos

protected boolean cacheBeanInfos
setCacheBeanInfos property value


infos

protected java.util.Hashtable infos
Cached BeanInfos

Constructor Detail

PropertyManager

public PropertyManager()
Method Detail

setCacheBeanInfos

public void setCacheBeanInfos(boolean how)
Establishes whether BeanInfo instances returned from the Introspector are cached. Also, ALWAYS clears the existing cache no matter what the current/previous setting is/was.

Parameters:
how - true to cache BeanInfo objects, false to not.
See Also:
getCacheBeanInfos()

getCacheBeanInfos

public boolean getCacheBeanInfos()
Returns the current setting of the cacheBeanInfos property.

See Also:
setCacheBeanInfos(boolean)

setExceptionReportPrintWriter

public void setExceptionReportPrintWriter(java.io.PrintWriter repOut)
If set to a non-null value, then exceptions will be reported via the passed Writer rather than thrown back to the caller. This will allow processing to continue with the next property instead of causing all remaining property values to be skipped.

Parameters:
repOut - the Writer to write to, or null (default) if exceptions should be thrown back to the caller.
See Also:
getExceptionReportPrintWriter()

getExceptionReportPrintWriter

public java.io.PrintWriter getExceptionReportPrintWriter()
Gets the PrintWriter currently be used for exception reporting.

See Also:
setExceptionReportPrintWriter(PrintWriter)

setReturnAllObjects

public void setReturnAllObjects(boolean how)
Establishes with the returned Property objects will just have Strings (false) or the actual Objects (true). The default is false or to return String values for all native types and Objects for non-native types.

Parameters:
how - Strings (false) or the actual Objects (true)
See Also:
setReturnNumericsAsStrings(boolean), getReturnAllObjects()

getReturnAllObjects

public boolean getReturnAllObjects()
Returns the setting of the returnAllObjects property

See Also:
setReturnAllObjects(boolean)

setReturnNumericsAsStrings

public void setReturnNumericsAsStrings(boolean how)
Establishes whether Number.class implementing classes and the native numeric typed property values are converted to strings on return from getPropertiesForObject(), or if they are left as the original Object value returned.

Parameters:
how - true to get String representations, false to get the Number implementing Objects
See Also:
getReturnNumericsAsStrings()

getReturnNumericsAsStrings

public boolean getReturnNumericsAsStrings()
Returns the setting of the returnNumericsAsStrings property

See Also:
setReturnNumericsAsStrings(boolean)

reportException

protected void reportException(java.lang.reflect.InvocationTargetException ex)
                        throws java.lang.reflect.InvocationTargetException
Return an exception by printing out the stack dump

java.lang.reflect.InvocationTargetException

reportException

protected void reportException(java.lang.RuntimeException ex)
Return an exception by printing out the stack dump


setViaReflection

public void setViaReflection(java.lang.Object cls,
                             java.util.Properties props)
                      throws java.beans.IntrospectionException,
                             java.beans.PropertyVetoException,
                             java.lang.reflect.InvocationTargetException,
                             java.lang.IllegalAccessException
This method takes the passed object and a set of properties and uses reflection to set the property values for the object to the values inside the property object. There are certain conventions supported here. Property values that are String.class typed, are converted to the appropriate type based on the argument type of the setter. Conversion from String.class to any of the native types (boolean,Boolean,int,Integer,short,Short,byte,Byte,char,Char, long,Long) is supported. All other types are passed as is with no coversion. Supporting this is a non-standard use of Property values, and should only be used for property values that remain in memory. The Properties.save() method won't correctly deal with non-String values.

Parameters:
cls - an object to set properties on
props - the property values to set on cls.
Throws:
java.beans.IntrospectionException - if an error occurs during introspection.
java.beans.PropertyVetoException - if a boolean property value is not one of (0,1,"yes","no","true","false")
java.lang.reflect.InvocationTargetException - if there is a problem invoking the property setting method.
java.lang.IllegalAccessException - if the property setting method can not be accessed.
See Also:
getPropertiesForObject(Object), setReturnAllObjects(boolean), getReturnAllObjects()

getPropertiesForObject

public java.util.Properties getPropertiesForObject(java.lang.Object cls)
                                            throws java.beans.IntrospectionException,
                                                   java.lang.reflect.InvocationTargetException,
                                                   java.lang.IllegalAccessException
This method uses introspection to find the properties for the passed object and turn them into a Properties object.

Parameters:
cls - the object to get the properties for
Throws:
java.beans.IntrospectionException - if there are problems accessing the object
java.lang.reflect.InvocationTargetException - if the property reader can not be invoked
java.lang.IllegalAccessException - if the object is not accessible,
See Also:
setViaReflection(Object,Properties)