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.junit.jupiter.api.Assertions;
6 import org.junit.jupiter.api.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 junit 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 Assertions.assertEquals(invokeOps.get(0).operands(), invokeOps.get(0).argOperands());
26
27 Assertions.assertEquals(invokeOps.get(1).operands().subList(0, 1), invokeOps.get(1).argOperands());
28
29 Assertions.assertEquals(invokeOps.get(2).operands(), invokeOps.get(2).argOperands());
30
31 Assertions.assertEquals(invokeOps.get(3).operands().subList(0, 1), invokeOps.get(3).argOperands());
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 Assertions.assertEquals(invokeOp.operands(), l);
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 }