1 import jdk.incubator.code.CodeReflection; 2 import jdk.incubator.code.Op; 3 import jdk.incubator.code.OpTransformer; 4 import jdk.incubator.code.bytecode.BytecodeGenerator; 5 import jdk.incubator.code.op.CoreOp; 6 import org.testng.Assert; 7 import org.testng.annotations.Test; 8 9 import java.lang.classfile.ClassFile; 10 import java.lang.classfile.components.ClassPrinter; 11 import java.lang.invoke.MethodHandle; 12 import java.lang.invoke.MethodHandles; 13 import java.lang.reflect.Method; 14 import java.util.Arrays; 15 import java.util.Optional; 16 import java.util.stream.Stream; 17 18 /* 19 * @test 20 * @enablePreview 21 * @modules jdk.incubator.code 22 * @run testng TestVarArg 23 * 24 */ 25 public class TestVarArg { 26 27 @Test 28 void test() throws Throwable { 29 var f = getFuncOp("f"); 30 f.writeTo(System.out); 31 32 var lf = f.transform(OpTransformer.LOWERING_TRANSFORMER); 33 lf.writeTo(System.out); 34 35 var bytes = BytecodeGenerator.generateClassData(MethodHandles.lookup(), f); 36 var classModel = ClassFile.of().parse(bytes); 37 ClassPrinter.toYaml(classModel, ClassPrinter.Verbosity.TRACE_ALL, System.out::print); 38 39 MethodHandle mh = BytecodeGenerator.generate(MethodHandles.lookup(), lf); 40 Assert.assertEquals(mh.invoke(), f()); 41 } 42 43 @CodeReflection 44 static String f() { 45 String r = ""; 46 String ls = System.lineSeparator(); 47 48 r += ls + h(1); 49 r += ls + h(2, 3); 50 r += ls + h(4, (byte) 5); 51 52 r += ls + k(Byte.MIN_VALUE, Byte.MAX_VALUE); 53 54 r += ls + j("s1", "s2", "s3"); 55 56 r += ls + w(8, 9); 57 58 r += k(); 59 60 r += l(11L, 12L); 61 62 r += d(21.0, 22.0); 63 64 return r; 65 } 66 67 static String h(int i, int... s) { 68 return i + ", " + Arrays.toString(s); 69 } 70 71 static String k(byte... s) { 72 return Arrays.toString(s); 73 } 74 75 static String j(String i, String... s) { 76 return i + ", " + Arrays.toString(s); 77 } 78 79 static <T extends Number> String w(T... ts) { 80 return Arrays.toString(ts); 81 } 82 83 static String l(long... a) { 84 return Arrays.toString(a); 85 } 86 87 static String d(double... a) { 88 return Arrays.toString(a); 89 } 90 91 private CoreOp.FuncOp getFuncOp(String name) { 92 Optional<Method> om = Stream.of(this.getClass().getDeclaredMethods()) 93 .filter(m -> m.getName().equals(name)) 94 .findFirst(); 95 96 Method m = om.get(); 97 return Op.ofMethod(m).get(); 98 } 99 }