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  *          jdk.compiler
  31  *          jdk.internal.jvmstat/sun.jvmstat.monitor
  32  * @run testng ClassLoaderHierarchyTest
  33  */
  34 
  35 import org.testng.Assert;
  36 import org.testng.annotations.Test;
  37 
  38 import jdk.test.lib.process.OutputAnalyzer;
  39 import jdk.test.lib.dcmd.CommandExecutor;
  40 import jdk.test.lib.dcmd.JMXExecutor;
  41 
  42 import java.io.File;
  43 import java.io.FileInputStream;
  44 import java.io.IOException;
  45 import java.nio.ByteBuffer;
  46 import java.nio.channels.FileChannel;
  47 
  48 public class ClassLoaderHierarchyTest {
  49 
  50 //+-- <bootstrap>
  51 //      |
  52 //      +-- "platform", jdk.internal.loader.ClassLoaders$PlatformClassLoader
  53 //      |     |
  54 //      |     +-- "app", jdk.internal.loader.ClassLoaders$AppClassLoader
  55 //      |
  56 //      +-- jdk.internal.reflect.DelegatingClassLoader
  57 //      |
  58 //      +-- "Kevin", ClassLoaderHierarchyTest$TestClassLoader
  59 //      |
  60 //      +-- ClassLoaderHierarchyTest$TestClassLoader
  61 //            |
  62 //            +-- "Bill", ClassLoaderHierarchyTest$TestClassLoader
  63 
  64     public void run(CommandExecutor executor) throws ClassNotFoundException {
  65 
  66         ClassLoader unnamed_cl = new TestClassLoader(null, null);
  67         Class<?> c1 = Class.forName("TestClass2", true, unnamed_cl);
  68         if (c1.getClassLoader() != unnamed_cl) {
  69             Assert.fail("TestClass defined by wrong classloader: " + c1.getClassLoader());
  70         }
  71 
  72         ClassLoader named_cl = new TestClassLoader("Kevin", null);
  73         Class<?> c2 = Class.forName("TestClass2", true, named_cl);
  74         if (c2.getClassLoader() != named_cl) {
  75             Assert.fail("TestClass defined by wrong classloader: " + c2.getClassLoader());
  76         }
  77 
  78         ClassLoader named_child_cl = new TestClassLoader("Bill", unnamed_cl);
  79         Class<?> c3 = Class.forName("TestClass2", true, named_child_cl);
  80         if (c3.getClassLoader() != named_child_cl) {
  81             Assert.fail("TestClass defined by wrong classloader: " + c3.getClassLoader());
  82         }
  83 
  84         // First test: simple output, no classes displayed
  85         OutputAnalyzer output = executor.execute("VM.classloaders");
  86         output.shouldContain("<bootstrap>");
  87         output.shouldMatch(".*TestClassLoader");
  88         output.shouldMatch("Kevin.*TestClassLoader");
  89         output.shouldMatch("Bill.*TestClassLoader");
  90 
  91         // Second test: print with classes.
  92         output = executor.execute("VM.classloaders show-classes");
  93         output.shouldContain("<bootstrap>");
  94         output.shouldContain("java.lang.Object");
  95         output.shouldMatch(".*TestClassLoader");
  96         output.shouldMatch("Kevin.*TestClassLoader");
  97         output.shouldMatch("Bill.*TestClassLoader");
  98         output.shouldContain("TestClass2");
  99         output.shouldContain("Hidden Classes:");
 100     }
 101 
 102     static class TestClassLoader extends ClassLoader {
 103 
 104         public TestClassLoader() {
 105             super();
 106         }
 107 
 108         public TestClassLoader(String name, ClassLoader parent) {
 109             super(name, parent);
 110         }
 111 
 112         public static final String CLASS_NAME = "TestClass2";
 113 
 114         static ByteBuffer readClassFile(String name)
 115         {
 116             File f = new File(System.getProperty("test.classes", "."),
 117                               name);
 118             try (FileInputStream fin = new FileInputStream(f);
 119                  FileChannel fc = fin.getChannel())
 120             {
 121                 return fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
 122             } catch (IOException e) {
 123                 Assert.fail("Can't open file: " + name, e);
 124             }
 125 
 126             /* Will not reach here as Assert.fail() throws exception */
 127             return null;
 128         }
 129 
 130         protected Class<?> loadClass(String name, boolean resolve)
 131             throws ClassNotFoundException
 132         {
 133             Class<?> c;
 134             if (!CLASS_NAME.equals(name)) {
 135                 c = super.loadClass(name, resolve);
 136             } else {
 137                 // should not delegate to the system class loader
 138                 c = findClass(name);
 139                 if (resolve) {
 140                     resolveClass(c);
 141                 }
 142             }
 143             return c;
 144         }
 145 
 146         protected Class<?> findClass(String name)
 147             throws ClassNotFoundException
 148         {
 149             if (!CLASS_NAME.equals(name)) {
 150                 throw new ClassNotFoundException("Unexpected class: " + name);
 151             }
 152             return defineClass(name, readClassFile(name + ".class"), null);
 153         }
 154 
 155     }
 156 
 157     @Test
 158     public void jmx() throws ClassNotFoundException {
 159         run(new JMXExecutor());
 160     }
 161 
 162 }
 163 
 164 class TestClass2 {
 165     static {
 166         Runnable r = () -> System.out.println("Hello");
 167         r.run();
 168     }
 169 }
 170