org.apache.ojb.broker.util
Class ClassHelper

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

public class ClassHelper
extends java.lang.Object

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

Version:
$Id: ClassHelper.java 365232 2005-12-21 22:36:07Z tomdz $

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

Method Detail

setClassLoader

public static void setClassLoader(java.lang.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 java.lang.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 java.net.URL getResource(java.lang.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 java.lang.Class getClass(java.lang.String className,
                                       boolean initialize)
                                throws java.lang.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:
java.lang.ClassNotFoundException

newInstance

public static java.lang.Object newInstance(java.lang.Class target)
                                    throws java.lang.InstantiationException,
                                           java.lang.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:
java.lang.InstantiationException
java.lang.IllegalAccessException

newInstance

public static java.lang.Object newInstance(java.lang.Class target,
                                           boolean makeAccessible)
                                    throws java.lang.InstantiationException,
                                           java.lang.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:
java.lang.InstantiationException
java.lang.IllegalAccessException

newInstance

public static java.lang.Object newInstance(java.lang.Class target,
                                           java.lang.Class[] types,
                                           java.lang.Object[] args)
                                    throws java.lang.InstantiationException,
                                           java.lang.IllegalAccessException,
                                           java.lang.IllegalArgumentException,
                                           java.lang.reflect.InvocationTargetException,
                                           java.lang.NoSuchMethodException,
                                           java.lang.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:
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.SecurityException

newInstance

public static java.lang.Object newInstance(java.lang.Class target,
                                           java.lang.Class[] types,
                                           java.lang.Object[] args,
                                           boolean makeAccessible)
                                    throws java.lang.InstantiationException,
                                           java.lang.IllegalAccessException,
                                           java.lang.IllegalArgumentException,
                                           java.lang.reflect.InvocationTargetException,
                                           java.lang.NoSuchMethodException,
                                           java.lang.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:
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.SecurityException

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Class clazz,
                                                 java.lang.String methodName,
                                                 java.lang.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 java.lang.reflect.Field getField(java.lang.Class clazz,
                                               java.lang.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 java.lang.Class getClass(java.lang.String name)
                                throws java.lang.ClassNotFoundException
Convenience method for getClass(name, true)

Parameters:
name - The qualified class name
Returns:
The class object
Throws:
java.lang.ClassNotFoundException

newInstance

public static java.lang.Object newInstance(java.lang.String className)
                                    throws java.lang.InstantiationException,
                                           java.lang.IllegalAccessException,
                                           java.lang.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:
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.ClassNotFoundException

newInstance

public static java.lang.Object newInstance(java.lang.String className,
                                           java.lang.Class[] types,
                                           java.lang.Object[] args)
                                    throws java.lang.InstantiationException,
                                           java.lang.IllegalAccessException,
                                           java.lang.IllegalArgumentException,
                                           java.lang.reflect.InvocationTargetException,
                                           java.lang.NoSuchMethodException,
                                           java.lang.SecurityException,
                                           java.lang.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:
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.SecurityException
java.lang.ClassNotFoundException

newInstance

public static java.lang.Object newInstance(java.lang.Class target,
                                           java.lang.Class type,
                                           java.lang.Object arg)
                                    throws java.lang.InstantiationException,
                                           java.lang.IllegalAccessException,
                                           java.lang.IllegalArgumentException,
                                           java.lang.reflect.InvocationTargetException,
                                           java.lang.NoSuchMethodException,
                                           java.lang.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:
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.SecurityException

newInstance

public static java.lang.Object newInstance(java.lang.String className,
                                           java.lang.Class type,
                                           java.lang.Object arg)
                                    throws java.lang.InstantiationException,
                                           java.lang.IllegalAccessException,
                                           java.lang.IllegalArgumentException,
                                           java.lang.reflect.InvocationTargetException,
                                           java.lang.NoSuchMethodException,
                                           java.lang.SecurityException,
                                           java.lang.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:
java.lang.InstantiationException
java.lang.IllegalAccessException
java.lang.IllegalArgumentException
java.lang.reflect.InvocationTargetException
java.lang.NoSuchMethodException
java.lang.SecurityException
java.lang.ClassNotFoundException

getMethod

public static java.lang.reflect.Method getMethod(java.lang.Object object,
                                                 java.lang.String methodName,
                                                 java.lang.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 java.lang.reflect.Method getMethod(java.lang.String className,
                                                 java.lang.String methodName,
                                                 java.lang.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 java.lang.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


(C) 2002 - 2006 Apache Software Foundation
All rights reserved. Published under the Apache License 2.0.
http://db.apache.org/ojb
Version: 1.0.4, 2005-12-30