Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/runtime/ClassUnload/DictionaryDependsTest.java
32284 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test DictionaryDependsTest25* @bug 821009426* @summary Create ClassLoader dependency from initiating loader to class loader through reflection27* @modules java.base/jdk.internal.misc28* java.compiler29* @library /testlibrary /testlibrary/whitebox /runtime/testlibrary30* @build sun.hotspot.WhiteBox31* @compile p2/c2.java MyDiffClassLoader.java32* @run main ClassFileInstaller sun.hotspot.WhiteBox33* sun.hotspot.WhiteBox$WhiteBoxPermission34* @run main/othervm -Xbootclasspath/a:. -Xmn8m -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI DictionaryDependsTest35*/36import sun.hotspot.WhiteBox;37import java.lang.reflect.Method;3839public class DictionaryDependsTest {40public static WhiteBox wb = WhiteBox.getWhiteBox();41public static final String MY_TEST = "DictionaryDependsTest$c1r";4243static public class c1r {4445private void test() throws Exception {46// forName loads through reflection and doesn't create dependency47Class<?> x = Class.forName("p2.c2", true, c1r.class.getClassLoader());48Method m = x.getMethod("method2");49java.lang.Object t = x.newInstance();50m.invoke(t);51}5253public c1r () throws Exception {54test();55ClassUnloadCommon.triggerUnloading(); // should unload p2.c256test();57ClassUnloadCommon.triggerUnloading(); // should unload p2.c258}59}6061public void test() throws Throwable {6263// now use the same loader to load class MyTest64Class MyTest_class = new MyDiffClassLoader(MY_TEST).loadClass(MY_TEST);6566try {67// Call MyTest to load p2.c2 twice and call p2.c2.method268MyTest_class.newInstance();69} catch (Exception e) {70System.out.println("Not expected NSME");71throw new RuntimeException("Not expecting NSME");72}73ClassUnloadCommon.triggerUnloading(); // should not unload anything74ClassUnloadCommon.failIf(!wb.isClassAlive(MY_TEST), "should not be unloaded");75ClassUnloadCommon.failIf(!wb.isClassAlive("p2.c2"), "should not be unloaded");76// Unless MyTest_class is referenced here, the compiler can unload it.77System.out.println("Should not unload anything before here because " + MyTest_class + " is still alive.");78}7980public static void main(String args[]) throws Throwable {81DictionaryDependsTest d = new DictionaryDependsTest();82d.test();83ClassUnloadCommon.triggerUnloading(); // should not unload anything84System.out.println("Should unload MyTest and p2.c2 just now");85ClassUnloadCommon.failIf(wb.isClassAlive(MY_TEST), "should be unloaded");86ClassUnloadCommon.failIf(wb.isClassAlive("p2.c2"), "should be unloaded");87}88}899091