net.xoetrope.xui.helper
Class ReflectionHelper

java.lang.Object
  extended by net.xoetrope.xui.helper.ReflectionHelper

public class ReflectionHelper
extends java.lang.Object

A helper to simplify some of the more common reflection tasks used in XUI

Copyright: Copyright (c) 2004

$Revision: 1.5 $


Field Summary
static int LOG_ERROR_MESSAGE
           
static int PRINT_STACK_TRACE
           
 
Constructor Summary
ReflectionHelper()
           
 
Method Summary
static java.lang.Object constructViaReflection(java.lang.ClassLoader cl, java.lang.Object klass)
          Create a new Object via reflection
static java.lang.Object constructViaReflection(java.lang.ClassLoader cl, java.lang.Object klass, java.lang.Class argClass, java.lang.Object value)
          Create a new Object via reflection
static java.lang.Object constructViaReflection(java.lang.Object klass, java.lang.Class[] params, java.lang.Object[] values)
          Create a new Object via reflection
static java.lang.Object constructViaReflection(java.lang.String klass, java.lang.Class argClass, java.lang.Object value)
          Create a new Object via reflection
static java.lang.Object constructViaReflection(java.lang.String className, java.lang.Object[] values)
          Create a new Object via reflection
static java.lang.Object deserialize(java.lang.Object value)
           
static java.lang.Object[] deserialize(java.lang.Object[] values)
           
static java.lang.Class getAltClass(java.lang.Class objectClass)
          Get an alternative for a class e.g. int.class for Integer.class
static int getErrorMode()
          Get the current error mode
static java.lang.Object getObject(java.lang.String paramType, java.lang.String content)
          Get an object from an XML (config file) description
static java.lang.Class getParamClass(java.lang.String paramType)
          Get the class from a parameter type as it appears in the XML
static java.lang.Object getViaReflection(java.lang.Class klass, java.lang.String methodName, java.lang.Object comp)
          Get a property using refelction
static java.lang.Object getViaReflection(java.lang.String methodName, java.lang.Object comp)
          Get a property using refelction
static java.lang.Object getViaReflection(java.lang.String methodName, java.lang.Object comp, boolean warn)
          Get a property using refelction
static java.lang.Object parseArgument(java.lang.Class param, java.lang.Object value)
          Converts the value passed to the ReflectionHelper to the specified class type if possible.
static void setErrorMode(int m)
          Set the error mode for reflection errors
static void setFieldViaReflection(java.lang.Object clazz, java.lang.String fieldName, java.lang.Object value)
          Set a field property using reflection instead of the property helper.
static void setFieldViaReflection(java.lang.Object clazz, java.lang.String fieldName, java.lang.String value)
          Set a field property using reflection instead of the property helper.
static boolean setViaReflection(java.lang.String methodName, java.lang.Object comp, java.lang.Object value)
          Set a property using reflection instead of the property helper.
static boolean setViaReflection(java.lang.String methodName, java.lang.Object comp, java.lang.Object[] values)
          Set a property using reflection instead of the property helper.
static boolean setViaReflection(java.lang.String methodName, java.lang.Object comp, java.lang.Object value, boolean isBoolean)
          Set a property using reflection instead of the property helper.
static boolean setViaReflection(java.lang.String methodName, java.lang.Object comp, java.lang.Object value, java.lang.Class objectClass)
          Set a property using reflection instead of the property helper.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PRINT_STACK_TRACE

public static final int PRINT_STACK_TRACE
See Also:
Constant Field Values

LOG_ERROR_MESSAGE

public static final int LOG_ERROR_MESSAGE
See Also:
Constant Field Values
Constructor Detail

ReflectionHelper

public ReflectionHelper()
Method Detail

setErrorMode

public static void setErrorMode(int m)
Set the error mode for reflection errors

Parameters:
m - the new mode

getErrorMode

public static int getErrorMode()
Get the current error mode

Returns:
the error mode

getViaReflection

public static java.lang.Object getViaReflection(java.lang.String methodName,
                                                java.lang.Object comp)
Get a property using refelction

Parameters:
methodName - the method name
comp - the component being modified
Returns:
the property value

getViaReflection

public static java.lang.Object getViaReflection(java.lang.String methodName,
                                                java.lang.Object comp,
                                                boolean warn)
Get a property using refelction

Parameters:
methodName - the method name
comp - the component being modified
warn - true to issue a warning if no matching method is found
Returns:
the property value

getViaReflection

public static java.lang.Object getViaReflection(java.lang.Class klass,
                                                java.lang.String methodName,
                                                java.lang.Object comp)
Get a property using refelction

Parameters:
klass - the class whose method is being invoked
methodName - the method name
comp - the component being modified
Returns:
the property value

setViaReflection

public static boolean setViaReflection(java.lang.String methodName,
                                       java.lang.Object comp,
                                       java.lang.Object value)
