test/hotspot/jtreg/runtime/ClassUnload/UnloadInterfaceTest.java
changeset 52359 e38473506688
parent 52358 c42cd17e8e64
parent 52357 e2478be9c682
child 52360 9cc7df3372b0
equal deleted inserted replaced
52358:c42cd17e8e64 52359:e38473506688
     1 /*
       
     2  * Copyright (c) 2018, 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  * @test UnloadInterfaceTest
       
    26  * @requires vm.opt.final.ClassUnloading
       
    27  * @modules java.base/jdk.internal.misc
       
    28  * @library /runtime/testlibrary /test/lib
       
    29  * @compile test/Interface.java
       
    30  * @compile test/ImplementorClass.java
       
    31  * @build sun.hotspot.WhiteBox
       
    32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
       
    33  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
       
    34  * @run main/othervm -Xbootclasspath/a:. -Xmn8m -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xlog:class+unload=trace UnloadInterfaceTest
       
    35  */
       
    36 import sun.hotspot.WhiteBox;
       
    37 import test.Interface;
       
    38 import java.lang.ClassLoader;
       
    39 
       
    40 /**
       
    41  * Test that verifies that class unloaded removes the implementor from its the interface that it implements
       
    42  * via logging.
       
    43  * [1.364s][info][class,unload] unloading class test.ImplementorClass 0x00000008000a2840
       
    44  * [1.366s][trace][class,unload] unlinking class (subclass): test.ImplementorClass
       
    45  * [1.366s][trace][class,unload] unlinking class (implementor): test.ImplementorClass
       
    46  */
       
    47 public class UnloadInterfaceTest {
       
    48     private static String className = "test.ImplementorClass";
       
    49     private static String interfaceName = "test.Interface";
       
    50 
       
    51     static class LoaderToUnload extends ClassLoader {
       
    52        ClassLoader myParent;
       
    53         public Class loadClass(String name) throws ClassNotFoundException {
       
    54             if (name.contains(className)) {
       
    55               System.out.println("className found " + className);
       
    56               byte[] data = ClassUnloadCommon.getClassData(name);
       
    57               return defineClass(name, data, 0, data.length);
       
    58             } else {
       
    59               return myParent.loadClass(name);
       
    60             }
       
    61         }
       
    62         public LoaderToUnload(ClassLoader parent) {
       
    63             super();
       
    64             myParent = parent;
       
    65         }
       
    66     }
       
    67 
       
    68     public static void main(String... args) throws Exception {
       
    69        run();
       
    70     }
       
    71 
       
    72     private static void run() throws Exception {
       
    73         final WhiteBox wb = WhiteBox.getWhiteBox();
       
    74 
       
    75         ClassUnloadCommon.failIf(wb.isClassAlive(className), "is not expected to be alive yet");
       
    76 
       
    77         // Load interface Class with one class loader.
       
    78         ClassLoader icl = ClassUnloadCommon.newClassLoader();
       
    79         Class<?> ic = icl.loadClass(interfaceName);
       
    80 
       
    81         ClassLoader cl = new LoaderToUnload(icl);
       
    82         Class<?> c = cl.loadClass(className);
       
    83         Object o = c.newInstance();
       
    84 
       
    85         ClassUnloadCommon.failIf(!wb.isClassAlive(className), "should be live here");
       
    86         ClassUnloadCommon.failIf(!wb.isClassAlive(interfaceName), "should be live here");
       
    87 
       
    88         cl = null; c = null; o = null;
       
    89         ClassUnloadCommon.triggerUnloading();
       
    90         ClassUnloadCommon.failIf(wb.isClassAlive(className), "should have been unloaded");
       
    91         ClassUnloadCommon.failIf(!wb.isClassAlive(interfaceName), "should be live here");
       
    92         System.out.println("We still have Interface referenced" + ic);
       
    93     }
       
    94 }
       
    95