author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58247 | 3aef3bccfae3 |
permissions | -rw-r--r-- |
12005 | 1 |
/* |
58247 | 2 |
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved. |
12005 | 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 |
package org.xml.sax.helpers; |
|
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 | 30 |
|
31 |
/** |
|
32 |
* Create a new instance of a class by name. |
|
33 |
* |
|
34 |
* |
|
35 |
* <p>This class contains a static method for creating an instance of a |
|
36 |
* class from an explicit class name. It tries to use the thread's context |
|
37 |
* ClassLoader if possible and falls back to using |
|
38 |
* Class.forName(String).</p> |
|
39 |
* |
|
40 |
* <p>This code is designed to compile and run on JDK version 1.1 and later |
|
41 |
* including versions of Java 2.</p> |
|
42 |
* |
|
43 |
* @author Edwin Goei, David Brownell |
|
16953 | 44 |
* @version 2.0.1 (sax2r2) |
12005 | 45 |
*/ |
46 |
class NewInstance { |
|
16953 | 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 | 49 |
/** |
50 |
* Creates a new instance of the specified class name |
|
51 |
* |
|
52 |
* Package private so this code is not exposed at the API level. |
|
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 | 55 |
throws ClassNotFoundException, IllegalAccessException, |
56 |
InstantiationException |
|
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 | 64 |
} |
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 | 86 |
} |
87 |
||
88 |
} |