FileDocCategorySizeDatePackage
NewInstance.javaAPI DocJava SE 6 API2677Tue Jun 10 00:27:36 BST 2008org.xml.sax.helpers

NewInstance

public class NewInstance extends Object
Create a new instance of a class by name.
This module, both source code and documentation, is in the Public Domain, and comes with NO WARRANTY. See http://www.saxproject.org for further information.

This class contains a static method for creating an instance of a class from an explicit class name. It tries to use the thread's context ClassLoader if possible and falls back to using Class.forName(String).

This code is designed to compile and run on JDK version 1.1 and later including versions of Java 2.

author
Edwin Goei, David Brownell
version
2.0.1 (sax2r2)

Fields Summary
Constructors Summary
Methods Summary
static java.lang.ClassLoadergetClassLoader()
Figure out which ClassLoader to use. For JDK 1.2 and later use the context ClassLoader.

        Method m = null;

        try {
            m = Thread.class.getMethod("getContextClassLoader", (Class[]) null);
        } catch (NoSuchMethodException e) {
            // Assume that we are running JDK 1.1, use the current ClassLoader
            return NewInstance.class.getClassLoader();
        }

        try {
            return (ClassLoader) m.invoke(Thread.currentThread(), (Object[]) null);
        } catch (IllegalAccessException e) {
            // assert(false)
            throw new UnknownError(e.getMessage());
        } catch (InvocationTargetException e) {
            // assert(e.getTargetException() instanceof SecurityException)
            throw new UnknownError(e.getMessage());
        }
    
static java.lang.ObjectnewInstance(java.lang.ClassLoader classLoader, java.lang.String className)
Creates a new instance of the specified class name Package private so this code is not exposed at the API level.

        Class driverClass;
        if (classLoader == null) {
            driverClass = Class.forName(className);
        } else {
            driverClass = classLoader.loadClass(className);
        }
        return driverClass.newInstance();