1 /*
   2  * Copyright (c) 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 import java.lang.runtime.PatternHandle;
  27 import java.lang.runtime.PatternHandles;
  28 import java.lang.runtime.SwitchBootstraps;
  29 import java.lang.invoke.CallSite;
  30 import java.lang.invoke.MethodHandle;
  31 import java.lang.invoke.MethodHandles;
  32 import java.lang.invoke.MethodType;
  33 
  34 import org.testng.annotations.Test;
  35 
  36 import static java.lang.invoke.MethodHandleInfo.REF_newInvokeSpecial;
  37 import static org.testng.Assert.assertEquals;
  38 
  39 /**
  40  * @test
  41  * @run testng RecordTest
  42  * @summary End-to-end test for record patterns
  43  */
  44 @Test
  45 public class RecordTest {
  46     record R(int a, String b, double c);
  47     record RR(R r1, R R2);
  48 
  49     private PatternHandle recordExtractor(Class<?> recordClass,
  50                                           Class<?>... paramTypes) throws Throwable {
  51         return PatternHandles.ofNamed(MethodHandles.lookup(), "_", PatternHandle.class,
  52                 recordClass, MethodType.methodType(void.class, paramTypes), recordClass.getName(), REF_newInvokeSpecial);
  53     }
  54 
  55     public void testRecord() throws Throwable {
  56         R r = new R(1, "two", 3.14d);
  57         PatternHandle rExtract = recordExtractor(R.class, int.class, String.class, double.class);
  58 
  59         MethodHandle tryExtract = PatternHandles.tryMatch(MethodHandles.lookup(), "_", MethodHandle.class, rExtract);
  60         MethodHandle a = PatternHandles.component(MethodHandles.lookup(), "_", MethodHandle.class, rExtract, 0);
  61         MethodHandle b = PatternHandles.component(MethodHandles.lookup(), "_", MethodHandle.class, rExtract, 1);
  62         MethodHandle c = PatternHandles.component(MethodHandles.lookup(), "_", MethodHandle.class, rExtract, 2);
  63 
  64         Object o = tryExtract.invoke(r);
  65         assertEquals(1, a.invoke(o));
  66         assertEquals("two", b.invoke(o));
  67         assertEquals(3.14d, c.invoke(o));
  68     }
  69 
  70     public void testFakeNested() throws Throwable {
  71         R r1 = new R(1, "two", 3.14d);
  72         R r2 = new R(2, "four", 6.0d);
  73         RR rr = new RR(r1, r2);
  74 
  75         PatternHandle rExtract = recordExtractor(R.class, int.class, String.class, double.class);
  76         PatternHandle rrExtract = recordExtractor(RR.class, R.class, R.class);
  77 
  78         MethodHandle tryExtractR = PatternHandles.tryMatch(MethodHandles.lookup(), "_", MethodHandle.class, rExtract);
  79         MethodHandle ra = PatternHandles.component(MethodHandles.lookup(), "_", MethodHandle.class, rExtract, 0);
  80         MethodHandle rb = PatternHandles.component(MethodHandles.lookup(), "_", MethodHandle.class, rExtract, 1);
  81         MethodHandle rc = PatternHandles.component(MethodHandles.lookup(), "_", MethodHandle.class, rExtract, 2);
  82 
  83         MethodHandle tryExtractRr = PatternHandles.tryMatch(MethodHandles.lookup(), "_", MethodHandle.class, rrExtract);
  84         MethodHandle r1c = PatternHandles.component(MethodHandles.lookup(), "_", MethodHandle.class, rrExtract, 0);
  85         MethodHandle r2c = PatternHandles.component(MethodHandles.lookup(), "_", MethodHandle.class, rrExtract, 1);
  86 
  87         Object o = tryExtractRr.invoke(rr);
  88         R o1 = (R) r1c.invoke(o);
  89         R o2 = (R) r2c.invoke(o);
  90 
  91         assertEquals(1, ra.invoke(o1));
  92         assertEquals("two", rb.invoke(o1));
  93         assertEquals(3.14d, rc.invoke(o1));
  94 
  95         assertEquals(2, ra.invoke(o2));
  96         assertEquals("four", rb.invoke(o2));
  97         assertEquals(6.0d, rc.invoke(o2));
  98     }
  99 
 100     public void testNested() throws Throwable {
 101         PatternHandle rExtract = recordExtractor(R.class, int.class, String.class, double.class);
 102         PatternHandle rrExtract = recordExtractor(RR.class, R.class, R.class);
 103 
 104         PatternHandle e = PatternHandles.nested(rrExtract, rExtract);
 105 
 106         R r1 = new R(1, "two", 3.14d);
 107         R r2 = new R(2, "four", 6.0d);
 108         RR rr = new RR(r1, r2);
 109 
 110         Object o = e.tryMatch().invoke(rr);
 111 
 112         assertEquals(e.component(0).invoke(o), new R(1, "two", 3.14d));
 113         assertEquals(e.component(1).invoke(o), new R(2, "four", 6.0d));
 114         assertEquals(e.component(2).invoke(o), 1);
 115         assertEquals(e.component(3).invoke(o), "two");
 116         assertEquals(e.component(4).invoke(o), 3.14d);
 117 
 118         PatternHandle ee = PatternHandles.nested(rrExtract, rExtract, rExtract);
 119         o = ee.tryMatch().invoke(rr);
 120 
 121         assertEquals(ee.component(0).invoke(o), new R(1, "two", 3.14d));
 122         assertEquals(ee.component(1).invoke(o), new R(2, "four", 6.0d));
 123         assertEquals(ee.component(2).invoke(o), 1);
 124         assertEquals(ee.component(3).invoke(o), "two");
 125         assertEquals(ee.component(4).invoke(o), 3.14d);
 126         assertEquals(ee.component(5).invoke(o), 2);
 127         assertEquals(ee.component(6).invoke(o), "four");
 128         assertEquals(ee.component(7).invoke(o), 6.0d);
 129     }
 130 
 131     record A(int a);
 132     record B(int a, int b);
 133     record S(String s);
 134     record T(String s, String t);
 135     record U();
 136 
 137     private Object component(PatternHandle e, int num, Object carrier) throws Throwable {
 138         return PatternHandles.component(MethodHandles.lookup(), "_", MethodHandle.class,
 139                 e, num).invoke(carrier);
 140     }
 141 
 142     public void testRecordSwitch() throws Throwable {
 143         PatternHandle[] extractors = {
 144                 recordExtractor(A.class, int.class),
 145                 recordExtractor(B.class, int.class, int.class),
 146                 recordExtractor(S.class, String.class),
 147                 recordExtractor(T.class, String.class, String.class),
 148                 recordExtractor(U.class)
 149         };
 150 
 151         Object[] exemplars = {
 152                 new A(1),
 153                 new B(2, 3),
 154                 new S("four"),
 155                 new T("five", "six"),
 156                 new U()
 157         };
 158 
 159         CallSite cs = SwitchBootstraps.patternSwitch(MethodHandles.lookup(), "_",
 160                 MethodType.methodType(SwitchBootstraps.PatternSwitchResult.class, Object.class),
 161                 extractors);
 162         MethodHandle mh = cs.dynamicInvoker();
 163         for (int i = 0; i < exemplars.length; i++) {
 164             Object exemplar = exemplars[i];
 165             SwitchBootstraps.PatternSwitchResult result = (SwitchBootstraps.PatternSwitchResult) mh.invoke(exemplar);
 166             assertEquals(result.index, i);
 167             switch (result.index) {
 168                 case 0:
 169                     assertEquals(component(extractors[i], 0, result.carrier), 1);
 170                     break;
 171                 case 1:
 172                     assertEquals(component(extractors[i], 0, result.carrier), 2);
 173                     assertEquals(component(extractors[i], 1, result.carrier), 3);
 174                     break;
 175                 case 2:
 176                     assertEquals(component(extractors[i], 0, result.carrier), "four");
 177                     break;
 178                 case 3:
 179                     assertEquals(component(extractors[i], 0, result.carrier), "five");
 180                     assertEquals(component(extractors[i], 1, result.carrier), "six");
 181                     break;
 182             };
 183 
 184             result = (SwitchBootstraps.PatternSwitchResult) mh.invoke(null);
 185             assertEquals(result.index, -1);
 186 
 187             result = (SwitchBootstraps.PatternSwitchResult) mh.invoke("foo");
 188             assertEquals(result.index, 5);
 189         }
 190     }
 191 
 192     record Box(Object o1);
 193 
 194     public void testNestedRecord() throws Throwable {
 195         PatternHandle boxA = PatternHandles.nested(recordExtractor(Box.class, Object.class),
 196                 recordExtractor(A.class, int.class));
 197         PatternHandle boxB = PatternHandles.nested(recordExtractor(Box.class, Object.class),
 198                 recordExtractor(B.class, int.class, int.class));
 199 
 200         CallSite cs = SwitchBootstraps.patternSwitch(MethodHandles.lookup(), "_",
 201                 MethodType.methodType(SwitchBootstraps.PatternSwitchResult.class, Object.class),
 202                 boxA, boxB);
 203         MethodHandle mh = cs.dynamicInvoker();
 204 
 205         assertEquals(((SwitchBootstraps.PatternSwitchResult) mh.invoke(new Box(new A(1)))).index, 0);
 206         assertEquals(((SwitchBootstraps.PatternSwitchResult) mh.invoke(new Box(new B(2, 3)))).index, 1);
 207         assertEquals(((SwitchBootstraps.PatternSwitchResult) mh.invoke(new Box("foo"))).index, 2);
 208         assertEquals(((SwitchBootstraps.PatternSwitchResult) mh.invoke(new Box(null))).index, 2);
 209         assertEquals(((SwitchBootstraps.PatternSwitchResult) mh.invoke("foo")).index, 2);
 210         assertEquals(((SwitchBootstraps.PatternSwitchResult) mh.invoke(null)).index, -1);
 211     }
 212 
 213     record RString(String i) { }
 214     record RObject(Object i) { }
 215     record Rint(int i) { }
 216 
 217 
 218     public void testNestedWithConstant() throws Throwable {
 219         PatternHandle rb = recordExtractor(Box.class, Object.class);
 220         PatternHandle rs = recordExtractor(RString.class, String.class);
 221         PatternHandle ro = recordExtractor(RObject.class, Object.class);
 222         PatternHandle ri = recordExtractor(Rint.class, int.class);
 223         PatternHandle cs = PatternHandles.ofConstant("foo");
 224         PatternHandle cn = PatternHandles.ofConstant(null);
 225         PatternHandle ci = PatternHandles.ofConstant(3);
 226 
 227         PatternHandleTest.assertMatch(PatternHandleTest.MatchKind.MATCH, PatternHandles.nested(rs, cs), new RString("foo"), "foo");
 228         PatternHandleTest.assertMatch(PatternHandleTest.MatchKind.NO_MATCH, PatternHandles.nested(rs, cs), new RString("bar"));
 229         PatternHandleTest.assertMatch(PatternHandleTest.MatchKind.NO_MATCH, PatternHandles.nested(rs, cs), new RString(null));
 230 
 231         PatternHandleTest.assertMatch(PatternHandleTest.MatchKind.MATCH, PatternHandles.nested(ro, cs), new RObject("foo"), "foo");
 232         PatternHandleTest.assertMatch(PatternHandleTest.MatchKind.NO_MATCH, PatternHandles.nested(ro, cs), new RObject("bar"));
 233         PatternHandleTest.assertMatch(PatternHandleTest.MatchKind.NO_MATCH, PatternHandles.nested(ro, cs), new RObject(3));
 234         PatternHandleTest.assertMatch(PatternHandleTest.MatchKind.NO_MATCH, PatternHandles.nested(ro, cs), new RObject(null));
 235 
 236         // @@@ These are waiting for support for either unboxing in adapt() or primitive constant patterns
 237 //        PatternHandleTest.assertMatch(PatternHandleTest.MatchKind.MATCH, PatternHandles.nested(ri, ci), new Rint(3), 3);
 238 //        PatternHandleTest.assertMatch(PatternHandleTest.MatchKind.NO_MATCH, PatternHandles.nested(ri, ci), new Rint(2));
 239 
 240         PatternHandleTest.assertMatch(PatternHandleTest.MatchKind.MATCH,
 241                 PatternHandles.nested(rb, PatternHandles.nested(rs, cs)),
 242                 new Box(new RString("foo")), new RString("foo"), "foo");
 243         PatternHandleTest.assertMatch(PatternHandleTest.MatchKind.NO_MATCH,
 244                 PatternHandles.nested(rb, PatternHandles.nested(rs, cs)),
 245                 new Box(new RString("bar")));
 246         PatternHandleTest.assertMatch(PatternHandleTest.MatchKind.NO_MATCH,
 247                 PatternHandles.nested(rb, PatternHandles.nested(rs, cs)),
 248                 new Box("foo"));
 249     }
 250 }