1 /* 2 * Copyright (c) 2015, 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 java.lang.module.Configuration; 25 import java.lang.module.ModuleFinder; 26 import java.lang.module.ModuleReference; 27 import java.lang.reflect.InvocationHandler; 28 import java.lang.reflect.Layer; 29 import java.lang.reflect.Module; 30 import java.lang.reflect.Proxy; 31 import java.nio.file.Path; 32 import java.nio.file.Paths; 33 import java.util.Arrays; 34 import java.util.List; 35 36 import static jdk.testlibrary.ProcessTools.executeTestJava; 37 38 import org.testng.annotations.BeforeTest; 39 import org.testng.annotations.Test; 40 import static org.testng.Assert.*; 41 42 /** 43 * @test 44 * @library ../../lib /lib/testlibrary 45 * @modules jdk.compiler 46 * @build ProxyTest CompilerUtils jdk.testlibrary.ProcessTools 47 * @run testng ProxyLayerTest 48 * @summary Test proxies to implement interfaces in a layer 49 */ 50 51 public class ProxyLayerTest { 52 53 private static final String TEST_SRC = System.getProperty("test.src"); 54 private static final String TEST_CLASSES = System.getProperty("test.classes"); 55 56 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 57 private static final Path MODS_DIR = Paths.get("mods"); 58 private static final Path CPATH_DIR = Paths.get(TEST_CLASSES); 59 60 // the names of the modules in this test 61 private static String[] modules = new String[] {"m1", "m2", "m3"}; 62 63 64 /** 65 * Compiles all modules used by the test 66 */ 67 @BeforeTest 68 public void compileAll() throws Exception { 69 for (String mn : modules) { 70 Path msrc = SRC_DIR.resolve(mn); 71 assertTrue(CompilerUtils.compile(msrc, MODS_DIR, "-modulesourcepath", SRC_DIR.toString())); 72 } 73 } 74 75 /** 76 * Test proxy implementing interfaces in a Layer defined in 77 * an unnamed module 78 */ 79 @Test 80 public void testProxyInUnnamed() throws Exception { 81 ModuleFinder finder = ModuleFinder.of(MODS_DIR); 82 Configuration cf = Configuration 83 .resolve(ModuleFinder.empty(), Layer.boot(), finder, modules).bind(); 84 85 ClassLoader loader = new ModuleClassLoader(cf); 86 Layer layer = Layer.create(cf, mn -> loader); 87 88 assertTrue(layer.findModule("m1").isPresent()); 89 assertTrue(layer.findModule("m2").isPresent()); 90 assertTrue(layer.findModule("m3").isPresent()); 91 92 Class<?>[] interfaces = new Class<?>[] { 93 Class.forName("p.one.I", false, loader), 94 Class.forName("p.two.A", false, loader), 95 Class.forName("p.three.P", false, loader), 96 }; 97 Object o = Proxy.newProxyInstance(loader, interfaces, handler); 98 99 Class<?> proxyClass = o.getClass(); 100 Package pkg = proxyClass.getPackage(); 101 assertFalse(proxyClass.getModule().isNamed()); 102 assertFalse(pkg.isSealed()); 103 assertEquals(proxyClass.getModule().getLayer(), null); 104 } 105 /** 106 * Test proxy implementing interfaces in a Layer and defined in a 107 * dynamic module 108 */ 109 @Test 110 public void testProxyInDynamicModule() throws Exception { 111 ModuleFinder finder = ModuleFinder.of(MODS_DIR); 112 Configuration cf = Configuration 113 .resolve(ModuleFinder.empty(), Layer.boot(), finder, modules).bind(); 114 115 ClassLoader loader = new ModuleClassLoader(cf); 116 Layer layer = Layer.create(cf, mn -> loader); 117 118 assertTrue(layer.findModule("m1").isPresent()); 119 assertTrue(layer.findModule("m2").isPresent()); 120 assertTrue(layer.findModule("m3").isPresent()); 121 122 Class<?>[] interfaces = new Class<?>[] { 123 Class.forName("p.one.internal.J", false, loader), 124 }; 125 Object o = Proxy.newProxyInstance(loader, interfaces, handler); 126 Class<?> proxyClass = o.getClass(); 127 Package pkg = proxyClass.getPackage(); 128 assertTrue(proxyClass.getModule().isNamed()); 129 assertFalse(pkg.isSealed()); // not sealed since no module location 130 assertEquals(proxyClass.getModule().getLayer(), null); 131 } 132 133 private final static InvocationHandler handler = 134 (proxy, m, params) -> { throw new RuntimeException(m.toString()); }; 135 136 }