diff options
author | tromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4> | 2002-12-20 03:49:20 +0000 |
---|---|---|
committer | tromey <tromey@138bc75d-0d04-0410-961f-82ee72b054a4> | 2002-12-20 03:49:20 +0000 |
commit | 68b16280c97b4ebabda6e3f26d3f3462f557ab8f (patch) | |
tree | bc765c016644edf5697556965a00b30ff3ca18f5 /libjava/org/xml/sax/helpers/NewInstance.java | |
parent | 4ab16f021ddcaddcda46b79844c8e71a4e931856 (diff) | |
download | ppe42-gcc-68b16280c97b4ebabda6e3f26d3f3462f557ab8f.tar.gz ppe42-gcc-68b16280c97b4ebabda6e3f26d3f3462f557ab8f.zip |
2002-12-19 Anthony Green <green@redhat.com>
* Makefile.am (ordinary_java_source_files): Add
org/xml/sax/helpers/NewInstance.java.
* Makefile.in: Rebuilt.
* org/xml/sax/package.html, org/xml/sax/ext/package.html,
org/xml/sax/helpers/package.html: New files.
* org/xml/sax/*: Upgrade to SAX 2.0.1 release from
http://www.saxproject.org.
git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@60350 138bc75d-0d04-0410-961f-82ee72b054a4
Diffstat (limited to 'libjava/org/xml/sax/helpers/NewInstance.java')
-rw-r--r-- | libjava/org/xml/sax/helpers/NewInstance.java | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/libjava/org/xml/sax/helpers/NewInstance.java b/libjava/org/xml/sax/helpers/NewInstance.java new file mode 100644 index 00000000000..7d107a65239 --- /dev/null +++ b/libjava/org/xml/sax/helpers/NewInstance.java @@ -0,0 +1,80 @@ +// NewInstance.java - create a new instance of a class by name. +// http://www.saxproject.org +// Written by Edwin Goei, edwingo@apache.org +// and by David Brownell, dbrownell@users.sourceforge.net +// NO WARRANTY! This class is in the Public Domain. + +// $Id: NewInstance.java,v 1.1.2.4 2002/01/29 21:34:14 dbrownell Exp $ + +package org.xml.sax.helpers; + +import java.lang.reflect.Method; +import java.lang.reflect.InvocationTargetException; + +/** + * Create a new instance of a class by name. + * + * <blockquote> + * <em>This module, both source code and documentation, is in the + * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em> + * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a> + * for further information. + * </blockquote> + * + * <p>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).</p> + * + * <p>This code is designed to compile and run on JDK version 1.1 and later + * including versions of Java 2.</p> + * + * @author Edwin Goei, David Brownell + * @version 2.0.1 (sax2r2) + */ +class NewInstance { + + /** + * Creates a new instance of the specified class name + * + * Package private so this code is not exposed at the API level. + */ + static Object newInstance (ClassLoader classLoader, String className) + throws ClassNotFoundException, IllegalAccessException, + InstantiationException + { + Class driverClass; + if (classLoader == null) { + driverClass = Class.forName(className); + } else { + driverClass = classLoader.loadClass(className); + } + return driverClass.newInstance(); + } + + /** + * Figure out which ClassLoader to use. For JDK 1.2 and later use + * the context ClassLoader. + */ + static ClassLoader getClassLoader () + { + Method m = null; + + try { + m = Thread.class.getMethod("getContextClassLoader", 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(), null); + } catch (IllegalAccessException e) { + // assert(false) + throw new UnknownError(e.getMessage()); + } catch (InvocationTargetException e) { + // assert(e.getTargetException() instanceof SecurityException) + throw new UnknownError(e.getMessage()); + } + } +} |