1
// NewInstance.java - create a new instance of a class by name.
2
// http://www.saxproject.org
3
// Written by Edwin Goei, edwingo@apache.org
4
// and by David Brownell, dbrownell@users.sourceforge.net
5
// NO WARRANTY! This class is in the Public Domain.
6
// $Id: NewInstance.java 226252 2005-06-22 02:13:56Z mrglavas $
8
package org.xml.sax.helpers;
11
* Create a new instance of a class by name.
14
* <em>This module, both source code and documentation, is in the
15
* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
16
* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
17
* for further information.
20
* <p>This class contains a static method for creating an instance of a
21
* class from an explicit class name. It tries to use the thread's context
22
* ClassLoader if possible and falls back to using
23
* Class.forName(String). It also takes into account JDK 1.2+'s
24
* AccessController mechanism for performing its actions. </p>
26
* <p>This code is designed to compile and run on JDK version 1.1 and later
27
* including versions of Java 2.</p>
29
* <p>This is <strong>not</strong> the NewInstance accompanying SAX 2.0.2; it
30
* represents many fixes to that code.
32
* @author Edwin Goei, David Brownell
33
* @version 2.0.1 (sax2r2)
39
// governs whether, if we fail in finding a class even
40
// when given a classloader, we'll make a last-ditch attempt
41
// to use the current classloader.
42
private static final boolean DO_FALLBACK = true;
45
* Creates a new instance of the specified class name
47
* Package private so this code is not exposed at the API level.
49
static Object newInstance (ClassLoader classLoader, String className)
50
throws ClassNotFoundException, IllegalAccessException,
51
InstantiationException
54
if (classLoader == null) {
55
// XXX Use the bootstrap ClassLoader. There is no way to
56
// load a class using the bootstrap ClassLoader that works
57
// in both JDK 1.1 and Java 2. However, this should still
58
// work b/c the following should be true:
60
// (cl == null) iff current ClassLoader == null
62
// Thus Class.forName(String) will use the current
63
// ClassLoader which will be the bootstrap ClassLoader.
64
driverClass = Class.forName(className);
67
driverClass = classLoader.loadClass(className);
68
} catch (ClassNotFoundException x) {
70
// Fall back to current classloader
71
classLoader = NewInstance.class.getClassLoader();
72
if (classLoader != null) {
73
driverClass = classLoader.loadClass(className);
76
driverClass = Class.forName(className);
83
Object instance = driverClass.newInstance();
88
* Figure out which ClassLoader to use. For JDK 1.2 and later use
89
* the context ClassLoader.
91
static ClassLoader getClassLoader ()
93
SecuritySupport ss = SecuritySupport.getInstance();
95
// Figure out which ClassLoader to use for loading the provider
96
// class. If there is a Context ClassLoader then use it.
97
ClassLoader cl = ss.getContextClassLoader();
99
// Assert: we are on JDK 1.1 or we have no Context ClassLoader
100
// so use the current ClassLoader
101
cl = NewInstance.class.getClassLoader();