1/* 2 * Copyright (c) 2016, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26import static jdk.test.lib.Asserts.*; 27 28import java.lang.module.Configuration; 29import java.lang.module.ModuleDescriptor; 30import java.lang.module.ModuleFinder; 31import java.util.HashMap; 32import java.util.Map; 33import java.util.Set; 34 35// 36// ClassLoader1 --> defines m1x --> packages p1 37// ClassLoader1 --> defines m2x --> packages p2 38// 39// m1x can read m2x 40// package p2 in m2x is exported to m1x 41// 42// class p1.c1 defined in m1x tries to access p2.c2 defined in m2x 43// Access allowed since m1x can read m2x and package p2 is exported to m1x. 44// 45public class ModuleSameCLMain { 46 47 // Create a layer over the boot layer. 48 // Define modules within this layer to test access between 49 // publically defined classes within packages of those modules. 50 public void createLayerOnBoot() throws Throwable { 51 52 // Define module: m1x 53 // Can read: java.base, m2x 54 // Packages: p1 55 // Packages exported: p1 is exported to unqualifiedly 56 ModuleDescriptor descriptor_m1x = 57 ModuleDescriptor.newModule("m1x") 58 .requires("java.base") 59 .requires("m2x") 60 .exports("p1") 61 .build(); 62 63 // Define module: m2x 64 // Can read: java.base 65 // Packages: p2 66 // Packages exported: package p2 is exported to m1x 67 ModuleDescriptor descriptor_m2x = 68 ModuleDescriptor.newModule("m2x") 69 .requires("java.base") 70 .exports("p2", Set.of("m1x")) 71 .build(); 72 73 // Set up a ModuleFinder containing all modules for this layer. 74 ModuleFinder finder = ModuleLibrary.of(descriptor_m1x, descriptor_m2x); 75 76 // Resolves "m1x" 77 Configuration cf = ModuleLayer.boot() 78 .configuration() 79 .resolve(finder, ModuleFinder.of(), Set.of("m1x")); 80 81 // map each module to the same class loader for this test 82 Map<String, ClassLoader> map = new HashMap<>(); 83 Loader1 cl1 = new Loader1(); 84 map.put("m1x", cl1); 85 map.put("m2x", cl1); 86 87 // Create layer that contains m1x & m2x 88 ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get); 89 assertTrue(layer.findLoader("m1x") == cl1); 90 assertTrue(layer.findLoader("m2x") == cl1); 91 assertTrue(layer.findLoader("java.base") == null); 92 93 // now use the same loader to load class p1.c1 94 Class p1_c1_class = cl1.loadClass("p1.c1"); 95 try { 96 p1_c1_class.newInstance(); 97 } catch (IllegalAccessError e) { 98 throw new RuntimeException("Test Failed, an IAE should not be thrown since p2 is exported qualifiedly to m1x"); 99 } 100 } 101 102 public static void main(String args[]) throws Throwable { 103 ModuleSameCLMain test = new ModuleSameCLMain(); 104 test.createLayerOnBoot(); 105 } 106 107 static class Loader1 extends ClassLoader { } 108} 109