1 /*
   2  * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 // NewInstance.java - create a new instance of a class by name.
  27 // http://www.saxproject.org
  28 // Written by Edwin Goei, edwingo@apache.org
  29 // and by David Brownell, dbrownell@users.sourceforge.net
  30 // NO WARRANTY!  This class is in the Public Domain.
  31 // $Id: NewInstance.java,v 1.2 2005/06/10 03:50:50 jeffsuttor Exp $
  32 
  33 package org.xml.sax.helpers;
  34 
  35 import java.lang.reflect.Method;
  36 import java.lang.reflect.InvocationTargetException;
  37 
  38 /**
  39  * Create a new instance of a class by name.
  40  *
  41  * <blockquote>
  42  * <em>This module, both source code and documentation, is in the
  43  * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
  44  * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
  45  * for further information.
  46  * </blockquote>
  47  *
  48  * <p>This class contains a static method for creating an instance of a
  49  * class from an explicit class name.  It tries to use the thread's context
  50  * ClassLoader if possible and falls back to using
  51  * Class.forName(String).</p>
  52  *
  53  * <p>This code is designed to compile and run on JDK version 1.1 and later
  54  * including versions of Java 2.</p>
  55  *
  56  * @author Edwin Goei, David Brownell
  57  * @version 2.0.1 (sax2r2)
  58  */
  59 class NewInstance {
  60     private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal";
  61     /**
  62      * Creates a new instance of the specified class name
  63      *
  64      * Package private so this code is not exposed at the API level.
  65      */
  66     static Object newInstance (ClassLoader classLoader, String className)
  67         throws ClassNotFoundException, IllegalAccessException,
  68             InstantiationException
  69     {
  70         // make sure we have access to restricted packages
  71         boolean internal = false;
  72         if (System.getSecurityManager() != null) {
  73             if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
  74                 internal = true;
  75             }
  76         }
  77 
  78         Class driverClass;
  79         if (classLoader == null || internal) {
  80             driverClass = Class.forName(className);
  81         } else {
  82             driverClass = classLoader.loadClass(className);
  83         }
  84         return driverClass.newInstance();
  85     }
  86 
  87 }