org.jbeans
Class ReflectionTools

java.lang.Object
  |
  +--org.jbeans.ReflectionTools

public class ReflectionTools
extends Object

This class is a toolkit to help in reflection. All JavaBean related methods and toolkit methods can be found in the com.bp.beans package

Author:
Brian Pontarelli

Constructor Summary
ReflectionTools()
           
 
Method Summary
static Class convertToWrapper(Class type)
          Converts the type given to the correct wrapper class for that type.
static Class findClass(String className, String packageName)
          Convience method that returns class given the package name and className
static Method getMethod(Class klass, String method, Class[] params)
          Returns the given class' method with the name method and the parameters params.
static Method getMethod(Object object, String method, Class[] params)
          Returns the given objects method with the name method and the parameters params.
static Method[] getMethods(Object object)
          Gets all the methods for an object and handles all the nasty exceptions that Java reflection can throw.
static Object instantiate(Class objectClass)
          Yep, another convience method that instantiates an object from a class and this is just like all the others and wraps the exceptions into one nice exception
static Object instantiate(String className)
          A convience method so that a class can be instantiated by name stored in a string without all the exceptions having to be caught.
static Object invokeMethod(Method method, Object object, Object[] params)
          Another convience method because invoking a reflected method is a really pain in the butt.
static boolean isSimpleReturnType(Method method)
          This method makes a really big blanket statement by stating that all simple types (wrapper classe and string) are in java.lang package.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReflectionTools

public ReflectionTools()
Method Detail

findClass

public static Class findClass(String className,
                              String packageName)
                       throws ReflectionException
Convience method that returns class given the package name and className
Parameters:
className - The name of the class (ie Foo)
packageName - (Optional) The name of the package (ie com.xor.util)
Returns:
The Class object for the class if it is found
Throws:
ReflectionException - If the class name is invalid

getMethod

public static Method getMethod(Object object,
                               String method,
                               Class[] params)
                        throws ReflectionException
Returns the given objects method with the name method and the parameters params. This is really just a convience method so that code doesn't have to catch all five thousand exceptions that Java reflection throws and make nifty error messages, this does all of that for you and throws a single, simple exception instead
Parameters:
obejct - The object to get the method from
method - The name of the method to fetch
params - The params to the method (can be null if the method does not take any parameters)
Returns:
The Method
Throws:
ReflectionException - If anything went wrong in the reflection, like wrong arguments to the method or invalid method name. This exception has a very good message body that tells exactly what the hell you screwed up

getMethod

public static Method getMethod(Class klass,
                               String method,
                               Class[] params)
                        throws ReflectionException
Returns the given class' method with the name method and the parameters params. This is really just a convience method so that code doesn't have to catch all five thousand exceptions that Java reflection throws and make nifty error messages, this does all of that for you and throws a single, simple exception instead
Parameters:
klass - The class object to get the method from
method - The name of the method to fetch
params - The params to the method (can be null if the method does not take any parameters)
Returns:
The Method
Throws:
ReflectionException - If anything went wrong in the reflection, like wrong arguments to the method or invalid method name. This exception has a very good message body that tells exactly what the hell you screwed up

getMethods

public static Method[] getMethods(Object object)
                           throws ReflectionException
Gets all the methods for an object and handles all the nasty exceptions that Java reflection can throw. This throws a nice exception
Parameters:
object - The object to get the methods from (only the public ones)
Returns:
The methods
Throws:
ReflectionException - If anything bad happened during reflection

invokeMethod

public static Object invokeMethod(Method method,
                                  Object object,
                                  Object[] params)
                           throws ReflectionException,
                                  RuntimeException,
                                  Error
Another convience method because invoking a reflected method is a really pain in the butt. It can throw like five billion exceptions and really make coding a lame experience. So, I've wrapped all those exceptions up and thrown a nice exception of my own making called ReflectionException. Although, for debugging purposes, this method will re-throw invocation target exceptions of type Error and RuntimeException. For example, NullPointerExceptions and OutOfMemoryError are not wrapped in a ReflectionException. Instead they are re-thrown. All other invocation target exceptions are unwrapped from the InvocationTargetException class and re-wrapped into a new ReflectionException as the target property.
Parameters:
method - The method to invoke
object - The object to invoke the method on
params - The params to the method
Returns:
The return value from the method
Throws:
ReflectionException - If any mishap occurred whilst Reflecting sire. All the exceptions that could be thrown whilst invoking will be wrapped inside the ReflectionException
RuntimeException - If the target of the InvocationTargetException is a RuntimeException, in which case, it is re-thrown
Error - If the target of the InvocationTargetException is an Error, in which case, it is re-thrown

instantiate

public static Object instantiate(Class objectClass)
                          throws ReflectionException
Yep, another convience method that instantiates an object from a class and this is just like all the others and wraps the exceptions into one nice exception
Parameters:
objectClass - The class to instantiate
Returns:
The new instance of the class
Throws:
ReflectionException - If you did something really dumb like put a security restriction on the class or tried to call a class that doesn't have a default constructor of something lame like that.

instantiate

public static Object instantiate(String className)
                          throws ReflectionException
A convience method so that a class can be instantiated by name stored in a string without all the exceptions having to be caught. Instead a single ReflectionException is caught and contains a descriptive message about the error
Parameters:
className - The name of the class to instantiate
Returns:
The new instance of the class
Throws:
ReflectionException - if something went wrong during name lookup or instantiation

isSimpleReturnType

public static boolean isSimpleReturnType(Method method)
This method makes a really big blanket statement by stating that all simple types (wrapper classe and string) are in java.lang package. Although this is true, java.lang also includes non-simple types and therefore this function will fail if the caller passes it a method whose return type is a non-simple type from java.lang
Parameters:
method - The method to check
Returns:
True if the methods return type is either a wrapper class or String

convertToWrapper

public static Class convertToWrapper(Class type)
Converts the type given to the correct wrapper class for that type. If the type given is not a primitive type then null is returned. If the type given is a wrapper class, then null is returned.
Parameters:
type - The type of convert to its wrapper class
Returns:
The wrapper type for the type given or null