1 import jdk.incubator.code.CodeReflection;
 2 import jdk.incubator.code.Op;
 3 import jdk.incubator.code.dialect.core.CoreOp;
 4 import jdk.incubator.code.dialect.java.JavaOp;
 5 import org.testng.Assert;
 6 import org.testng.annotations.Test;
 7 
 8 import java.lang.reflect.Method;
 9 import java.util.ArrayList;
10 import java.util.Optional;
11 import java.util.stream.Stream;
12 
13 /*
14  * @test
15  * @modules jdk.incubator.code
16  * @run testng TestInvokeOp
17  */
18 public class TestInvokeOp {
19 
20     @Test
21     void test() {
22         var f = getFuncOp(this.getClass(), "f");
23         var invokeOps = f.elements().filter(ce -> ce instanceof JavaOp.InvokeOp).map(ce -> ((JavaOp.InvokeOp) ce)).toList();
24 
25         Assert.assertEquals(invokeOps.get(0).argOperands(), invokeOps.get(0).operands());
26 
27         Assert.assertEquals(invokeOps.get(1).argOperands(), invokeOps.get(1).operands().subList(0, 1));
28 
29         Assert.assertEquals(invokeOps.get(2).argOperands(), invokeOps.get(2).operands());
30 
31         Assert.assertEquals(invokeOps.get(3).argOperands(), invokeOps.get(3).operands().subList(0, 1));
32 
33         for (JavaOp.InvokeOp invokeOp : invokeOps) {
34             var l = new ArrayList<>(invokeOp.argOperands());
35             if (invokeOp.isVarArgs()) {
36                 l.addAll(invokeOp.varArgOperands());
37             }
38             Assert.assertEquals(l, invokeOp.operands());
39         }
40     }
41 
42     @CodeReflection
43     void f() {
44         s(1);
45         s(4, 2, 3);
46         i();
47         i(0.0, 0.0);
48     }
49 
50     static void s(int a, long... l) {}
51     void i(double... d) {}
52 
53     static CoreOp.FuncOp getFuncOp(Class<?> c, String name) {
54         Optional<Method> om = Stream.of(c.getDeclaredMethods())
55                 .filter(m -> m.getName().equals(name))
56                 .findFirst();
57 
58         Method m = om.get();
59         return Op.ofMethod(m).get();
60     }
61 }