|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Object | +--com.cafesoft.core.util.ClassUtils
Provides methods to perform utility functions on classes.
| 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 |
public ClassUtils()
| Method Detail |
public static Object createInstance(String className)
throws ClassInstantiationException
className - the class name of the class to be instantiated
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.
public static Object createInstance(String className,
Object[] initArgs)
throws ClassInstantiationException
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.
className - the name of the class to instantiate.initArgs - the arguments to pass to the constructor.
ClassInstantiationException - if any errors occurs while creating
the new object.
public static Object createInstance(String className,
Object[] initArgs,
Class[] initArgsTypes)
throws ClassInstantiationException
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.
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.
ClassInstantiationException - if any errors occurs while creating
the new object.
public static Object invokeMethod(String methodName,
Object[] args,
Object invokingObj)
throws MethodInvocationException
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()
}
}
}
methodName - the name of the method to invokeargs - the method argumentsinvokingObj - the object to invoke the method upon
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.
public static Object invokeMethod(Method method,
Object[] args,
Object invokingObj)
throws MethodInvocationException
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()
}
}
}
method - the method object to invokeargs - the method argumentsinvokingObj - the object to invoke the method upon
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
exceptionpublic static String getPackageFilePath(Object object)
Useful for JUnit tests where data files are stored in the same package directory as the test files.
For example,
object - The object to create the package file path for
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||