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