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