1 /* 2 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. 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 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 // This is a utlitity test class for loading classes-under-test 26 // by means of custom class loader. 27 // See AppCDS/jvmti/transformRelatedClasses/TransformRelatedClasses.java 28 // for an example. 29 // Use this test app in conjunction with other tests 30 // to load and exercise classes using custom class loader(s). 31 // This class is intended to be called by the "main test driver" 32 // inside a child process, normally with sharing enabled. 33 // 34 // Arguments: customJarPath, loaderType, testClass 35 // customJarPath - a path to jar file containing classes for 36 // loading via this custom class loader, including the 37 // testClass 38 // loaderType - Currently only "unregistered" 39 // (Fingerprint verification method) is allowed 40 // testClass - the class to be loader; the test method with 41 // signature 'public static void test()' will be called 42 // on this class, so class must contain such method 43 44 45 import java.io.File; 46 import java.lang.reflect.Method; 47 import java.net.URL; 48 import java.net.URLClassLoader; 49 import java.util.logging.Logger; 50 51 public class CustomLoaderApp { 52 public static void ping() {}; 53 54 private static void log(String msg) { 55 System.out.println("CustomLoaderApp: " + msg); 56 } 57 58 public static void main(String[] args) throws Exception { 59 String path = args[0]; 60 URL url = new File(path).toURI().toURL(); 61 URL[] urls = new URL[] {url}; 62 63 String loaderType = args[1]; 64 log("loaderType = " + loaderType); 65 66 String testClass = args[2]; 67 log("testClass = " + testClass); 68 69 switch(loaderType) { 70 case "unregistered": 71 loadAndUseWithUnregisteredLoader(urls, testClass); 72 break; 73 default: 74 throw new IllegalArgumentException("loader type is wrong: " + loaderType); 75 } 76 } 77 78 79 // Load the test classes using unregistered loader 80 // (i.e. loader that is not using AppCDS API) 81 private static void loadAndUseWithUnregisteredLoader(URL[] urls, String testClass) 82 throws Exception { 83 URLClassLoader urlClassLoader = new URLClassLoader(urls); 84 callTestMethod(loadAndCheck(urlClassLoader, testClass)); 85 } 86 87 private static Class loadAndCheck(ClassLoader loader, String className) 88 throws ClassNotFoundException { 89 Class c = loader.loadClass(className); 90 log("class =" + c); 91 log("loader = " + c.getClassLoader()); 92 93 // Check that c is defined by the correct loader 94 if (c.getClassLoader() != loader) { 95 String msg = String.format("c.getClassLoader() equals to <%s>, expected <%s>", 96 c.getClassLoader(), loader); 97 throw new RuntimeException(msg); 98 } 99 return c; 100 } 101 102 private static void callTestMethod(Class c) throws Exception { 103 Method[] methods = c.getDeclaredMethods(); 104 for (Method m : methods) { 105 log("method = " + m.getName()); 106 if (m.getName().equals("test")) 107 m.invoke(null); 108 } 109 } 110 }