org.apache.ojb.broker.util
Class ClassHelper

java.lang.Object
  extended by org.apache.ojb.broker.util.ClassHelper

public class ClassHelper
extends Object

Helper class with static methods for java class, method, and field handling.

Version:
$Id: ClassHelper.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $

Method Summary
static Object buildNewObjectInstance(ClassDescriptor cld)
          Builds a new instance for the class represented by the given class descriptor.
static Class getClass(String name)
          Convenience method for getClass(name, true)
static Class getClass(String className, boolean initialize)
          Retrieves the class object for the given qualified class name.
static ClassLoader getClassLoader()
          Returns the class loader currently used by OJB.
static Field getField(Class clazz, String fieldName)
          Determines the field via reflection look-up.
static Method getMethod(Class clazz, String methodName, Class[] params)
          Determines the method with the specified signature via reflection look-up.
static Method getMethod(Object object, String methodName, Class[] params)
          Determines the method with the specified signature via reflection look-up.
static Method getMethod(String className, String methodName, Class[] params)
          Determines the method with the specified signature via reflection look-up.
static URL getResource(String name)
          Determines the url of the indicated resource using the currently set class loader.
static Object newInstance(Class target)
          Returns a new instance of the given class, using the default or a no-arg constructor.
static Object newInstance(Class target, boolean makeAccessible)
          Returns a new instance of the given class, using the default or a no-arg constructor.
static Object newInstance(Class target, Class[] types, Object[] args)
          Returns a new instance of the given class, using the constructor with the specified parameter types.
static Object newInstance(Class target, Class[] types, Object[] args, boolean makeAccessible)
          Returns a new instance of the given class, using the constructor with the specified parameter types.
static Object newInstance(Class target, Class type, Object arg)
          Returns a new instance of the given class using the constructor with the specified parameter.
static Object newInstance(String className)
          Returns a new instance of the class with the given qualified name using the default or or a no-arg constructor.
static Object newInstance(String className, Class[] types, Object[] args)
          Returns a new instance of the class with the given qualified name using the constructor with the specified signature.
static Object newInstance(String className, Class type, Object arg)
          Returns a new instance of the class with the given qualified name using the constructor with the specified parameter.
static void setClassLoader(ClassLoader loader)
          Sets the classloader to be used by OJB.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

setClassLoader

public static void setClassLoader(ClassLoader loader)
Sets the classloader to be used by OJB. This can be set by external application that need to pass a specific classloader to OJB.

Parameters:
loader - The class loader. If null then OJB will use the class loader of the current thread

getClassLoader

public static ClassLoader getClassLoader()
Returns the class loader currently used by OJB. Defaults to the class loader of the current thread (Thread.currentThread().getContextClassLoader()) if not set differently. If class loader is not explicitly set and the loader for the current thread context is null, the JVM default class loader will be used.

Returns:
The classloader used by OJB
See Also:
setClassLoader(ClassLoader)

getResource

public static URL getResource(String name)
Determines the url of the indicated resource using the currently set class loader.

Parameters:
name - The resource name
Returns:
The resource's url

getClass

public static Class getClass(String className,
                             boolean initialize)
                      throws ClassNotFoundException
Retrieves the class object for the given qualified class name.

Parameters:
className - The qualified name of the class
initialize - Whether the class shall be initialized
Returns:
The class object
Throws:
ClassNotFoundException

newInstance

public static Object newInstance(Class target)
                          throws InstantiationException,
                                 IllegalAccessException
Returns a new instance of the given class, using the default or a no-arg constructor.

Parameters:
target - The class to instantiate
Returns:
The instance
Throws:
InstantiationException
IllegalAccessException

newInstance

public static Object newInstance(Class target,
                                 boolean makeAccessible)
                          throws InstantiationException,
                                 IllegalAccessException
Returns a new instance of the given class, using the default or a no-arg constructor. This method can also use private no-arg constructors if makeAccessible is set to true (and there are no other security constraints).

Parameters:
target - The class to instantiate
makeAccessible - If the constructor shall be made accessible prior to using it
Returns:
The instance
Throws:
InstantiationException
IllegalAccessException

newInstance

public static Object newInstance(Class target,
                                 Class[] types,
                                 Object[] args)
                          throws InstantiationException,
                                 IllegalAccessException,
                                 IllegalArgumentException,
                                 InvocationTargetException,
                                 NoSuchMethodException,
                                 SecurityException
Returns a new instance of the given class, using the constructor with the specified parameter types.

Parameters:
target - The class to instantiate
types - The parameter types
args - The arguments
Returns:
The instance
Throws:
InstantiationException
IllegalAccessException
IllegalArgumentException
InvocationTargetException
NoSuchMethodException
SecurityException

