Path: blob/master/test/hotspot/jtreg/runtime/ClassUnload/ConstantPoolDependsTest.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 ConstantPoolDependsTest25* @bug 821009426* @summary Create ClassLoader dependency from initiating loader to class loader through constant pool reference27* @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 ConstantPoolDependsTest35*/3637import sun.hotspot.WhiteBox;38import jdk.test.lib.classloader.ClassUnloadCommon;3940public class ConstantPoolDependsTest {41public static WhiteBox wb = WhiteBox.getWhiteBox();42public static final String MY_TEST = "ConstantPoolDependsTest$c1c";4344public static class c1c {45private void test() throws Exception {46// ConstantPool.klass_at_impl loads through constant pool and creates dependency47p2.c2 c2_obj = new p2.c2();48c2_obj.method2();49}5051public c1c () throws Exception {52test();53ClassUnloadCommon.triggerUnloading(); // should not unload anything54test();55ClassUnloadCommon.triggerUnloading(); // should not unload anything56}57}5859static void test() throws Throwable {6061// now use the same loader to load class MyTest62Class MyTest_class = new MyDiffClassLoader(MY_TEST).loadClass(MY_TEST);6364try {65// Call MyTest to load p2.c2 twice and call p2.c2.method266MyTest_class.newInstance();67} catch (Exception e) {68throw new RuntimeException("Test FAILED if NoSuchMethodException is thrown");69}70ClassUnloadCommon.triggerUnloading(); // should not unload anything71ClassUnloadCommon.failIf(!wb.isClassAlive(MY_TEST), "should not be unloaded");72ClassUnloadCommon.failIf(!wb.isClassAlive("p2.c2"), "should not be unloaded");73// Unless MyTest_class is referenced here, the compiler can unload it.74System.out.println("Should not unload anything before here because " + MyTest_class + " is still alive.");75}7677public static void main(String args[]) throws Throwable {78test();79ClassUnloadCommon.triggerUnloading(); // should unload80System.gc();81System.out.println("Should unload p2.c2 just now");82ClassUnloadCommon.failIf(wb.isClassAlive(MY_TEST), "should be unloaded");83ClassUnloadCommon.failIf(wb.isClassAlive("p2.c2"), "should be unloaded");84}85}868788