Umod_PkgNotExp.java revision 10420:c558850fac57
1172677Smlaier/* 2172677Smlaier * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3172677Smlaier * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4235426Sdelphij * 5172677Smlaier * This code is free software; you can redistribute it and/or modify it 6172677Smlaier * under the terms of the GNU General Public License version 2 only, as 7172677Smlaier * published by the Free Software Foundation. Oracle designates this 8172677Smlaier * particular file as subject to the "Classpath" exception as provided 9235426Sdelphij * by Oracle in the LICENSE file that accompanied this code. 10172677Smlaier * 11172677Smlaier * This code is distributed in the hope that it will be useful, but WITHOUT 12172677Smlaier * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13172677Smlaier * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14235426Sdelphij * version 2 for more details (a copy is included in the LICENSE file that 15235426Sdelphij * accompanied this code). 16172677Smlaier * 17235426Sdelphij * You should have received a copy of the GNU General Public License version 18235426Sdelphij * 2 along with this work; if not, write to the Free Software Foundation, 19235426Sdelphij * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20235426Sdelphij * 21235426Sdelphij * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22235426Sdelphij * or visit www.oracle.com if you need additional information or have any 23235426Sdelphij * questions. 24172677Smlaier */ 25235426Sdelphij 26235426Sdelphij/* 27235426Sdelphij * @test 28235426Sdelphij * @summary Test if package p2 in module m2 is not exported, then class p1.c1 29235426Sdelphij * in an unnamed module can not access p2.c2 in module m2. 30235426Sdelphij * @library /testlibrary /test/lib 31235426Sdelphij * @compile myloaders/MySameClassLoader.java 32235426Sdelphij * @compile p2/c2.java 33235426Sdelphij * @compile p1/c1.java 34235426Sdelphij * @build Umod_PkgNotExp 35235426Sdelphij * @run main/othervm -Xbootclasspath/a:. Umod_PkgNotExp 36235426Sdelphij */ 37235426Sdelphij 38235426Sdelphijimport static jdk.test.lib.Asserts.*; 39235426Sdelphij 40235426Sdelphijimport java.lang.reflect.Layer; 41235426Sdelphijimport java.lang.module.Configuration; 42235426Sdelphijimport java.lang.module.ModuleDescriptor; 43235426Sdelphijimport java.lang.module.ModuleFinder; 44172677Smlaierimport java.util.HashMap; 45235426Sdelphijimport java.util.Map; 46172677Smlaierimport java.util.Set; 47172677Smlaierimport myloaders.MySameClassLoader; 48235426Sdelphij 49235426Sdelphij// ClassLoader1 --> defines m1 --> no packages 50235426Sdelphij// defines m2 --> packages p2 51172677Smlaier// 52172677Smlaier// m1 can read m2 53172677Smlaier// package p2 in m2 is not exported 54172677Smlaier// 55235426Sdelphij// class p1.c1 defined in an unnamed module tries to access p2.c2 defined in m2 56235426Sdelphij// Access denied since p2 is not exported. 57172677Smlaier// 58172677Smlaierpublic class Umod_PkgNotExp { 59172677Smlaier 60172677Smlaier // Create a Layer over the boot layer. 61172677Smlaier // Define modules within this layer to test access between 62235426Sdelphij // publically defined classes within packages of those modules. 63235426Sdelphij public void createLayerOnBoot() throws Throwable { 64235426Sdelphij 65235426Sdelphij // Define module: m1 66235426Sdelphij // Can read: java.base, m2 67235426Sdelphij // Packages: none 68235426Sdelphij // Packages exported: none 69241231Sdelphij ModuleDescriptor descriptor_m1 = 70235426Sdelphij new ModuleDescriptor.Builder("m1") 71241231Sdelphij .requires("java.base") 72241231Sdelphij .requires("m2") 73235426Sdelphij .build(); 74235426Sdelphij 75235426Sdelphij // Define module: m2 76235426Sdelphij // Can read: java.base 77235426Sdelphij // Packages: p2 78 // Packages exported: none 79 ModuleDescriptor descriptor_m2 = 80 new ModuleDescriptor.Builder("m2") 81 .requires("java.base") 82 .conceals("p2") 83 .build(); 84 85 // Set up a ModuleFinder containing all modules for this layer. 86 ModuleFinder finder = ModuleLibrary.of(descriptor_m1, descriptor_m2); 87 88 // Resolves "m1" 89 Configuration cf = Layer.boot() 90 .configuration() 91 .resolveRequires(finder, ModuleFinder.empty(), Set.of("m1")); 92 93 // map each module to the same class loader for this test 94 Map<String, ClassLoader> map = new HashMap<>(); 95 map.put("m1", MySameClassLoader.loader1); 96 map.put("m2", MySameClassLoader.loader1); 97 98 // Create Layer that contains m1 and m2 99 Layer layer = Layer.boot().defineModules(cf, map::get); 100 101 assertTrue(layer.findLoader("m1") == MySameClassLoader.loader1); 102 assertTrue(layer.findLoader("m2") == MySameClassLoader.loader1); 103 assertTrue(layer.findLoader("java.base") == null); 104 105 // now use the same loader to load class p1.c1 106 Class p1_c1_class = MySameClassLoader.loader1.loadClass("p1.c1"); 107 try { 108 p1_c1_class.newInstance(); 109 throw new RuntimeException("Failed to get IAE (p2 in m2 is not exported)"); 110 } catch (IllegalAccessError e) { 111 System.out.println(e.getMessage()); 112 if (!e.getMessage().contains("does not export")) { 113 throw new RuntimeException("Wrong message: " + e.getMessage()); 114 } 115 } 116 } 117 118 public static void main(String args[]) throws Throwable { 119 Umod_PkgNotExp test = new Umod_PkgNotExp(); 120 test.createLayerOnBoot(); 121 } 122} 123