Package org.apache.bsf.util
Class ReflectionUtils
- java.lang.Object
-
- org.apache.bsf.util.ReflectionUtils
-
public class ReflectionUtils extends java.lang.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
Constructors Constructor Description ReflectionUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addEventListener(java.lang.Object source, java.lang.String eventSetName, EventProcessor processor)
Add an event processor as a listener to some event coming out of an object.static Bean
createBean(java.lang.ClassLoader cld, java.lang.String className, java.lang.Class[] argTypes, java.lang.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(java.lang.ClassLoader cld, java.lang.String className, java.lang.Object[] args)
Create a bean using given class loader and using the appropriate constructor for the given args.static Bean
getField(java.lang.Object target, java.lang.String fieldName)
static Bean
getProperty(java.lang.Object target, java.lang.String propName, java.lang.Integer index)
Get a property of a bean.static void
setField(java.lang.Object target, java.lang.String fieldName, Bean value, TypeConvertorRegistry tcr)
static void
setProperty(java.lang.Object target, java.lang.String propName, java.lang.Integer index, java.lang.Object value, java.lang.Class valueType, TypeConvertorRegistry tcr)
Set a property of a bean to a given value.
-
-
-
Method Detail
-
addEventListener
public static void addEventListener(java.lang.Object source, java.lang.String eventSetName, EventProcessor processor) throws java.beans.IntrospectionException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.InstantiationException, java.lang.reflect.InvocationTargetException
Add an event processor as a listener to some event coming out of an object.- Parameters:
source
- event sourceeventSetName
- name of event set from event src to bind toprocessor
- event processor the event should be delegated to when it occurs; either via processEvent or processExceptionableEvent.- Throws:
java.beans.IntrospectionException
- if unable to introspectjava.lang.IllegalArgumentException
- if event set is unknownjava.lang.IllegalAccessException
- if the event adapter class or initializer is not accessible.java.lang.InstantiationException
- if event adapter instantiation failsjava.lang.reflect.InvocationTargetException
- if something goes wrong while running add event listener method
-
createBean
public static Bean createBean(java.lang.ClassLoader cld, java.lang.String className, java.lang.Class[] argTypes, java.lang.Object[] args) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException, java.io.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 instantiateargTypes
- array of argument typesargs
- array of arguments- Returns:
- the newly created bean
- Throws:
java.lang.ClassNotFoundException
- if class is not loadedjava.lang.NoSuchMethodException
- if constructor can't be foundjava.lang.InstantiationException
- if class can't be instantiatedjava.lang.IllegalAccessException
- if class is not accessiblejava.lang.IllegalArgumentException
- if argument problemjava.lang.reflect.InvocationTargetException
- if constructor exceptedjava.io.IOException
- if I/O error in beans.instantiate
-
createBean
public static Bean createBean(java.lang.ClassLoader cld, java.lang.String className, java.lang.Object[] args) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.InstantiationException, java.lang.IllegalAccessException, java.lang.IllegalArgumentException, java.lang.reflect.InvocationTargetException, java.io.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 instantiateargs
- array of arguments- Returns:
- the newly created bean
- Throws:
java.lang.ClassNotFoundException
- if class is not loadedjava.lang.NoSuchMethodException
- if constructor can't be foundjava.lang.InstantiationException
- if class can't be instantiatedjava.lang.IllegalAccessException
- if class is not accessiblejava.lang.IllegalArgumentException
- if argument problemjava.lang.reflect.InvocationTargetException
- if constructor exceptedjava.io.IOException
- if I/O error in beans.instantiate
-
getField
public static Bean getField(java.lang.Object target, java.lang.String fieldName) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException
- Throws:
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
-
getProperty
public static Bean getProperty(java.lang.Object target, java.lang.String propName, java.lang.Integer index) throws java.beans.IntrospectionException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Get a property of a bean.- Parameters:
target
- the object whose prop is to be gottenpropName
- name of the property to setindex
- index to get (if property is indexed)- Throws:
java.beans.IntrospectionException
- if unable to introspectjava.lang.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).java.lang.IllegalAccessException
- if read method is not accessiblejava.lang.reflect.InvocationTargetException
- if read method excepts
-
setField
public static void setField(java.lang.Object target, java.lang.String fieldName, Bean value, TypeConvertorRegistry tcr) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException
- Throws:
java.lang.IllegalArgumentException
java.lang.IllegalAccessException
-
setProperty
public static void setProperty(java.lang.Object target, java.lang.String propName, java.lang.Integer index, java.lang.Object value, java.lang.Class valueType, TypeConvertorRegistry tcr) throws java.beans.IntrospectionException, java.lang.IllegalArgumentException, java.lang.IllegalAccessException, java.lang.reflect.InvocationTargetException
Set a property of a bean to a given value.- Parameters:
target
- the object whose prop is to be setpropName
- name of the property to setindex
- index to set (if property is indexed)value
- the property valuevalueType
- 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:
java.beans.IntrospectionException
- if unable to introspectjava.lang.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).java.lang.IllegalAccessException
- if write method is not accessiblejava.lang.reflect.InvocationTargetException
- if write method excepts
-
-