1 /* 2 * Copyright (c) 2016, 2018, 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 static jdk.test.lib.Asserts.*; 25 26 import java.lang.module.Configuration; 27 import java.lang.module.ModuleDescriptor; 28 import java.lang.module.ModuleFinder; 29 import java.util.HashMap; 30 import java.util.Map; 31 import java.util.Set; 32 33 // 34 // ClassLoader1 --> defines m1x --> packages p1 35 // ClassLoader1 --> defines m2x --> packages p2 36 // 37 // m1x can read m2x 38 // package p2 in m2x is exported to m1x 39 // 40 // class p1.c1 defined in m1x tries to access p2.c2 defined in m2x 41 // Access allowed since m1x can read m2x and package p2 is exported to m1x. 42 // 43 public class ModuleSameCLMain { 44 45 // Create a layer over the boot layer. 46 // Define modules within this layer to test access between 47 // publically defined classes within packages of those modules. 48 public void createLayerOnBoot() throws Throwable { 49 50 // Define module: m1x 51 // Can read: java.base, m2x 52 // Packages: p1 53 // Packages exported: p1 is exported to unqualifiedly 54 ModuleDescriptor descriptor_m1x = 55 ModuleDescriptor.newModule("m1x") 56 .requires("java.base") 57 .requires("m2x") 58 .exports("p1") 59 .build(); 60 61 // Define module: m2x 62 // Can read: java.base 63 // Packages: p2 64 // Packages exported: package p2 is exported to m1x 65 ModuleDescriptor descriptor_m2x = 66 ModuleDescriptor.newModule("m2x") 67 .requires("java.base") 68 .exports("p2", Set.of("m1x")) 69 .build(); 70 71 // Set up a ModuleFinder containing all modules for this layer. 72 ModuleFinder finder = ModuleLibrary.of(descriptor_m1x, descriptor_m2x); 73 74 // Resolves "m1x" 75 Configuration cf = ModuleLayer.boot() 76 .configuration() 77 .resolve(finder, ModuleFinder.of(), Set.of("m1x")); 78 79 // map each module to the same class loader for this test 80 Map<String, ClassLoader> map = new HashMap<>(); 81 Loader1 cl1 = new Loader1(); 82 map.put("m1x", cl1); 83 map.put("m2x", cl1); 84 85 // Create layer that contains m1x & m2x 86 ModuleLayer layer = ModuleLayer.boot().defineModules(cf, map::get); 87 assertTrue(layer.findLoader("m1x") == cl1); 88 assertTrue(layer.findLoader("m2x") == cl1); 89 assertTrue(layer.findLoader("java.base") == null); 90 91 // now use the same loader to load class p1.c1 92 Class p1_c1_class = cl1.loadClass("p1.c1"); 93 try { 94 p1_c1_class.newInstance(); 95 } catch (IllegalAccessError e) { 96 throw new RuntimeException("Test Failed, an IAE should not be thrown since p2 is exported qualifiedly to m1x"); 97 } 98 } 99 100 public static void main(String args[]) throws Throwable { 101 ModuleSameCLMain test = new ModuleSameCLMain(); 102 test.createLayerOnBoot(); 103 } 104 105 static class Loader1 extends ClassLoader { } 106 }