Set a property using reflection instead of the property helper. This version of the method looks up the available methods and tries to match the method call based upon the supplied arguments. There is some additional overhead in this lookup in comparison to the version where the argument type is supplied.

Parameters:
methodName - the method name
comp - the component being modified
value - the new value
Returns:
true if the property is set OK

setViaReflection

public static boolean setViaReflection(java.lang.String methodName,
                                       java.lang.Object comp,
                                       java.lang.Object[] values)
Set a property using reflection instead of the property helper. This version of the method looks up the available methods and tries to match the method call based upon the supplied arguments. There is some additional overhead in this lookup in comparison to the version where the argument type is supplied.

Parameters:
methodName - the method name
comp - the component being modified
value - the new value
Returns:
true if the property is set OK

parseArgument

public static java.lang.Object parseArgument(java.lang.Class param,
                                             java.lang.Object value)
Converts the value passed to the ReflectionHelper to the specified class type if possible.

Parameters:
param - the class instance required
value - the argument, probably represented as a string
Returns:
the object or null if the conversion/construction cannot complete

setViaReflection

public static boolean setViaReflection(java.lang.String methodName,
                                       java.lang.Object comp,
                                       java.lang.Object value,
                                       boolean isBoolean)
Set a property using reflection instead of the property helper.

Parameters:
methodName - the method name
comp - the component being modified
value - the new value
isBoolean - is it a boolean property being modified
Returns:
true if the property is set OK

setViaReflection

public static boolean setViaReflection(java.lang.String methodName,
                                       java.lang.Object comp,
                                       java.lang.Object value,
                                       java.lang.Class objectClass)
Set a property using reflection instead of the property helper.

Parameters:
methodName - the method name
comp - the component being modified
value - the new value
objectClass - the class of the argument of the set method being invoked
Returns:
true if the property is set OK

setFieldViaReflection

public static void setFieldViaReflection(java.lang.Object clazz,
                                         java.lang.String fieldName,
                                         java.lang.String value)
Set a field property using reflection instead of the property helper.

Parameters:
clazz - the object instance whose property we want to set
fieldName - the name of the field to set
value - the new value

setFieldViaReflection

public static void setFieldViaReflection(java.lang.Object clazz,
                                         java.lang.String fieldName,
                                         java.lang.Object value)
Set a field property using reflection instead of the property helper.

Parameters:
clazz - the object instance whose property we want to set
fieldName - the name of the field to set
value - the new value

constructViaReflection

public static java.lang.Object constructViaReflection(java.lang.String className,
                                                      java.lang.Object[] values)
Create a new Object via reflection

Parameters:
className - the class to construct
values - an array of arguments used by the constructor
Returns:
the new object

constructViaReflection

public static java.lang.Object constructViaReflection(java.lang.Object klass,
                                                      java.lang.Class[] params,
                                                      java.lang.Object[] values)
Create a new Object via reflection

Parameters:
params - the types used by the constructor
klass - the class to construct or the classname
values - an array of arguments used by the constructor
Returns:
the new object

constructViaReflection

public static java.lang.Object constructViaReflection(java.lang.String klass,
                                                      java.lang.Class argClass,
                                                      java.lang.Object value)
Create a new Object via reflection

Parameters:
klass - the class to construct or the classname
argClass - the argument type used by the constructor
value - an argument used by the constructor
Returns:
the new object

constructViaReflection

public static java.lang.Object constructViaReflection(java.lang.ClassLoader cl,
                                                      java.lang.Object klass,
                                                      java.lang.Class argClass,
                                                      java.lang.Object value)
Create a new Object via reflection

Parameters:
cl - the class loader to use to located the class, or null to use Class.forName to locate the class
klass - the class to construct or the class name
argClass - the argument type used by the constructor
value - an argument used by the constructor
Returns:
the new object

constructViaReflection

public static java.lang.Object constructViaReflection(java.lang.ClassLoader cl,
                                                      java.lang.Object klass)
Create a new Object via reflection

Parameters:
cl - the class loader to use to located the class, or null to use Class.forName to locate the class
klass - the class to construct or the classname
Returns:
the new object

getAltClass

public static java.lang.Class getAltClass(java.lang.Class objectClass)
Get an alternative for a class e.g. int.class for Integer.class

Parameters:
objectClass - the class of the argument
Returns:
the alternative class, or null if there is no suitable class.

getParamClass

public static java.lang.Class getParamClass(java.lang.String paramType)
Get the class from a parameter type as it appears in the XML

Parameters:
paramType - the type name
Returns:
the class of the paramenter type

getObject

public static java.lang.Object getObject(java.lang.String paramType,
                                         java.lang.String content)
Get an object from an XML (config file) description

Parameters:
paramType - the class name/type
content - the content of the object
Returns:
the new object

deserialize

public static java.lang.Object deserialize(java.lang.Object value)

deserialize

public static java.lang.Object[] deserialize(java.lang.Object[] values)