1 /* 2 * Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2018, SAP SE. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /* 26 * @test 27 * @summary Test of diagnostic command VM.classloaders 28 * @library /test/lib 29 * @modules java.base/jdk.internal.misc 30 * java.compiler 31 * java.management 32 * jdk.internal.jvmstat/sun.jvmstat.monitor 33 * @run testng ClassLoaderHierarchyTest 34 */ 35 36 import org.testng.Assert; 37 import org.testng.annotations.Test; 38 39 import jdk.test.lib.process.OutputAnalyzer; 40 import jdk.test.lib.dcmd.CommandExecutor; 41 import jdk.test.lib.dcmd.JMXExecutor; 42 43 import java.io.File; 44 import java.io.FileInputStream; 45 import java.io.IOException; 46 import java.nio.ByteBuffer; 47 import java.nio.channels.FileChannel; 48 49 public class ClassLoaderHierarchyTest { 50 51 //+-- <bootstrap> 52 // | 53 // +-- "platform", jdk.internal.loader.ClassLoaders$PlatformClassLoader 54 // | | 55 // | +-- "app", jdk.internal.loader.ClassLoaders$AppClassLoader 56 // | 57 // +-- jdk.internal.reflect.DelegatingClassLoader 58 // | 59 // +-- "Kevin", ClassLoaderHierarchyTest$TestClassLoader 60 // | 61 // +-- ClassLoaderHierarchyTest$TestClassLoader 62 // | 63 // +-- "Bill", ClassLoaderHierarchyTest$TestClassLoader 64 65 public void run(CommandExecutor executor) throws ClassNotFoundException { 66 67 ClassLoader unnamed_cl = new TestClassLoader(null, null); 68 Class<?> c1 = Class.forName("TestClass2", true, unnamed_cl); 69 if (c1.getClassLoader() != unnamed_cl) { 70 Assert.fail("TestClass defined by wrong classloader: " + c1.getClassLoader()); 71 } 72 73 ClassLoader named_cl = new TestClassLoader("Kevin", null); 74 Class<?> c2 = Class.forName("TestClass2", true, named_cl); 75 if (c2.getClassLoader() != named_cl) { 76 Assert.fail("TestClass defined by wrong classloader: " + c2.getClassLoader()); 77 } 78 79 ClassLoader named_child_cl = new TestClassLoader("Bill", unnamed_cl); 80 Class<?> c3 = Class.forName("TestClass2", true, named_child_cl); 81 if (c3.getClassLoader() != named_child_cl) { 82 Assert.fail("TestClass defined by wrong classloader: " + c3.getClassLoader()); 83 } 84 85 // First test: simple output, no classes displayed 86 OutputAnalyzer output = executor.execute("VM.classloaders"); 87 output.shouldContain("<bootstrap>"); 88 output.shouldMatch(".*TestClassLoader"); 89 output.shouldMatch("Kevin.*TestClassLoader"); 90 output.shouldMatch("Bill.*TestClassLoader"); 91 92 // Second test: print with classes. 93 output = executor.execute("VM.classloaders show-classes"); 94 output.shouldContain("<bootstrap>"); 95 output.shouldContain("java.lang.Object"); 96 output.shouldMatch(".*TestClassLoader"); 97 output.shouldMatch("Kevin.*TestClassLoader"); 98 output.shouldMatch("Bill.*TestClassLoader"); 99 output.shouldContain("TestClass2"); 100 output.shouldContain("Weak Hidden Classes:"); 101 } 102 103 static class TestClassLoader extends ClassLoader { 104 105 public TestClassLoader() { 106 super(); 107 } 108 109 public TestClassLoader(String name, ClassLoader parent) { 110 super(name, parent); 111 } 112 113 public static final String CLASS_NAME = "TestClass2"; 114 115 static ByteBuffer readClassFile(String name) 116 { 117 File f = new File(System.getProperty("test.classes", "."), 118 name); 119 try (FileInputStream fin = new FileInputStream(f); 120 FileChannel fc = fin.getChannel()) 121 { 122 return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); 123 } catch (IOException e) { 124 Assert.fail("Can't open file: " + name, e); 125 } 126 127 /* Will not reach here as Assert.fail() throws exception */ 128 return null; 129 } 130 131 protected Class<?> loadClass(String name, boolean resolve) 132 throws ClassNotFoundException 133 { 134 Class<?> c; 135 if (!CLASS_NAME.equals(name)) { 136 c = super.loadClass(name, resolve); 137 } else { 138 // should not delegate to the system class loader 139 c = findClass(name); 140 if (resolve) { 141 resolveClass(c); 142 } 143 } 144 return c; 145 } 146 147 protected Class<?> findClass(String name) 148 throws ClassNotFoundException 149 { 150 if (!CLASS_NAME.equals(name)) { 151 throw new ClassNotFoundException("Unexpected class: " + name); 152 } 153 return defineClass(name, readClassFile(name + ".class"), null); 154 } 155 156 } 157 158 @Test 159 public void jmx() throws ClassNotFoundException { 160 run(new JMXExecutor()); 161 } 162 163 } 164 165 class TestClass2 { 166 static { 167 Runnable r = () -> System.out.println("Hello"); 168 r.run(); 169 } 170 } 171