1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reqserved.
   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
  26  * @key gc
  27  * @bug 8114823
  28  * @requires vm.gc == null
  29  * @requires vm.opt.ExplicitGCInvokesConcurrent != true
  30  * @requires vm.opt.ClassUnloading != true
  31  * @library /test/lib
  32  * @modules java.base/jdk.internal.misc
  33  *          java.management
  34  * @build sun.hotspot.WhiteBox
  35  * @run main ClassFileInstaller sun.hotspot.WhiteBox
  36  *                              sun.hotspot.WhiteBox$WhiteBoxPermission
  37  *
  38  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  39  *                   -XX:-ClassUnloading -XX:+UseG1GC TestClassUnloadingDisabled
  40  *
  41  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  42  *                   -XX:-ClassUnloading -XX:+UseSerialGC TestClassUnloadingDisabled
  43  *
  44  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  45  *                   -XX:-ClassUnloading -XX:+UseParallelGC TestClassUnloadingDisabled
  46  *
  47  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI
  48  *                   -XX:-ClassUnloading -XX:+UseConcMarkSweepGC TestClassUnloadingDisabled
  49  */
  50 
  51 import java.io.File;
  52 import java.io.IOException;
  53 import java.nio.file.Files;
  54 import java.nio.file.Path;
  55 import java.nio.file.Paths;
  56 
  57 import sun.hotspot.WhiteBox;
  58 
  59 import static jdk.test.lib.Asserts.*;
  60 
  61 public class TestClassUnloadingDisabled {
  62     public static void main(String args[]) throws Exception {
  63         final WhiteBox wb = WhiteBox.getWhiteBox();
  64         // Fetch the dir where the test class and the class
  65         // to be loaded resides.
  66         String classDir = TestClassUnloadingDisabled.class.getProtectionDomain().getCodeSource().getLocation().getPath();
  67         String className = "ClassToLoadUnload";
  68 
  69         assertFalse(wb.isClassAlive(className), "Should not be loaded yet");
  70 
  71         // The NoPDClassLoader handles loading classes in the test directory
  72         // and loads them without a protection domain, which in some cases
  73         // keeps the class live regardless of marking state.
  74         NoPDClassLoader nopd = new NoPDClassLoader(classDir);
  75         nopd.loadClass(className);
  76 
  77         assertTrue(wb.isClassAlive(className), "Class should be loaded");
  78 
  79         // Clear the class-loader, class and object references to make
  80         // class unloading possible.
  81         nopd = null;
  82 
  83         System.gc();
  84         assertTrue(wb.isClassAlive(className), "Class should not have ben unloaded");
  85     }
  86 }
  87 
  88 class NoPDClassLoader extends ClassLoader {
  89     String path;
  90 
  91     NoPDClassLoader(String path) {
  92         this.path = path;
  93     }
  94 
  95     public Class<?> loadClass(String name) throws ClassNotFoundException {
  96         byte[] cls = null;
  97         File f = new File(path,name + ".class");
  98 
  99         // Delegate class loading if class not present in the given
 100         // directory.
 101         if (!f.exists()) {
 102             return super.loadClass(name);
 103         }
 104 
 105         try {
 106             Path path = Paths.get(f.getAbsolutePath());
 107             cls = Files.readAllBytes(path);
 108         } catch (IOException e) {
 109             throw new ClassNotFoundException(name);
 110         }
 111 
 112         // Define class with no protection domain and resolve it.
 113         return defineClass(name, cls, 0, cls.length, null);
 114     }
 115 }
 116 
 117 class ClassToLoadUnload {
 118 }