Path: blob/master/test/hotspot/jtreg/runtime/ClassUnload/DictionaryDependsTest.java
40942 views
/*1* Copyright (c) 2018, 2021, 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* @requires vm.opt.final.ClassUnloading28* @modules java.base/jdk.internal.misc29* java.compiler30* @library /test/lib31* @build sun.hotspot.WhiteBox32* @compile p2/c2.java MyDiffClassLoader.java33* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox34* @run main/othervm -Xbootclasspath/a:. -Xmn8m -XX:+UnlockDiagnosticVMOptions -Xlog:class+unload -XX:+WhiteBoxAPI DictionaryDependsTest35*/36import sun.hotspot.WhiteBox;37import java.lang.reflect.Method;38import jdk.test.lib.classloader.ClassUnloadCommon;3940public class DictionaryDependsTest {41public static WhiteBox wb = WhiteBox.getWhiteBox();42public static final String MY_TEST = "DictionaryDependsTest$c1r";4344static public class c1r {4546private void test() throws Exception {47// forName loads through reflection and doesn't create dependency48Class<?> x = Class.forName("p2.c2", true, c1r.class.getClassLoader());49Method m = x.getMethod("method2");50java.lang.Object t = x.newInstance();51m.invoke(t);52}5354public c1r () throws Exception {55test();56ClassUnloadCommon.triggerUnloading(); // should unload p2.c257test();58ClassUnloadCommon.triggerUnloading(); // should unload p2.c259}60}6162public void test() throws Throwable {6364// now use the same loader to load class MyTest65Class MyTest_class = new MyDiffClassLoader(MY_TEST).loadClass(MY_TEST);6667try {68// Call MyTest to load p2.c2 twice and call p2.c2.method269MyTest_class.newInstance();70} catch (Exception e) {71System.out.println("Not expected NSME");72throw new RuntimeException("Not expecting NSME");73}74ClassUnloadCommon.triggerUnloading(); // should not unload anything75ClassUnloadCommon.failIf(!wb.isClassAlive(MY_TEST), "should not be unloaded");76ClassUnloadCommon.failIf(!wb.isClassAlive("p2.c2"), "should not be unloaded");77// Unless MyTest_class is referenced here, the compiler can unload it.78System.out.println("Should not unload anything before here because " + MyTest_class + " is still alive.");79}8081public static void main(String args[]) throws Throwable {82DictionaryDependsTest d = new DictionaryDependsTest();83d.test();84ClassUnloadCommon.triggerUnloading(); // should not unload anything85System.out.println("Should unload MyTest and p2.c2 just now");86ClassUnloadCommon.failIf(wb.isClassAlive(MY_TEST), "should be unloaded");87ClassUnloadCommon.failIf(wb.isClassAlive("p2.c2"), "should be unloaded");88}89}909192