src/java.xml/share/classes/org/xml/sax/helpers/NewInstance.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58247 3aef3bccfae3
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     1
/*
58247
3aef3bccfae3 8231083: Clarify SAX documentation
joehw
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     4
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    10
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    15
 * accompanied this code).
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    16
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    20
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    23
 * questions.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    24
 */
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    25
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    26
package org.xml.sax.helpers;
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    27
45853
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 38817
diff changeset
    28
import java.lang.reflect.InvocationTargetException;
37927
fe4019feb049 8152912: SAX XMLReaderFactory needs to be ServiceLoader compliant
joehw
parents: 25868
diff changeset
    29
import java.util.Objects;
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    30
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    31
/**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    32
 * Create a new instance of a class by name.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    33
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    34
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    35
 * <p>This class contains a static method for creating an instance of a
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    36
 * class from an explicit class name.  It tries to use the thread's context
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    37
 * ClassLoader if possible and falls back to using
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    38
 * Class.forName(String).</p>
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    39
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    40
 * <p>This code is designed to compile and run on JDK version 1.1 and later
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    41
 * including versions of Java 2.</p>
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    42
 *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    43
 * @author Edwin Goei, David Brownell
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    44
 * @version 2.0.1 (sax2r2)
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    45
 */
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    46
class NewInstance {
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    47
    private static final String DEFAULT_PACKAGE = "com.sun.org.apache.xerces.internal";
38817
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    48
    private static final String DEFAULT_CLASS = "com.sun.org.apache.xerces.internal.parsers.SAXParser";
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    49
    /**
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    50
     * Creates a new instance of the specified class name
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    51
     *
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    52
     * Package private so this code is not exposed at the API level.
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    53
     */
37927
fe4019feb049 8152912: SAX XMLReaderFactory needs to be ServiceLoader compliant
joehw
parents: 25868
diff changeset
    54
    static <T> T newInstance (Class<T> type, ClassLoader loader, String clsName)
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    55
        throws ClassNotFoundException, IllegalAccessException,
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    56
            InstantiationException
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    57
    {
37927
fe4019feb049 8152912: SAX XMLReaderFactory needs to be ServiceLoader compliant
joehw
parents: 25868
diff changeset
    58
        ClassLoader classLoader = Objects.requireNonNull(loader);
fe4019feb049 8152912: SAX XMLReaderFactory needs to be ServiceLoader compliant
joehw
parents: 25868
diff changeset
    59
        String className = Objects.requireNonNull(clsName);
fe4019feb049 8152912: SAX XMLReaderFactory needs to be ServiceLoader compliant
joehw
parents: 25868
diff changeset
    60
38817
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    61
        // Instantiate directly for the SAX default parser
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    62
        if (className.equals(DEFAULT_CLASS)) {
37927
fe4019feb049 8152912: SAX XMLReaderFactory needs to be ServiceLoader compliant
joehw
parents: 25868
diff changeset
    63
            return type.cast(new com.sun.org.apache.xerces.internal.parsers.SAXParser());
16953
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    64
        }
a44e04deb948 6657673: Issues with JAXP
joehw
parents: 12457
diff changeset
    65
38817
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    66
        // make sure we have access to restricted packages
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    67
        boolean internal = false;
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    68
        if (System.getSecurityManager() != null) {
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    69
            if (className != null && className.startsWith(DEFAULT_PACKAGE)) {
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    70
                internal = true;
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    71
            }
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    72
        }
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    73
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    74
        Class<?> driverClass;
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    75
        if (classLoader == null || internal) {
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    76
            driverClass = Class.forName(className);
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    77
        } else {
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    78
            driverClass = classLoader.loadClass(className);
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    79
        }
fe8c4fe3a303 8158246: Several api/org_xml/sax/helpers/XMLReader tests failed due to no SAXException occurs
joehw
parents: 37927
diff changeset
    80
45853
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 38817
diff changeset
    81
        try {
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 38817
diff changeset
    82
            return type.cast(driverClass.getConstructor().newInstance());
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 38817
diff changeset
    83
        } catch (NoSuchMethodException | SecurityException | InvocationTargetException ex) {
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 38817
diff changeset
    84
            throw new InstantiationException(ex.getMessage());
bfa06be36a17 8181154: Fix lint warnings in JAXP repo: deprecation
joehw
parents: 38817
diff changeset
    85
        }
12005
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    86
    }
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    87
a754d69d5e60 7150324: Stop using drop bundles in jaxp
ohair
parents:
diff changeset
    88
}