AccessCheckAllUnnamed.java revision 12585:8c7769a2e1fe
1219019Sgabor/* 2219019Sgabor * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. 3219019Sgabor * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4219019Sgabor * 5219019Sgabor * This code is free software; you can redistribute it and/or modify it 6219019Sgabor * under the terms of the GNU General Public License version 2 only, as 7219019Sgabor * published by the Free Software Foundation. 8219019Sgabor * 9219019Sgabor * This code is distributed in the hope that it will be useful, but WITHOUT 10219019Sgabor * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11219019Sgabor * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12219019Sgabor * version 2 for more details (a copy is included in the LICENSE file that 13219019Sgabor * accompanied this code). 14219019Sgabor * 15219019Sgabor * You should have received a copy of the GNU General Public License version 16219019Sgabor * 2 along with this work; if not, write to the Free Software Foundation, 17219019Sgabor * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18219019Sgabor * 19219019Sgabor * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20219019Sgabor * or visit www.oracle.com if you need additional information or have any 21219019Sgabor * questions. 22219019Sgabor */ 23219019Sgabor 24219019Sgaborimport java.lang.reflect.Module; 25219019Sgaborimport static jdk.test.lib.Asserts.*; 26219019Sgabor 27219019Sgabor/* 28219019Sgabor * @test 29219019Sgabor * @modules java.base/jdk.internal.misc 30219019Sgabor * @library /test/lib .. 31219019Sgabor * @compile p2/c2.java 32219019Sgabor * @compile p1/c1.java 33219019Sgabor * @build sun.hotspot.WhiteBox 34219019Sgabor * @compile/module=java.base java/lang/reflect/ModuleHelper.java 35219019Sgabor * @run main ClassFileInstaller sun.hotspot.WhiteBox 36219019Sgabor * sun.hotspot.WhiteBox$WhiteBoxPermission 37219019Sgabor * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI AccessCheckAllUnnamed 38219019Sgabor */ 39219019Sgabor 40219019Sgaborpublic class AccessCheckAllUnnamed { 41219019Sgabor 42219019Sgabor // Test a series of error conditions for API JVM_AddModuleExportsToAllUnnamed 43219019Sgabor // and then test that a class in the unnamed module can access a package in a 44219019Sgabor // named module that has been exported to all unnamed modules. 45219019Sgabor public static void main(String args[]) throws Throwable { 46219019Sgabor Object m1x, m2x; 47219019Sgabor 48219019Sgabor // Get the java.lang.reflect.Module object for module java.base. 49219019Sgabor Class jlObject = Class.forName("java.lang.Object"); 50219019Sgabor Object jlObject_jlrM = jlObject.getModule(); 51219019Sgabor assertNotNull(jlObject_jlrM, "jlrModule object of java.lang.Object should not be null"); 52219019Sgabor 53219019Sgabor // Get the class loader for AccessCheckWorks and assume it's also used to 54219019Sgabor // load class p2.c2. 55219019Sgabor ClassLoader this_cldr = AccessCheckAllUnnamed.class.getClassLoader(); 56219019Sgabor 57219019Sgabor // Define a module for p3. 58219019Sgabor m1x = ModuleHelper.ModuleObject("module_one", this_cldr, new String[] { "p3" }); 59219019Sgabor assertNotNull(m1x, "Module should not be null"); 60219019Sgabor ModuleHelper.DefineModule(m1x, "9.0", "m1x/there", new String[] { "p3" }); 61219019Sgabor ModuleHelper.AddReadsModule(m1x, jlObject_jlrM); 62219019Sgabor 63219019Sgabor // Define a module for p2. 64219019Sgabor m2x = ModuleHelper.ModuleObject("module_two", this_cldr, new String[] { "p2" }); 65219019Sgabor assertNotNull(m2x, "Module should not be null"); 66219019Sgabor ModuleHelper.DefineModule(m2x, "9.0", "m2x/there", new String[] { "p2" }); 67219019Sgabor ModuleHelper.AddReadsModule(m2x, jlObject_jlrM); 68219019Sgabor 69219019Sgabor try { 70219019Sgabor ModuleHelper.AddModuleExportsToAllUnnamed((Module)null, "p2"); 71219019Sgabor throw new RuntimeException("Failed to get the expected NPE for null module"); 72219019Sgabor } catch(NullPointerException e) { 73219019Sgabor // Expected 74219019Sgabor } 75219019Sgabor 76219019Sgabor try { 77219019Sgabor ModuleHelper.AddModuleExportsToAllUnnamed(m2x, null); 78219019Sgabor throw new RuntimeException("Failed to get the expected NPE for null package"); 79219019Sgabor } catch(NullPointerException e) { 80219019Sgabor // Expected 81219019Sgabor } 82219019Sgabor 83219019Sgabor try { 84219019Sgabor ModuleHelper.AddModuleExportsToAllUnnamed(this_cldr, "p2"); 85219019Sgabor throw new RuntimeException("Failed to get the expected IAE for bad module"); 86219019Sgabor } catch(IllegalArgumentException e) { 87219019Sgabor // Expected 88219019Sgabor } 89219019Sgabor 90219019Sgabor try { 91219019Sgabor ModuleHelper.AddModuleExportsToAllUnnamed(m2x, "p3"); 92219019Sgabor throw new RuntimeException("Failed to get the expected IAE for package in other module"); 93219019Sgabor } catch(IllegalArgumentException e) { 94219019Sgabor // Expected 95219019Sgabor } 96219019Sgabor 97219019Sgabor try { 98219019Sgabor ModuleHelper.AddModuleExportsToAllUnnamed(m2x, "p4"); 99219019Sgabor throw new RuntimeException("Failed to get the expected IAE for package not in module"); 100219019Sgabor } catch(IllegalArgumentException e) { 101219019Sgabor // Expected 102219019Sgabor } 103219019Sgabor 104219019Sgabor // Export package p2 in m2x to allUnnamed. 105219019Sgabor ModuleHelper.AddModuleExportsToAllUnnamed(m2x, "p2"); 106219019Sgabor 107219019Sgabor // p1.c1's ctor tries to call a method in p2.c2. This should succeed because 108219019Sgabor // p1 is in an unnamed module and p2.c2 is exported to all unnamed modules. 109219019Sgabor Class p1_c1_class = Class.forName("p1.c1"); 110219019Sgabor try { 111219019Sgabor Object c1_obj = p1_c1_class.newInstance(); 112219019Sgabor } catch (IllegalAccessError f) { 113219019Sgabor throw new RuntimeException( 114219019Sgabor "Class in unnamed module could not access package p2 exported to all unnamed modules"); 115219019Sgabor } 116219019Sgabor } 117219019Sgabor} 118219019Sgabor 119219019Sgabor