1 /*
2 * Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @modules jdk.incubator.code
27 * @run junit TestPatterns
28 * @enablePreview */
29
30 import jdk.incubator.code.CodeReflection;
31 import jdk.incubator.code.Op;
32 import jdk.incubator.code.OpTransformer;
33 import jdk.incubator.code.dialect.core.CoreOp;
34 import jdk.incubator.code.interpreter.Interpreter;
35 import org.junit.jupiter.api.Assertions;
36 import org.junit.jupiter.api.Test;
37
38 import java.lang.invoke.MethodHandles;
39 import java.lang.reflect.Method;
40 import java.util.Optional;
41 import java.util.stream.Stream;
42
43 public class TestPatterns {
44
45 interface Point {
46 }
47
48 record ConcretePoint(int x, int y) implements Point {
49 }
50
51 enum Color {RED, GREEN, BLUE}
52
53 record ColoredPoint(ConcretePoint p, Color c) implements Point {
54 }
55
56 record Rectangle(Point upperLeft, Point lowerRight) {
57 }
58
59
60 @CodeReflection
61 public static String recordPatterns(Object r) {
62 if (r instanceof Rectangle(
63 ColoredPoint(ConcretePoint p, Color c),
64 ColoredPoint lr)) {
65 return p.toString();
66 } else {
67 return "";
68 }
69 }
70
71 @Test
72 public void testRecordPatterns() {
73 CoreOp.FuncOp f = getFuncOp("recordPatterns");
74
75 System.out.println(f.toText());
76
77 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER);
78
79 System.out.println(lf.toText());
80
81 {
82 Rectangle r = new Rectangle(
83 new ColoredPoint(new ConcretePoint(1, 2), Color.RED),
84 new ColoredPoint(new ConcretePoint(3, 4), Color.BLUE));
85 Assertions.assertEquals(recordPatterns(r), Interpreter.invoke(MethodHandles.lookup(), lf, r));
86 }
87
88 {
89 Rectangle r = new Rectangle(
90 new ColoredPoint(new ConcretePoint(1, 2), Color.RED),
91 new ConcretePoint(3, 4));
92 Assertions.assertEquals(recordPatterns(r), Interpreter.invoke(MethodHandles.lookup(), lf, r));
93 }
94
95 {
96 Rectangle r = new Rectangle(
97 new ConcretePoint(1, 2),
98 new ConcretePoint(3, 4));
99 Assertions.assertEquals(recordPatterns(r), Interpreter.invoke(MethodHandles.lookup(), lf, r));
100 }
101
102 {
103 String r = "";;
104 Assertions.assertEquals(recordPatterns(r), Interpreter.invoke(MethodHandles.lookup(), lf, r));
105 }
106 }
107
108 record R(Number n) {}
109
110 @CodeReflection
111 static boolean recordPatterns2(Object o) {
112 return o instanceof R(_);
113 }
114
115 @Test
116 void testRecordPattern2() {
117
118 CoreOp.FuncOp f = getFuncOp("recordPatterns2");
119 System.out.println(f.toText());
120
121 CoreOp.FuncOp lf = f.transform(OpTransformer.LOWERING_TRANSFORMER);
122 System.out.println(lf.toText());
123
124 Object[] objects = {new R(1), "str", null};
125 for (Object o : objects) {
126 Assertions.assertEquals(recordPatterns2(o), Interpreter.invoke(MethodHandles.lookup(), lf, o));
127 }
128 }
129
130
131 static CoreOp.FuncOp getFuncOp(String name) {
132 Optional<Method> om = Stream.of(TestPatterns.class.getDeclaredMethods())
133 .filter(m -> m.getName().equals(name))
134 .findFirst();
135
136 Method m = om.get();
137 return Op.ofMethod(m).get();
138 }
139
140 }