newInstance

public static Object newInstance(Class target,
                                 Class[] types,
                                 Object[] args,
                                 boolean makeAccessible)
                          throws InstantiationException,
                                 IllegalAccessException,
                                 IllegalArgumentException,
                                 InvocationTargetException,
                                 NoSuchMethodException,
                                 SecurityException
Returns a new instance of the given class, using the constructor with the specified parameter types. This method can also use private constructors if makeAccessible is set to true (and there are no other security constraints).

Parameters:
target - The class to instantiate
types - The parameter types
args - The arguments
makeAccessible - If the constructor shall be made accessible prior to using it
Returns:
The instance
Throws:
InstantiationException
IllegalAccessException
IllegalArgumentException
InvocationTargetException
NoSuchMethodException
SecurityException

getMethod

public static Method getMethod(Class clazz,
                               String methodName,
                               Class[] params)
Determines the method with the specified signature via reflection look-up.

Parameters:
clazz - The java class to search in
methodName - The method's name
params - The parameter types
Returns:
The method object or null if no matching method was found

getField

public static Field getField(Class clazz,
                             String fieldName)
Determines the field via reflection look-up.

Parameters:
clazz - The java class to search in
fieldName - The field's name
Returns:
The field object or null if no matching field was found

getClass

public static Class getClass(String name)
                      throws ClassNotFoundException
Convenience method for getClass(name, true)

Parameters:
name - The qualified class name
Returns:
The class object
Throws:
ClassNotFoundException

newInstance

public static Object newInstance(String className)
                          throws InstantiationException,
                                 IllegalAccessException,
                                 ClassNotFoundException
Returns a new instance of the class with the given qualified name using the default or or a no-arg constructor.

Parameters:
className - The qualified name of the class to instantiate
Throws:
InstantiationException
IllegalAccessException
ClassNotFoundException

newInstance

public static Object newInstance(String className,
                                 Class[] types,
                                 Object[] args)
                          throws InstantiationException,
                                 IllegalAccessException,
                                 IllegalArgumentException,
                                 InvocationTargetException,
                                 NoSuchMethodException,
                                 SecurityException,
                                 ClassNotFoundException
Returns a new instance of the class with the given qualified name using the constructor with the specified signature.

Parameters:
className - The qualified name of the class to instantiate
types - The parameter types
args - The arguments
Returns:
The instance
Throws:
InstantiationException
IllegalAccessException
IllegalArgumentException
InvocationTargetException
NoSuchMethodException
SecurityException
ClassNotFoundException

newInstance

public static Object newInstance(Class target,
                                 Class type,
                                 Object arg)
                          throws InstantiationException,
                                 IllegalAccessException,
                                 IllegalArgumentException,
                                 InvocationTargetException,
                                 NoSuchMethodException,
                                 SecurityException
Returns a new instance of the given class using the constructor with the specified parameter.

Parameters:
target - The class to instantiate
type - The types of the single parameter of the constructor
arg - The argument
Returns:
The instance
Throws:
InstantiationException
IllegalAccessException
IllegalArgumentException
InvocationTargetException
NoSuchMethodException
SecurityException

newInstance

public static Object newInstance(String className,
                                 Class type,
                                 Object arg)
                          throws InstantiationException,
                                 IllegalAccessException,
                                 IllegalArgumentException,
                                 InvocationTargetException,
                                 NoSuchMethodException,
                                 SecurityException,
                                 ClassNotFoundException
Returns a new instance of the class with the given qualified name using the constructor with the specified parameter.

Parameters:
className - The qualified name of the class to instantiate
type - The types of the single parameter of the constructor
arg - The argument
Returns:
The instance
Throws:
InstantiationException
IllegalAccessException
IllegalArgumentException
InvocationTargetException
NoSuchMethodException
SecurityException
ClassNotFoundException

getMethod

public static Method getMethod(Object object,
                               String methodName,
                               Class[] params)
Determines the method with the specified signature via reflection look-up.

Parameters:
object - The instance whose class is searched for the method
methodName - The method's name
params - The parameter types
Returns:
A method object or null if no matching method was found

getMethod

public static Method getMethod(String className,
                               String methodName,
                               Class[] params)
Determines the method with the specified signature via reflection look-up.

Parameters:
className - The qualified name of the searched class
methodName - The method's name
params - The parameter types
Returns:
A method object or null if no matching method was found

buildNewObjectInstance

public static Object buildNewObjectInstance(ClassDescriptor cld)
Builds a new instance for the class represented by the given class descriptor.

Parameters:
cld - The class descriptor
Returns:
The instance


Copyright © 2004-2012 The Kuali Foundation. All Rights Reserved.