1/* 2 * Copyright (c) 2016, 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 24import java.lang.module.Configuration; 25import java.lang.module.ModuleFinder; 26import java.lang.reflect.Method; 27import java.net.URL; 28import java.net.URLClassLoader; 29import java.nio.file.Path; 30import java.nio.file.Paths; 31import java.util.Set; 32 33public class TestLayer { 34 private static final Path MODS_DIR = Paths.get("mods"); 35 private static final Set<String> modules = Set.of("m1", "m2"); 36 37 public static void main(String[] args) throws Exception { 38 // disable security manager until Class.forName is called. 39 SecurityManager sm = System.getSecurityManager(); 40 if (sm != null) { 41 System.setSecurityManager(null); 42 } 43 44 ModuleFinder finder = ModuleFinder.of(MODS_DIR); 45 46 Configuration parent = ModuleLayer.boot().configuration(); 47 Configuration cf = parent.resolveAndBind(ModuleFinder.of(), 48 finder, 49 modules); 50 51 ClassLoader scl = ClassLoader.getSystemClassLoader(); 52 ModuleLayer layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, scl); 53 54 Module m1 = layer.findModule("m1").get(); 55 Module m2 = layer.findModule("m2").get(); 56 57 if (sm != null) { 58 System.setSecurityManager(sm); 59 } 60 61 // find exported and non-exported class from a named module 62 findClass(m1, "p1.A"); 63 findClass(m1, "p1.internal.B"); 64 findClass(m2, "p2.C"); 65 66 // find class from unnamed module 67 ClassLoader ld = TestLayer.class.getClassLoader(); 68 findClass(ld.getUnnamedModule(), "TestDriver"); 69 70 // check if clinit should not be initialized 71 // compile without module-path; so use reflection 72 Class<?> c = Class.forName(m1, "p1.Initializer"); 73 Method m = c.getMethod("isInited"); 74 Boolean isClinited = (Boolean) m.invoke(null); 75 if (isClinited.booleanValue()) { 76 throw new RuntimeException("clinit should not be invoked"); 77 } 78 } 79 80 static Class<?> findClass(Module module, String cn) { 81 Class<?> c = Class.forName(module, cn); 82 if (c == null) { 83 throw new RuntimeException(cn + " not found in " + module); 84 } 85 if (c.getModule() != module) { 86 throw new RuntimeException(c.getModule() + " != " + module); 87 } 88 return c; 89 } 90} 91