Path: blob/master/test/jdk/java/lang/Class/forName/modules/TestLayer.java
41159 views
/*1* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.module.Configuration;24import java.lang.module.ModuleFinder;25import java.lang.reflect.Method;26import java.net.URL;27import java.net.URLClassLoader;28import java.nio.file.Path;29import java.nio.file.Paths;30import java.util.Set;3132public class TestLayer {33private static final Path MODS_DIR = Paths.get("mods");34private static final Set<String> modules = Set.of("m1", "m2");3536public static void main(String[] args) throws Exception {37// disable security manager until Class.forName is called.38SecurityManager sm = System.getSecurityManager();39if (sm != null) {40System.setSecurityManager(null);41}4243ModuleFinder finder = ModuleFinder.of(MODS_DIR);4445Configuration parent = ModuleLayer.boot().configuration();46Configuration cf = parent.resolveAndBind(ModuleFinder.of(),47finder,48modules);4950ClassLoader scl = ClassLoader.getSystemClassLoader();51ModuleLayer layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, scl);5253Module m1 = layer.findModule("m1").get();54Module m2 = layer.findModule("m2").get();5556if (sm != null) {57System.setSecurityManager(sm);58}5960// find exported and non-exported class from a named module61findClass(m1, "p1.A");62findClass(m1, "p1.internal.B");63findClass(m2, "p2.C");6465// find class from unnamed module66ClassLoader ld = TestLayer.class.getClassLoader();67findClass(ld.getUnnamedModule(), "TestDriver");6869// check if clinit should not be initialized70// compile without module-path; so use reflection71Class<?> c = Class.forName(m1, "p1.Initializer");72Method m = c.getMethod("isInited");73Boolean isClinited = (Boolean) m.invoke(null);74if (isClinited.booleanValue()) {75throw new RuntimeException("clinit should not be invoked");76}77}7879static Class<?> findClass(Module module, String cn) {80Class<?> c = Class.forName(module, cn);81if (c == null) {82throw new RuntimeException(cn + " not found in " + module);83}84if (c.getModule() != module) {85throw new RuntimeException(c.getModule() + " != " + module);86}87return c;88}89}909192