1 /* 2 * Copyright (c) 2022, 2024, 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 /* 25 * @test 26 * @summary Test dynamic proxies with parameter types or return type of value class 27 * @enablePreview 28 * @run testng/othervm ProxyTest 29 */ 30 31 import java.lang.reflect.*; 32 import java.util.Arrays; 33 34 import jdk.internal.vm.annotation.ImplicitlyConstructible; 35 import org.testng.annotations.Test; 36 import static org.testng.Assert.*; 37 38 public class ProxyTest { 39 static value class V { 40 int v; 41 V(int v) { 42 this.v = v; 43 } 44 } 45 46 @ImplicitlyConstructible 47 static value class P { 48 int p; 49 P(int p) { 50 this.p = p; 51 } 52 } 53 54 interface I { 55 int getV(V v); 56 int getP(P p); 57 } 58 59 interface J { 60 int[] getV(V[] v); 61 } 62 63 @Test 64 public void testProxy() throws Exception { 65 InvocationHandler handler = new InvocationHandler() { 66 @Override 67 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 68 switch (method.getName()) { 69 case "getV": 70 V v = (V)args[0]; 71 return v.v; 72 case "getP": 73 P p = (P)args[0]; 74 return p.p; 75 default: 76 throw new UnsupportedOperationException(method.toString()); 77 } 78 } 79 }; 80 81 Class<?>[] intfs = new Class<?>[] { I.class }; 82 I i = (I) Proxy.newProxyInstance(ProxyTest.class.getClassLoader(), intfs, handler); 83 84 assertTrue(i.getV(new V(100)) == 100); 85 assertTrue(i.getP(new P(200)) == 200); 86 } 87 88 @Test 89 public void testValueArrayType() { 90 InvocationHandler handler = new InvocationHandler() { 91 @Override 92 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 93 switch (method.getName()) { 94 case "getV": 95 V[] vs = (V[])args[0]; 96 return Arrays.stream(vs).mapToInt(v -> v.v).toArray(); 97 default: 98 throw new UnsupportedOperationException(method.toString()); 99 } 100 } 101 }; 102 103 Class<?>[] intfs = new Class<?>[] { J.class }; 104 J j = (J) Proxy.newProxyInstance(ProxyTest.class.getClassLoader(), intfs, handler); 105 106 V[] array = new V[] { new V(10), new V(20), new V(30)}; 107 assertEquals(j.getV(array), new int[] { 10, 20, 30}); 108 } 109 }