com.cafesoft.core.util
Class ClassUtils

java.lang.Object
  |
  +--com.cafesoft.core.util.ClassUtils

public class ClassUtils
extends Object

Provides methods to perform utility functions on classes.

Since:
10/18/01

Constructor Summary
ClassUtils()
           
 
Method Summary
static Object createInstance(String className)
          Instantiate a new instance of the specified class name.
static Object createInstance(String className, Object[] initArgs)
          Instantiate a new Instance of the specified class name.
static Object createInstance(String className, Object[] initArgs, Class[] initArgsTypes)
          Instantiate a new Instance of the specified class name.
static String getPackageFilePath(Object object)
          This method creates a package file path.
static Object invokeMethod(Method method, Object[] args, Object invokingObj)
          Invoke the method specified with the given arguments on the given object NOTE: If the method invoked throws an exception, that exception will be the cause of the MethodInvocationException.
static Object invokeMethod(String methodName, Object[] args, Object invokingObj)
          Invoke the method of the given methodName with the given arguments on the given object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClassUtils

public ClassUtils()
Method Detail

createInstance

public static Object createInstance(String className)
                             throws ClassInstantiationException
Instantiate a new instance of the specified class name.

Parameters:
className - the class name of the class to be instantiated
Returns:
an instance of the specified object
Throws:
ClassInstantiationException - thrown if any exceptions occur while instantiating the class. There are several underlying exceptions that are thrown while instantiating classes, this exception will nest them and pass them up the line.

createInstance

public static Object createInstance(String className,
                                    Object[] initArgs)
                             throws ClassInstantiationException
Instantiate a new Instance of the specified class name.

This method will use introspection to discover what constructor to call to create an instance of the specified class. Once discovered, the method will invoke the new constructor and a new instance will be created.

Note: This method will use the runtime classes of the initArg elements and not interfaces or superclasses. If you need this functionality use the constructor below.

Parameters:
className - the name of the class to instantiate.
initArgs - the arguments to pass to the constructor.
Returns:
an instance of the specified object.
Throws:
ClassInstantiationException - if any errors occurs while creating the new object.

createInstance

public static Object createInstance(String className,
                                    Object[] initArgs,
                                    Class[] initArgsTypes)
                             throws ClassInstantiationException
Instantiate a new Instance of the specified class name.

This method will use introspection to discover what constructor to call to create an instance of the specified class. Once discovered, the method will invoke the new constructor and a new instance will be created.

This method will use the initArgsTypes array to match the constructor to use.

Parameters:
className - the name of the class to instantiate.
initArgs - the arguments to pass to the constructor.
initArgsTypes - an array of Class objects that represent the types of the corresponding initArgs elements.
Returns:
an instance of the specified object.
Throws:
ClassInstantiationException - if any errors occurs while creating the new object.

invokeMethod

public static Object invokeMethod(String methodName,
                                  Object[] args,
                                  Object invokingObj)
                           throws MethodInvocationException
Invoke the method of the given methodName with the given arguments on the given object.

NOTE: If the method invoked throws an exception, that exception will be the cause of the MethodInvocationException.

For example:

 public void setName(String name) throws NameException
 {
     ....
      throw new NameException("Blah");
 }
 
now dynamically if we attempt to invoke the method as such,
 try
 {
     invokeMethod("setName", args, obj)
 }
 catch(MethodInvocationException mie)
 {
     if(mie.getCause() != null)
     {
          if(mie.getCause() instanceof NameException)
          {
                  throw mie.getCause()
          }
     }
 }

 

Parameters:
methodName - the name of the method to invoke
args - the method arguments
invokingObj - the object to invoke the method upon
Returns:
the return value of the method invocation, null if the method return type is void
Throws:
MethodInvocationException - throw if an error occurs invoking method. There are several underlying exceptions that can occur while invoking a method so for additional context review the cause of the exception. Thrown if the method name is null, Thrown if the invoking Object is null.

invokeMethod

public static Object invokeMethod(Method method,
                                  Object[] args,
                                  Object invokingObj)
                           throws MethodInvocationException
Invoke the method specified with the given arguments on the given object

NOTE: If the method invoked throws an exception, that exception will be the cause of the MethodInvocationException.

For example:

 public void setName(String name) throws NameException
 {
     ....
 	throw new NameException("Blah");
 }
 
now dynamically if we attempt to invoke the method as such,
 try
 {
     invokeMethod(setNameMethod, args, obj)
 }
 catch(MethodInvocationException mie)
 {
     if(mie.getCause() != null)
 	{
 	    if(mie.getCause() instanceof NameException)
 		{
 		    throw mie.getCause()
 		}
 	}
 }

 

Parameters:
method - the method object to invoke
args - the method arguments
invokingObj - the object to invoke the method upon
Returns:
the return value of the method invocation, null if the method return type is void
Throws:
MethodInvocationException - throw if an error occurs invoking method. There are several underlying exceptions that can occur while invoking a method so for additional context review the cause of the exception

getPackageFilePath

public static String getPackageFilePath(Object object)
This method creates a package file path.

Useful for JUnit tests where data files are stored in the same package directory as the test files.

For example,

  • Fully Qualified Class Name : com.abc.pkg1.ClassName
  • Result of method : com/abc/pkg1

    Parameters:
    object - The object to create the package file path for
    Returns:
    A String containing the package file path


  • Generated on 10:40:31 AM September 10, 2003, © 1996-2003 Cafésoft LLC. All rights reserved.