1 /* 2 * Copyright (c) 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 Smoke test for captured values in quoted lambdas. 27 * @modules jdk.incubator.code 28 * @run testng TestCaptureQuoted 29 */ 30 31 import jdk.incubator.code.dialect.core.CoreOp.Var; 32 import jdk.incubator.code.Op; 33 import jdk.incubator.code.Quoted; 34 import jdk.incubator.code.interpreter.Interpreter; 35 import java.lang.invoke.MethodHandles; 36 import java.util.ArrayList; 37 import java.util.Iterator; 38 import java.util.List; 39 import java.util.stream.IntStream; 40 41 import org.testng.annotations.*; 42 import static org.testng.Assert.*; 43 44 public class TestCaptureQuoted { 45 46 @Test(dataProvider = "ints") 47 public void testCaptureIntParam(int x) { 48 Quoted quoted = (int y) -> x + y; 49 assertEquals(quoted.capturedValues().size(), 1); 50 assertEquals(((Var)quoted.capturedValues().values().iterator().next()).value(), x); 51 List<Object> arguments = new ArrayList<>(); 52 arguments.add(1); 53 arguments.addAll(quoted.capturedValues().values()); 54 int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(), 55 arguments); 56 assertEquals(res, x + 1); 57 } 58 59 static class Context { 60 final int x; 61 62 Context(int x) { 63 this.x = x; 64 } 65 66 Quoted quoted() { 67 return (int y) -> x + y; 68 } 69 } 70 71 @Test(dataProvider = "ints") 72 public void testCaptureIntField(int x) { 73 Context context = new Context(x); 74 Quoted quoted = context.quoted(); 75 assertEquals(quoted.capturedValues().size(), 1); 76 assertEquals(quoted.capturedValues().values().iterator().next(), context); 77 List<Object> arguments = new ArrayList<>(); 78 arguments.add(1); 79 arguments.addAll(quoted.capturedValues().values()); 80 int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(), 81 arguments); 82 assertEquals(res, x + 1); 83 } 84 85 @Test 86 public void testCaptureThisRefAndIntConstant() { 87 final int x = 100; 88 String hello = "hello"; 89 Quoted quoted = (Integer y) -> y.intValue() + hashCode() + hello.length() + x; 90 assertEquals(quoted.capturedValues().size(), 3); 91 Iterator<Object> it = quoted.capturedValues().values().iterator(); 92 assertEquals(it.next(), this); 93 assertEquals(((Var)it.next()).value(), hello); 94 assertEquals(((Var)it.next()).value(), x); 95 List<Object> arguments = new ArrayList<>(); 96 arguments.add(1); 97 arguments.addAll(quoted.capturedValues().values()); 98 int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(), 99 arguments); 100 assertEquals(res, x + 1 + hashCode() + hello.length()); 101 } 102 103 @Test 104 public void testCaptureThisInInvocationArg() { 105 Quoted quoted = (Number y) -> y.intValue() + Integer.valueOf(hashCode()); 106 assertEquals(quoted.capturedValues().size(), 1); 107 Iterator<Object> it = quoted.capturedValues().values().iterator(); 108 assertEquals(it.next(), this); 109 List<Object> arguments = new ArrayList<>(); 110 arguments.add(1); 111 arguments.addAll(quoted.capturedValues().values()); 112 int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(), 113 arguments); 114 assertEquals(res, 1 + hashCode()); 115 } 116 117 record R(int i) {} 118 119 @Test 120 public void testCaptureThisInNewArg() { 121 Quoted quoted = (Number y) -> y.intValue() + new R(hashCode()).i; 122 assertEquals(quoted.capturedValues().size(), 1); 123 Iterator<Object> it = quoted.capturedValues().values().iterator(); 124 assertEquals(it.next(), this); 125 List<Object> arguments = new ArrayList<>(); 126 arguments.add(1); 127 arguments.addAll(quoted.capturedValues().values()); 128 int res = (int)Interpreter.invoke(MethodHandles.lookup(), (Op & Op.Invokable) quoted.op(), 129 arguments); 130 assertEquals(res, 1 + hashCode()); 131 } 132 133 134 @DataProvider(name = "ints") 135 public Object[][] ints() { 136 return IntStream.range(0, 50) 137 .mapToObj(i -> new Object[] { i }) 138 .toArray(Object[][]::new); 139 } 140 }