1 /* |
|
2 * Copyright (c) 2015, 2017, 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. |
|
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 |
|
84 URLClassLoader urlClassLoader = new URLClassLoader(urls); |
|
85 Class c2 = Util.defineClassFromJAR(urlClassLoader, jarFile, ONLY_BUILTIN); |
|
86 |
|
87 if (c2.getClassLoader() != urlClassLoader) { |
|
88 throw new RuntimeException("Error in test"); |
|
89 } |
|
90 |
|
91 if (wb.isSharedClass(LoaderSegregation.class)) { |
|
92 if (wb.isSharedClass(c2)) { |
|
93 throw new RuntimeException("wb.isSharedClass(c2) should be false - " + |
|
94 "unregistered loader cannot load an archived BUILTIN class"); |
|
95 } |
|
96 } |
|
97 } |
|
98 } |
|
99 } |
|