org.apache.bsf.util
Class ReflectionUtils

java.lang.Object
  extended by org.apache.bsf.util.ReflectionUtils

public class ReflectionUtils
extends Object

This file is a collection of reflection utilities. There are utilities for creating beans, getting bean infos, setting/getting properties, and binding events.

Author:
Sanjiva Weerawarana, Joseph Kesselman

Constructor Summary
ReflectionUtils()
           
 
Method Summary
static void addEventListener(Object source, String eventSetName, EventProcessor processor)
          Add an event processor as a listener to some event coming out of an object.
static Bean createBean(ClassLoader cld, String className, Class[] argTypes, Object[] args)
          Create a bean using given class loader and using the appropriate constructor for the given args of the given arg types.
static Bean createBean(ClassLoader cld, String className, Object[] args)
          Create a bean using given class loader and using the appropriate constructor for the given args.
static Bean getField(Object target, String fieldName)
           
static Bean getProperty(Object target, String propName, Integer index)
          Get a property of a bean.
static void setField(Object target, String fieldName, Bean value, TypeConvertorRegistry tcr)
           
static void setProperty(Object target, String propName, Integer index, Object value, Class valueType, TypeConvertorRegistry tcr)
          Set a property of a bean to a given value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionUtils

public ReflectionUtils()
Method Detail

addEventListener

public static void addEventListener(Object source,
                                    String eventSetName,
                                    EventProcessor processor)
                             throws IntrospectionException,
                                    IllegalArgumentException,
                                    IllegalAccessException,
                                    InstantiationException,
                                    InvocationTargetException
Add an event processor as a listener to some event coming out of an object.

Parameters:
source - event source
eventSetName - name of event set from event src to bind to
processor - event processor the event should be delegated to when it occurs; either via processEvent or processExceptionableEvent.
Throws:
IntrospectionException - if unable to introspect
IllegalArgumentException - if event set is unknown
IllegalAccessException - if the event adapter class or initializer is not accessible.
InstantiationException - if event adapter instantiation fails
InvocationTargetException - if something goes wrong while running add event listener method

createBean

public static Bean createBean(ClassLoader cld,
                              String className,
                              Class[] argTypes,
                              Object[] args)
                       throws ClassNotFoundException,
                              NoSuchMethodException,
                              InstantiationException,
                              IllegalAccessException,
                              IllegalArgumentException,
                              InvocationTargetException,
                              IOException
Create a bean using given class loader and using the appropriate constructor for the given args of the given arg types.

Parameters:
cld - the class loader to use. If null, Class.forName is used.
className - name of class to instantiate
argTypes - array of argument types
args - array of arguments
Returns:
the newly created bean
Throws:
ClassNotFoundException - if class is not loaded
NoSuchMethodException - if constructor can't be found
InstantiationException - if class can't be instantiated
IllegalAccessException - if class is not accessible
IllegalArgumentException - if argument problem
InvocationTargetException - if constructor excepted
IOException - if I/O error in beans.instantiate

createBean

public static Bean createBean(ClassLoader cld,
                              String className,
                              Object[] args)
                       throws ClassNotFoundException,
                              NoSuchMethodException,
                              InstantiationException,
                              IllegalAccessException,
                              IllegalArgumentException,
                              InvocationTargetException,
                              IOException
Create a bean using given class loader and using the appropriate constructor for the given args. Figures out the arg types and calls above.

Parameters:
cld - the class loader to use. If null, Class.forName is used.
className - name of class to instantiate
args - array of arguments
Returns:
the newly created bean
Throws:
ClassNotFoundException - if class is not loaded
NoSuchMethodException - if constructor can't be found
InstantiationException - if class can't be instantiated
IllegalAccessException - if class is not accessible
IllegalArgumentException - if argument problem
InvocationTargetException - if constructor excepted
IOException - if I/O error in beans.instantiate

getField

public static Bean getField(Object target,
                            String fieldName)
                     throws IllegalArgumentException,
                            IllegalAccessException
Throws:
IllegalArgumentException
IllegalAccessException

getProperty

public static Bean getProperty(Object target,
                               String propName,
                               Integer index)
                        throws IntrospectionException,
                               IllegalArgumentException,
                               IllegalAccessException,
                               InvocationTargetException
Get a property of a bean.

Parameters:
target - the object whose prop is to be gotten
propName - name of the property to set
index - index to get (if property is indexed)
Throws:
IntrospectionException - if unable to introspect
IllegalArgumentException - if problems with args: if the property is unknown, or if the property is given an index when its not, or if the property is not writeable, or if the given value cannot be assigned to the it (type mismatch).
IllegalAccessException - if read method is not accessible
InvocationTargetException - if read method excepts

setField

public static void setField(Object target,
                            String fieldName,
                            Bean value,
                            TypeConvertorRegistry tcr)
                     throws IllegalArgumentException,
                            IllegalAccessException
Throws:
IllegalArgumentException
IllegalAccessException

setProperty

public static void setProperty(Object target,
                               String propName,
                               Integer index,
                               Object value,
                               Class valueType,
                               TypeConvertorRegistry tcr)
                        throws IntrospectionException,
                               IllegalArgumentException,
                               IllegalAccessException,
                               InvocationTargetException
Set a property of a bean to a given value.

Parameters:
target - the object whose prop is to be set
propName - name of the property to set
index - index to set (if property is indexed)
value - the property value
valueType - the type of the above (needed when its null)
tcr - type convertor registry to use to convert value type to property type if necessary
Throws:
IntrospectionException - if unable to introspect
IllegalArgumentException - if problems with args: if the property is unknown, or if the property is given an index when its not, or if the property is not writeable, or if the given value cannot be assigned to the it (type mismatch).
IllegalAccessException - if write method is not accessible
InvocationTargetException - if write method excepts