1 /* 2 * Copyright (c) 2015, 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 /** 26 * Used with -p or --upgrade-module-path to exercise the replacement 27 * of classes in modules that are linked into the runtime image. 28 */ 29 30 package jdk.test; 31 32 public class Main { 33 static final ClassLoader PLATFORM_LOADER = ClassLoader.getPlatformClassLoader(); 34 static final ClassLoader SYS_LOADER = ClassLoader.getSystemClassLoader(); 35 36 public static void main(String[] args) throws Exception { 37 ClassLoader loader = null; 38 boolean shouldOverride = false; 39 40 /* 41 * 3 Arguments are passed to this test: 42 * 1. className: Name of the class to load. 43 * 2. loaderName: Either "platform" or "app", which specifies which ClassLoader is expected 44 * to be the defining ClassLoader once the class is loaded. The initiating 45 * ClassLoader is always the default ClassLoader (which should be the 46 * app (system) ClassLoader. 47 * 3. shouldOverride: Either "true" or "false" to indicate whether the loaded class 48 * should be the one we are attempting to override with (not the archived version). 49 */ 50 51 assertTrue(args.length == 3, "Unexpected number of arguments: expected 3, actual " + args.length); 52 String className = args[0].replace('/', '.'); 53 String loaderName = args[1]; // "platform" or "app" 54 String shouldOverrideName = args[2]; // "true" or "false" 55 56 if (loaderName.equals("app")) { 57 loader = SYS_LOADER; 58 } else if (loaderName.equals("platform")) { 59 loader = PLATFORM_LOADER; 60 } else { 61 assertTrue(false); 62 } 63 64 if (shouldOverrideName.equals("true")) { 65 shouldOverride = true; 66 } else if (shouldOverrideName.equals("false")) { 67 shouldOverride = false; 68 } else { 69 assertTrue(false); 70 } 71 72 // Load the class with the default ClassLoader. 73 Class<?> clazz = Class.forName(className, true, loader); 74 // Make sure we got the expected defining ClassLoader 75 testLoader(clazz, loader); 76 // Create an instance and see what toString() returns 77 String s = clazz.newInstance().toString(); 78 // The overridden version of the class should return "hi". Make sure 79 // it does only if we are expecting to have loaded the overridden version. 80 assertTrue(s.equals("hi") == shouldOverride); 81 } 82 83 /** 84 * Asserts that given class has the expected defining loader. 85 */ 86 static void testLoader(Class<?> clazz, ClassLoader expected) { 87 ClassLoader loader = clazz.getClassLoader(); 88 if (loader != expected) { 89 throw new RuntimeException(clazz + " loaded by " + loader + ", expected " + expected); 90 } 91 } 92 93 static void assertTrue(boolean expr) { 94 assertTrue(expr, ""); 95 } 96 97 static void assertTrue(boolean expr, String msg) { 98 if (!expr) 99 throw new RuntimeException("assertion failed: " + msg); 100 } 101 }