Path: blob/master/test/hotspot/jtreg/runtime/ClassUnload/UnloadInterfaceTest.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 UnloadInterfaceTest25* @requires vm.opt.final.ClassUnloading26* @modules java.base/jdk.internal.misc27* @library /test/lib28* @compile test/Interface.java29* @compile test/ImplementorClass.java30* @build sun.hotspot.WhiteBox31* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox32* @run main/othervm -Xbootclasspath/a:. -Xmn8m -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xlog:class+unload=trace UnloadInterfaceTest33*/34import sun.hotspot.WhiteBox;35import test.Interface;36import java.lang.ClassLoader;37import jdk.test.lib.classloader.ClassUnloadCommon;3839/**40* Test that verifies that class unloaded removes the implementor from its the interface that it implements41* via logging.42* [1.364s][info][class,unload] unloading class test.ImplementorClass 0x00000008000a284043* [1.366s][trace][class,unload] unlinking class (subclass): test.ImplementorClass44* [1.366s][trace][class,unload] unlinking class (implementor): test.ImplementorClass45*/46public class UnloadInterfaceTest {47private static String className = "test.ImplementorClass";48private static String interfaceName = "test.Interface";4950static class LoaderToUnload extends ClassLoader {51ClassLoader myParent;52public Class loadClass(String name) throws ClassNotFoundException {53if (name.contains(className)) {54System.out.println("className found " + className);55byte[] data = ClassUnloadCommon.getClassData(name);56return defineClass(name, data, 0, data.length);57} else {58return myParent.loadClass(name);59}60}61public LoaderToUnload(ClassLoader parent) {62super();63myParent = parent;64}65}6667public static void main(String... args) throws Exception {68run();69}7071private static void run() throws Exception {72final WhiteBox wb = WhiteBox.getWhiteBox();7374ClassUnloadCommon.failIf(wb.isClassAlive(className), "is not expected to be alive yet");7576// Load interface Class with one class loader.77ClassLoader icl = ClassUnloadCommon.newClassLoader();78Class<?> ic = icl.loadClass(interfaceName);7980ClassLoader cl = new LoaderToUnload(icl);81Class<?> c = cl.loadClass(className);82Object o = c.newInstance();8384ClassUnloadCommon.failIf(!wb.isClassAlive(className), "should be live here");85ClassUnloadCommon.failIf(!wb.isClassAlive(interfaceName), "should be live here");8687cl = null; c = null; o = null;88ClassUnloadCommon.triggerUnloading();89ClassUnloadCommon.failIf(wb.isClassAlive(className), "should have been unloaded");90ClassUnloadCommon.failIf(!wb.isClassAlive(interfaceName), "should be live here");91System.out.println("We still have Interface referenced" + ic);92}93}94959697