Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/test/gc/class_unloading/TestClassUnloadingDisabled.java
32285 views
/*1* Copyright (c) 2018, 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* @test25* @key gc26* @bug 811482327* @requires vm.gc == null28* @requires vm.opt.ExplicitGCInvokesConcurrent != true29* @requires vm.opt.ClassUnloading != true30* @library /testlibrary /testlibrary/whitebox31* @build sun.hotspot.WhiteBox32* @run main ClassFileInstaller sun.hotspot.WhiteBox33* sun.hotspot.WhiteBox$WhiteBoxPermission34*35* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI36* -XX:-ClassUnloading -XX:+UseG1GC TestClassUnloadingDisabled37*38* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI39* -XX:-ClassUnloading -XX:+UseSerialGC TestClassUnloadingDisabled40*41* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI42* -XX:-ClassUnloading -XX:+UseParallelGC TestClassUnloadingDisabled43*44* @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI45* -XX:-ClassUnloading -XX:+UseConcMarkSweepGC TestClassUnloadingDisabled46*/4748import java.io.File;49import java.io.IOException;50import java.nio.file.Files;51import java.nio.file.Path;52import java.nio.file.Paths;5354import sun.hotspot.WhiteBox;5556import com.oracle.java.testlibrary.Asserts;5758public class TestClassUnloadingDisabled {59public static void main(String args[]) throws Exception {60final WhiteBox wb = WhiteBox.getWhiteBox();61// Fetch the dir where the test class and the class62// to be loaded resides.63String classDir = TestClassUnloadingDisabled.class.getProtectionDomain().getCodeSource().getLocation().getPath();64String className = "ClassToLoadUnload";6566Asserts.assertFalse(wb.isClassAlive(className), "Should not be loaded yet");6768// The NoPDClassLoader handles loading classes in the test directory69// and loads them without a protection domain, which in some cases70// keeps the class live regardless of marking state.71NoPDClassLoader nopd = new NoPDClassLoader(classDir);72nopd.loadClass(className);7374Asserts.assertTrue(wb.isClassAlive(className), "Class should be loaded");7576// Clear the class-loader, class and object references to make77// class unloading possible.78nopd = null;7980System.gc();81Asserts.assertTrue(wb.isClassAlive(className), "Class should not have ben unloaded");82}83}8485class NoPDClassLoader extends ClassLoader {86String path;8788NoPDClassLoader(String path) {89this.path = path;90}9192public Class<?> loadClass(String name) throws ClassNotFoundException {93byte[] cls = null;94File f = new File(path,name + ".class");9596// Delegate class loading if class not present in the given97// directory.98if (!f.exists()) {99return super.loadClass(name);100}101102try {103Path path = Paths.get(f.getAbsolutePath());104cls = Files.readAllBytes(path);105} catch (IOException e) {106throw new ClassNotFoundException(name);107}108109// Define class with no protection domain and resolve it.110return defineClass(name, cls, 0, cls.length, null);111}112}113114class ClassToLoadUnload {115}116117118