|
1 /* |
|
2 * Copyright (c) 2019, 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.File; |
|
26 import java.net.URL; |
|
27 import java.net.URLClassLoader; |
|
28 import sun.hotspot.WhiteBox; |
|
29 |
|
30 public class HelloUnload { |
|
31 private static String className = "CustomLoadee"; |
|
32 |
|
33 public static void main(String args[]) throws Exception { |
|
34 if (args.length != 3) { |
|
35 throw new RuntimeException("Unexpected number of arguments: expected 3, actual " + args.length); |
|
36 } |
|
37 |
|
38 String path = args[0]; |
|
39 URL url = new File(path).toURI().toURL(); |
|
40 URL[] urls = new URL[] {url}; |
|
41 System.out.println(path); |
|
42 System.out.println(url); |
|
43 |
|
44 // unload the custom class loader |
|
45 boolean doUnload = false; |
|
46 if (args[1].equals("true")) { |
|
47 doUnload = true; |
|
48 } else if (args[1].equals("false")) { |
|
49 doUnload = false; |
|
50 } else { |
|
51 throw new RuntimeException("args[1] can only be either \"true\" or \"false\", actual " + args[1]); |
|
52 } |
|
53 |
|
54 // should the CustomLoadee class be in the shared archive |
|
55 boolean inArchive = false; |
|
56 if (args[2].equals("true")) { |
|
57 inArchive = true; |
|
58 } else if (args[2].equals("false")) { |
|
59 inArchive = false; |
|
60 } else { |
|
61 throw new RuntimeException("args[2] can only be either \"true\" or \"false\", actual " + args[1]); |
|
62 } |
|
63 |
|
64 URLClassLoader urlClassLoader = |
|
65 new URLClassLoader("HelloClassLoader", urls, null); |
|
66 Class c = Class.forName(className, true, urlClassLoader); |
|
67 System.out.println(c); |
|
68 System.out.println(c.getClassLoader()); |
|
69 Object o = c.newInstance(); |
|
70 |
|
71 // [1] Check that CustomLoadee is defined by the correct loader |
|
72 if (c.getClassLoader() != urlClassLoader) { |
|
73 throw new RuntimeException("c.getClassLoader() == " + c.getClassLoader() + |
|
74 ", expected == " + urlClassLoader); |
|
75 } |
|
76 |
|
77 // [2] Check that CustomLoadee is loaded from shared archive. |
|
78 WhiteBox wb = WhiteBox.getWhiteBox(); |
|
79 if(wb.isSharedClass(HelloUnload.class)) { |
|
80 if (inArchive && !wb.isSharedClass(c)) { |
|
81 throw new RuntimeException("wb.isSharedClass(c) should be true"); |
|
82 } |
|
83 } |
|
84 |
|
85 ClassUnloadCommon.failIf(!wb.isClassAlive(className), "should be live here"); |
|
86 |
|
87 if (doUnload) { |
|
88 String loaderName = urlClassLoader.getName(); |
|
89 int loadedRefcount = wb.getSymbolRefcount(loaderName); |
|
90 System.out.println("Refcount of symbol " + loaderName + " is " + loadedRefcount); |
|
91 |
|
92 urlClassLoader = null; c = null; o = null; |
|
93 ClassUnloadCommon.triggerUnloading(); |
|
94 System.out.println("Is CustomLoadee alive? " + wb.isClassAlive(className)); |
|
95 ClassUnloadCommon.failIf(wb.isClassAlive(className), "should have been unloaded"); |
|
96 |
|
97 int unloadedRefcount = wb.getSymbolRefcount(loaderName); |
|
98 System.out.println("Refcount of symbol " + loaderName + " is " + unloadedRefcount); |
|
99 |
|
100 // refcount of a permanent symbol will not be decremented |
|
101 if (loadedRefcount != 65535) { |
|
102 ClassUnloadCommon.failIf(unloadedRefcount != (loadedRefcount - 1), "Refcount must be decremented"); |
|
103 } |
|
104 } |
|
105 } |
|
106 } |