1 import jdk.incubator.code.Op;
 2 import org.testng.Assert;
 3 import org.testng.annotations.Test;
 4 
 5 import java.lang.invoke.MethodHandles;
 6 import java.lang.reflect.Method;
 7 import jdk.incubator.code.OpTransformer;
 8 import jdk.incubator.code.interpreter.Interpreter;
 9 import jdk.incubator.code.op.CoreOp;
10 import jdk.incubator.code.CodeReflection;
11 import java.util.Optional;
12 import java.util.stream.Stream;
13 
14 /*
15  * @test
16  * @modules jdk.incubator.code
17  * @run testng TestPatterns2
18  * @enablePreview */
19 public class TestPatterns2 {
20 
21     record R<T extends Number> (T n) {}
22 
23     @CodeReflection
24     static boolean f(Object o) {
25         return o instanceof R(Integer i);
26     }
27 
28     @Test
29     void test() {
30 
31         CoreOp.FuncOp f = getFuncOp("f");
32         f.writeTo(System.out);
33 
34         CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER);
35         lf.writeTo(System.out);
36 
37         R[] args = {new R(1), new R(2d)};
38         for (R arg : args) {
39             Assert.assertEquals(Interpreter.invoke(MethodHandles.lookup(), lf, arg), f(arg));
40         }
41     }
42 
43     static CoreOp.FuncOp getFuncOp(String name) {
44         Optional<Method> om = Stream.of(TestPatterns2.class.getDeclaredMethods())
45                 .filter(m -> m.getName().equals(name))
46                 .findFirst();
47 
48         Method m = om.get();
49         return Op.ofMethod(m).get();
50     }
51 }