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  * @run main Unreflect TestPatterns
 29  * @run junit TestPatterns
 30  * @enablePreview */
 31 
 32 import jdk.incubator.code.Reflect;
 33 import jdk.incubator.code.Op;
 34 import jdk.incubator.code.CodeTransformer;
 35 import jdk.incubator.code.dialect.core.CoreOp;
 36 import jdk.incubator.code.interpreter.Interpreter;
 37 import org.junit.jupiter.api.Assertions;
 38 import org.junit.jupiter.api.Test;
 39 
 40 import java.lang.invoke.MethodHandles;
 41 import java.lang.reflect.Method;
 42 import java.util.Optional;
 43 import java.util.stream.Stream;
 44 
 45 public class TestPatterns {
 46 
 47     interface Point {
 48     }
 49 
 50     record ConcretePoint(int x, int y) implements Point {
 51     }
 52 
 53     enum Color {RED, GREEN, BLUE}
 54 
 55     record ColoredPoint(ConcretePoint p, Color c) implements Point {
 56     }
 57 
 58     record Rectangle(Point upperLeft, Point lowerRight) {
 59     }
 60 
 61 
 62     @Reflect
 63     public static String recordPatterns(Object r) {
 64         if (r instanceof Rectangle(
 65                 ColoredPoint(ConcretePoint p, Color c),
 66                 ColoredPoint lr)) {
 67             return p.toString();
 68         } else {
 69             return "";
 70         }
 71     }
 72 
 73     @Test
 74     public void testRecordPatterns() {
 75         CoreOp.FuncOp f = getFuncOp("recordPatterns");
 76 
 77         System.out.println(f.toText());
 78 
 79         CoreOp.FuncOp lf = f.transform(CodeTransformer.LOWERING_TRANSFORMER);
 80 
 81         System.out.println(lf.toText());
 82 
 83         {
 84             Rectangle r = new Rectangle(
 85                     new ColoredPoint(new ConcretePoint(1, 2), Color.RED),
 86                     new ColoredPoint(new ConcretePoint(3, 4), Color.BLUE));
 87             Assertions.assertEquals(recordPatterns(r), Interpreter.invoke(MethodHandles.lookup(), lf, r));
 88         }
 89 
 90         {
 91             Rectangle r = new Rectangle(
 92                     new ColoredPoint(new ConcretePoint(1, 2), Color.RED),
 93                     new ConcretePoint(3, 4));
 94             Assertions.assertEquals(recordPatterns(r), Interpreter.invoke(MethodHandles.lookup(), lf, r));
 95         }
 96 
 97         {
 98             Rectangle r = new Rectangle(
 99                     new ConcretePoint(1, 2),
100                     new ConcretePoint(3, 4));
101             Assertions.assertEquals(recordPatterns(r), Interpreter.invoke(MethodHandles.lookup(), lf, r));
102         }
103 
104         {
105             String r = "";;
106             Assertions.assertEquals(recordPatterns(r), Interpreter.invoke(MethodHandles.lookup(), lf, r));
107         }
108     }
109 
110     record R(Number n) {}
111 
112     @Reflect
113     static boolean recordPatterns2(Object o) {
114         return o instanceof R(_);
115     }
116 
117     @Test
118     void testRecordPattern2() {
119 
120         CoreOp.FuncOp f = getFuncOp("recordPatterns2");
121         System.out.println(f.toText());
122 
123         CoreOp.FuncOp lf = f.transform(CodeTransformer.LOWERING_TRANSFORMER);
124         System.out.println(lf.toText());
125 
126         Object[] objects = {new R(1), "str", null};
127         for (Object o : objects) {
128             Assertions.assertEquals(recordPatterns2(o), Interpreter.invoke(MethodHandles.lookup(), lf, o));
129         }
130     }
131 
132 
133     static CoreOp.FuncOp getFuncOp(String name) {
134         Optional<Method> om = Stream.of(TestPatterns.class.getDeclaredMethods())
135                 .filter(m -> m.getName().equals(name))
136                 .findFirst();
137 
138         Method m = om.get();
139         return Op.ofMethod(m).get();
140     }
141 
142 }