|
1 /* |
|
2 * Copyright (c) 2018, 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 * @test |
|
26 * @key gc |
|
27 * @bug 8114823 |
|
28 * @requires vm.gc == null |
|
29 * @requires vm.opt.ExplicitGCInvokesConcurrent != true |
|
30 * @requires vm.opt.ClassUnloading != true |
|
31 * @library /testlibrary /testlibrary/whitebox |
|
32 * @build sun.hotspot.WhiteBox |
|
33 * @run main ClassFileInstaller sun.hotspot.WhiteBox |
|
34 * sun.hotspot.WhiteBox$WhiteBoxPermission |
|
35 * |
|
36 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI |
|
37 * -XX:-ClassUnloading -XX:+UseG1GC TestClassUnloadingDisabled |
|
38 * |
|
39 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI |
|
40 * -XX:-ClassUnloading -XX:+UseSerialGC TestClassUnloadingDisabled |
|
41 * |
|
42 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI |
|
43 * -XX:-ClassUnloading -XX:+UseParallelGC TestClassUnloadingDisabled |
|
44 * |
|
45 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI |
|
46 * -XX:-ClassUnloading -XX:+UseConcMarkSweepGC TestClassUnloadingDisabled |
|
47 */ |
|
48 |
|
49 import java.io.File; |
|
50 import java.io.IOException; |
|
51 import java.nio.file.Files; |
|
52 import java.nio.file.Path; |
|
53 import java.nio.file.Paths; |
|
54 |
|
55 import sun.hotspot.WhiteBox; |
|
56 |
|
57 import com.oracle.java.testlibrary.Asserts; |
|
58 |
|
59 public class TestClassUnloadingDisabled { |
|
60 public static void main(String args[]) throws Exception { |
|
61 final WhiteBox wb = WhiteBox.getWhiteBox(); |
|
62 // Fetch the dir where the test class and the class |
|
63 // to be loaded resides. |
|
64 String classDir = TestClassUnloadingDisabled.class.getProtectionDomain().getCodeSource().getLocation().getPath(); |
|
65 String className = "ClassToLoadUnload"; |
|
66 |
|
67 Asserts.assertFalse(wb.isClassAlive(className), "Should not be loaded yet"); |
|
68 |
|
69 // The NoPDClassLoader handles loading classes in the test directory |
|
70 // and loads them without a protection domain, which in some cases |
|
71 // keeps the class live regardless of marking state. |
|
72 NoPDClassLoader nopd = new NoPDClassLoader(classDir); |
|
73 nopd.loadClass(className); |
|
74 |
|
75 Asserts.assertTrue(wb.isClassAlive(className), "Class should be loaded"); |
|
76 |
|
77 // Clear the class-loader, class and object references to make |
|
78 // class unloading possible. |
|
79 nopd = null; |
|
80 |
|
81 System.gc(); |
|
82 Asserts.assertTrue(wb.isClassAlive(className), "Class should not have ben unloaded"); |
|
83 } |
|
84 } |
|
85 |
|
86 class NoPDClassLoader extends ClassLoader { |
|
87 String path; |
|
88 |
|
89 NoPDClassLoader(String path) { |
|
90 this.path = path; |
|
91 } |
|
92 |
|
93 public Class<?> loadClass(String name) throws ClassNotFoundException { |
|
94 byte[] cls = null; |
|
95 File f = new File(path,name + ".class"); |
|
96 |
|
97 // Delegate class loading if class not present in the given |
|
98 // directory. |
|
99 if (!f.exists()) { |
|
100 return super.loadClass(name); |
|
101 } |
|
102 |
|
103 try { |
|
104 Path path = Paths.get(f.getAbsolutePath()); |
|
105 cls = Files.readAllBytes(path); |
|
106 } catch (IOException e) { |
|
107 throw new ClassNotFoundException(name); |
|
108 } |
|
109 |
|
110 // Define class with no protection domain and resolve it. |
|
111 return defineClass(name, cls, 0, cls.length, null); |
|
112 } |
|
113 } |
|
114 |
|
115 class ClassToLoadUnload { |
|
116 } |