Path: blob/aarch64-shenandoah-jdk8u272-b10/jaxp/src/org/xml/sax/helpers/NewInstance.java
48576 views
/*1* Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425// NewInstance.java - create a new instance of a class by name.26// http://www.saxproject.org27// Written by Edwin Goei, [email protected]28// and by David Brownell, [email protected]29// NO WARRANTY! This class is in the Public Domain.30// $Id: NewInstance.java,v 1.2 2005/06/10 03:50:50 jeffsuttor Exp $3132package org.xml.sax.helpers;3334import java.lang.reflect.Method;35import java.lang.reflect.InvocationTargetException;3637/**38* Create a new instance of a class by name.39*40* <blockquote>41* <em>This module, both source code and documentation, is in the42* Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>43* See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>44* for further information.45* </blockquote>46*47* <p>This class contains a static method for creating an instance of a48* class from an explicit class name. It tries to use the thread's context49* ClassLoader if possible and falls back to using50* Class.forName(String).</p>51*52* <p>This code is designed to compile and run on JDK version 1.1 and later53* including versions of Java 2.</p>54*55* @author Edwin Goei, David Brownell56* @version 2.0.1 (sax2r2)57*/58class NewInstance {59private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal";60/**61* Creates a new instance of the specified class name62*63* Package private so this code is not exposed at the API level.64*/65static Object newInstance (ClassLoader classLoader, String className)66throws ClassNotFoundException, IllegalAccessException,67InstantiationException68{69// make sure we have access to restricted packages70boolean internal = false;71if (System.getSecurityManager() != null) {72if (className != null && className.startsWith(DEFAULT_PACKAGE)) {73internal = true;74}75}7677Class driverClass;78if (classLoader == null || internal) {79driverClass = Class.forName(className);80} else {81driverClass = classLoader.loadClass(className);82}83return driverClass.newInstance();84}8586}878889