1 /* 2 * Copyright (c) 2016, 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 package p3; 25 26 import p4.Foo; 27 import java.lang.module.ModuleDescriptor; 28 import java.lang.reflect.Field; 29 30 import static java.lang.module.ModuleDescriptor.Exports.Modifier.*; 31 32 /** 33 * Test if m4 is an open module and p4 is package that m3 can access 34 */ 35 public class Main { main(String... args)36 public static void main(String... args) throws Exception { 37 Module m4 = Foo.class.getModule(); 38 if (!m4.isOpen("p4")) { 39 throw new RuntimeException("m3 can't access p4"); 40 } 41 42 // Test if it can access a private field 43 Foo foo = Foo.create("foo"); 44 45 Field field = Foo.class.getDeclaredField("name"); 46 field.setAccessible(true); 47 String name = (String) field.get(foo); 48 if (!name.equals("foo")) { 49 throw new RuntimeException("unexpected Foo::name value = " + name); 50 } 51 52 checkOpenModule(); 53 } 54 55 // check the module descriptor of the open module m4 checkOpenModule()56 static void checkOpenModule() { 57 ModuleDescriptor md = Foo.class.getModule().getDescriptor(); 58 System.out.println(md); 59 60 if (!md.isOpen()) { 61 throw new RuntimeException("m4 is a open module"); 62 } 63 64 if (md.packages().size() != 1 || !md.packages().contains("p4")) { 65 throw new RuntimeException("unexpected m4 packages: " + md.packages()); 66 } 67 68 if (!md.opens().isEmpty()) { 69 throw new RuntimeException("unexpected m4 opens: " + md.opens()); 70 } 71 } 72 73 } 74