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