author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 57765 | a89ec7fc99aa |
permissions | -rw-r--r-- |
48138 | 1 |
/* |
57567
b000362a89a0
8202339: [TESTBUG] Consolidate the tests in runtime/SharedArchiveFile and runtime/appcds
coleenp
parents:
48138
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
48138 | 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. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
* |
|
23 |
*/ |
|
24 |
||
25 |
import java.io.*; |
|
26 |
import java.net.*; |
|
27 |
import sun.hotspot.WhiteBox; |
|
28 |
||
29 |
public class LoaderSegregation { |
|
30 |
// Use these definitions instead of literal strings, to avoid typos. |
|
31 |
static final String ONLY_BUILTIN = "OnlyBuiltin"; |
|
32 |
static final String ONLY_UNREGISTERED = "OnlyUnregistered"; |
|
33 |
||
34 |
public static void main(String args[]) throws Exception { |
|
35 |
WhiteBox wb = WhiteBox.getWhiteBox(); |
|
36 |
||
37 |
// [A] An archived BUILTIN class cannot be a subclass of a non-BUILTIN class. |
|
38 |
// [B] An archived BUILTIN class cannot implement a non-BUILTIN interface. |
|
39 |
if (wb.isSharedClass(LoaderSegregation.class)) { |
|
40 |
// [1] check that CustomLoadee is loadable from archive |
|
41 |
if (!wb.isSharedClass(CustomLoadee.class)) { |
|
42 |
throw new RuntimeException("wb.isSharedClass(CustomLoadee.class) should be true"); |
|
43 |
} |
|
44 |
||
45 |
// [2] CustomInterface2_ia should be archived, even though it was not specified in the classlist. |
|
46 |
// It was successfully dumped as a side effect of attempting to load CustomLoadee2 |
|
47 |
// during dump time. Note that CustomLoadee2 failed to dump because one of its interfaces, |
|
48 |
// CustomInterface2_ib, was not loadable from the BOOT/EXT/APP classpath. during dump time. |
|
49 |
if (!wb.isSharedClass(CustomInterface2_ia.class)) { |
|
50 |
throw new RuntimeException("wb.isSharedClass(CustomInterface2_ia.class) should be true"); |
|
51 |
} |
|
52 |
||
53 |
// [3] Check that the BUILTIN versions of CustomLoadee2 and CustomLoadee3Child are loadable |
|
54 |
// at run time (since we have append LoaderSegregation_app2.jar the classpath), |
|
55 |
// but these classes must be loaded from the JAR file. |
|
56 |
if (wb.isSharedClass(CustomLoadee2.class)) { |
|
57 |
throw new RuntimeException("wb.isSharedClass(CustomLoadee2.class) should be false"); |
|
58 |
} |
|
59 |
if (wb.isSharedClass(CustomLoadee3.class)) { |
|
60 |
throw new RuntimeException("wb.isSharedClass(CustomLoadee3.class) should be false"); |
|
61 |
} |
|
62 |
if (wb.isSharedClass(CustomLoadee3Child.class)) { |
|
63 |
throw new RuntimeException("wb.isSharedClass(CustomLoadee3Child.class) should be false"); |
|
64 |
} |
|
65 |
} |
|
66 |
||
67 |
// [C] BUILTIN and UNREGISTERED classes can be loaded only by their corresponding |
|
68 |
// type of loaders. |
|
69 |
||
70 |
String path = args[0]; |
|
71 |
File jarFile = new File(path); |
|
72 |
URL url = new File(path).toURI().toURL(); |
|
73 |
URL[] urls = new URL[] {url}; |
|
74 |
ClassLoader appLoader = LoaderSegregation.class.getClassLoader(); |
|
75 |
||
76 |
{ // BUILTIN LOADER |
|
77 |
try { |
|
78 |
appLoader.loadClass(ONLY_UNREGISTERED); |
|
79 |
throw new RuntimeException("BUILTIN loader cannot load archived UNREGISTERED class"); |
|
80 |
} catch (ClassNotFoundException expected) {} |
|
81 |
} |
|
82 |
||
83 |
{ // UNREGISTERED LOADER |
|
57765
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
84 |
URLClassLoader urlClassLoader = new URLClassLoader(urls) { |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
85 |
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
86 |
synchronized (getClassLoadingLock(name)) { |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
87 |
Class<?> c = findLoadedClass(name); |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
88 |
if (c == null) { |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
89 |
try { |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
90 |
c = findClass(name); |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
91 |
} catch (ClassNotFoundException e) { |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
92 |
c = getParent().loadClass(name); |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
93 |
} |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
94 |
} |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
95 |
if (resolve) { |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
96 |
resolveClass(c); |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
97 |
} |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
98 |
return c; |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
99 |
} |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
100 |
} |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
101 |
}; |
a89ec7fc99aa
8226645: [TESTBUG] some AppCDS tests rely on illegal reflective access
ccheung
parents:
57567
diff
changeset
|
102 |
Class<?> c2 = urlClassLoader.loadClass(ONLY_BUILTIN); |
48138 | 103 |
|
104 |
if (c2.getClassLoader() != urlClassLoader) { |
|
105 |
throw new RuntimeException("Error in test"); |
|
106 |
} |
|
107 |
||
108 |
if (wb.isSharedClass(LoaderSegregation.class)) { |
|
109 |
if (wb.isSharedClass(c2)) { |
|
110 |
throw new RuntimeException("wb.isSharedClass(c2) should be false - " + |
|
111 |
"unregistered loader cannot load an archived BUILTIN class"); |
|
112 |
} |
|
113 |
} |
|
114 |
} |
|
115 |
} |
|
116 |
} |