Class | Line # | Actions | ||||
---|---|---|---|---|---|---|
NewInstance | 34 | 13 | 6 | 17 |
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,v 1.4 2002/01/30 20:52:27 dbrownell Exp $ | |
7 | ||
8 | package org.xml.sax.helpers; | |
9 | ||
10 | import java.lang.reflect.Method; | |
11 | import java.lang.reflect.InvocationTargetException; | |
12 | ||
13 | /** | |
14 | * Create a new instance of a class by name. | |
15 | * | |
16 | * <blockquote> | |
17 | * <em>This module, both source code and documentation, is in the | |
18 | * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em> | |
19 | * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a> | |
20 | * for further information. | |
21 | * </blockquote> | |
22 | * | |
23 | * <p>This class contains a static method for creating an instance of a | |
24 | * class from an explicit class name. It tries to use the thread's context | |
25 | * ClassLoader if possible and falls back to using | |
26 | * Class.forName(String).</p> | |
27 | * | |
28 | * <p>This code is designed to compile and run on JDK version 1.1 and later | |
29 | * including versions of Java 2.</p> | |
30 | * | |
31 | * @author Edwin Goei, David Brownell | |
32 | * @version 2.0.1 (sax2r2) | |
33 | */ | |
34 | class NewInstance { | |
35 | ||
36 | /** | |
37 | * Creates a new instance of the specified class name | |
38 | * | |
39 | * Package private so this code is not exposed at the API level. | |
40 | */ | |
41 | 0 | static Object newInstance (ClassLoader classLoader, String className) |
42 | throws ClassNotFoundException, IllegalAccessException, | |
43 | InstantiationException | |
44 | { | |
45 | 0 | Class driverClass; |
46 | 0 | if (classLoader == null) { |
47 | 0 | driverClass = Class.forName(className); |
48 | } else { | |
49 | 0 | driverClass = classLoader.loadClass(className); |
50 | } | |
51 | 0 | return driverClass.newInstance(); |
52 | } | |
53 | ||
54 | /** | |
55 | * Figure out which ClassLoader to use. For JDK 1.2 and later use | |
56 | * the context ClassLoader. | |
57 | */ | |
58 | 0 | static ClassLoader getClassLoader () |
59 | { | |
60 | 0 | Method m = null; |
61 | ||
62 | 0 | try { |
63 | 0 | m = Thread.class.getMethod("getContextClassLoader", null); |
64 | } catch (NoSuchMethodException e) { | |
65 | // Assume that we are running JDK 1.1, use the current ClassLoader | |
66 | 0 | return NewInstance.class.getClassLoader(); |
67 | } | |
68 | ||
69 | 0 | try { |
70 | 0 | return (ClassLoader) m.invoke(Thread.currentThread(), null); |
71 | } catch (IllegalAccessException e) { | |
72 | // assert(false) | |
73 | 0 | throw new UnknownError(e.getMessage()); |
74 | } catch (InvocationTargetException e) { | |
75 | // assert(e.getTargetException() instanceof SecurityException) | |
76 | 0 | throw new UnknownError(e.getMessage()); |
77 | } | |
78 | } | |
79 | } |