Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/vm/ClassLoaderHierarchyTest.java
41153 views
/*1* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2018 SAP SE. 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 it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 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 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* @test26* @summary Test of diagnostic command VM.classloaders27* @library /test/lib28* @modules java.base/jdk.internal.misc29* jdk.compiler30* jdk.internal.jvmstat/sun.jvmstat.monitor31* @run testng ClassLoaderHierarchyTest32*/3334import org.testng.Assert;35import org.testng.annotations.Test;3637import jdk.test.lib.process.OutputAnalyzer;38import jdk.test.lib.dcmd.CommandExecutor;39import jdk.test.lib.dcmd.JMXExecutor;4041import java.io.File;42import java.io.FileInputStream;43import java.io.IOException;44import java.nio.ByteBuffer;45import java.nio.channels.FileChannel;4647public class ClassLoaderHierarchyTest {4849//+-- <bootstrap>50// |51// +-- "platform", jdk.internal.loader.ClassLoaders$PlatformClassLoader52// | |53// | +-- "app", jdk.internal.loader.ClassLoaders$AppClassLoader54// |55// +-- jdk.internal.reflect.DelegatingClassLoader56// |57// +-- "Kevin", ClassLoaderHierarchyTest$TestClassLoader58// |59// +-- ClassLoaderHierarchyTest$TestClassLoader60// |61// +-- "Bill", ClassLoaderHierarchyTest$TestClassLoader6263public void run(CommandExecutor executor) throws ClassNotFoundException {6465ClassLoader unnamed_cl = new TestClassLoader(null, null);66Class<?> c1 = Class.forName("TestClass2", true, unnamed_cl);67if (c1.getClassLoader() != unnamed_cl) {68Assert.fail("TestClass defined by wrong classloader: " + c1.getClassLoader());69}7071ClassLoader named_cl = new TestClassLoader("Kevin", null);72Class<?> c2 = Class.forName("TestClass2", true, named_cl);73if (c2.getClassLoader() != named_cl) {74Assert.fail("TestClass defined by wrong classloader: " + c2.getClassLoader());75}7677ClassLoader named_child_cl = new TestClassLoader("Bill", unnamed_cl);78Class<?> c3 = Class.forName("TestClass2", true, named_child_cl);79if (c3.getClassLoader() != named_child_cl) {80Assert.fail("TestClass defined by wrong classloader: " + c3.getClassLoader());81}8283// First test: simple output, no classes displayed84OutputAnalyzer output = executor.execute("VM.classloaders");85output.shouldContain("<bootstrap>");86output.shouldMatch(".*TestClassLoader");87output.shouldMatch("Kevin.*TestClassLoader");88output.shouldMatch("Bill.*TestClassLoader");8990// Second test: print with classes.91output = executor.execute("VM.classloaders show-classes");92output.shouldContain("<bootstrap>");93output.shouldContain("java.lang.Object");94output.shouldMatch(".*TestClassLoader");95output.shouldMatch("Kevin.*TestClassLoader");96output.shouldMatch("Bill.*TestClassLoader");97output.shouldContain("TestClass2");98output.shouldContain("Hidden Classes:");99}100101static class TestClassLoader extends ClassLoader {102103public TestClassLoader() {104super();105}106107public TestClassLoader(String name, ClassLoader parent) {108super(name, parent);109}110111public static final String CLASS_NAME = "TestClass2";112113static ByteBuffer readClassFile(String name)114{115File f = new File(System.getProperty("test.classes", "."),116name);117try (FileInputStream fin = new FileInputStream(f);118FileChannel fc = fin.getChannel())119{120return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());121} catch (IOException e) {122Assert.fail("Can't open file: " + name, e);123}124125/* Will not reach here as Assert.fail() throws exception */126return null;127}128129protected Class<?> loadClass(String name, boolean resolve)130throws ClassNotFoundException131{132Class<?> c;133if (!CLASS_NAME.equals(name)) {134c = super.loadClass(name, resolve);135} else {136// should not delegate to the system class loader137c = findClass(name);138if (resolve) {139resolveClass(c);140}141}142return c;143}144145protected Class<?> findClass(String name)146throws ClassNotFoundException147{148if (!CLASS_NAME.equals(name)) {149throw new ClassNotFoundException("Unexpected class: " + name);150}151return defineClass(name, readClassFile(name + ".class"), null);152}153154}155156@Test157public void jmx() throws ClassNotFoundException {158run(new JMXExecutor());159}160161}162163class TestClass2 {164static {165Runnable r = () -> System.out.println("Hello");166r.run();167}168}169170171172