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 import java.lang.*; 31 import java.lang.reflect.*; 32 import sun.hotspot.WhiteBox; 33 34 35 public class LimitModsHelper { 36 static final ClassLoader PLATFORM_LOADER = ClassLoader.getPlatformClassLoader(); 37 static final ClassLoader SYS_LOADER = ClassLoader.getSystemClassLoader(); 38 39 public static void main(String[] args) throws Exception { 40 assertTrue(args.length == 4); 41 String[] classNames = new String[3]; 42 for (int i = 0; i < 3; i++) { 43 classNames[i] = args[i].replace('/', '.'); 44 } 45 int excludeModIdx = Integer.parseInt(args[3]); 46 47 ClassLoader expectedLoaders[] = {null, PLATFORM_LOADER, SYS_LOADER}; 48 49 WhiteBox wb = WhiteBox.getWhiteBox(); 50 51 Class<?> clazz = null; 52 for (int i = 0; i < 3; i++) { 53 try { 54 // Load the class with the default ClassLoader. 55 clazz = Class.forName(classNames[i]); 56 } catch (Exception e) { 57 if (i == excludeModIdx) { 58 System.out.println(classNames[i] + " not found as expected because the module isn't in the --limit-modules - PASSED"); 59 } else { 60 throw(e); 61 } 62 } 63 64 if (clazz != null && i != excludeModIdx) { 65 // Make sure we got the expected defining ClassLoader 66 testLoader(clazz, expectedLoaders[i]); 67 68 // Make sure the class is in the shared space 69 if (!wb.isSharedClass(clazz)) { 70 throw new RuntimeException(clazz.getName() + 71 ".class should be in the shared space. " + 72 "loader=" + clazz.getClassLoader() + " module=" + clazz.getModule().getName()); 73 } 74 } 75 clazz = null; 76 } 77 } 78 79 /** 80 * Asserts that given class has the expected defining loader. 81 */ 82 static void testLoader(Class<?> clazz, ClassLoader expected) { 83 ClassLoader loader = clazz.getClassLoader(); 84 if (loader != expected) { 85 throw new RuntimeException(clazz + " loaded by " + loader + ", expected " + expected); 86 } 87 } 88 89 static void assertTrue(boolean expr) { 90 if (!expr) 91 throw new RuntimeException("assertion failed"); 92 } 93 }