1 /* 2 * Copyright (c) 2015, 2016, 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 /* @test 27 * @bug 8139885 28 * @bug 8150635 29 * @run testng/othervm -ea -esa test.java.lang.invoke.LoopCombinatorTest 30 */ 31 32 package test.java.lang.invoke; 33 34 import java.lang.invoke.MethodHandle; 35 import java.lang.invoke.MethodHandles; 36 import java.lang.invoke.MethodHandles.Lookup; 37 import java.lang.invoke.MethodType; 38 import java.util.*; 39 40 import static java.lang.invoke.MethodType.methodType; 41 42 import static org.testng.AssertJUnit.*; 43 44 import org.testng.annotations.*; 45 46 /** 47 * Tests for the loop combinators introduced in JEP 274. 48 */ 49 public class LoopCombinatorTest { 50 51 static final Lookup LOOKUP = MethodHandles.lookup(); 52 53 @Test 54 public static void testLoopFac() throws Throwable { 55 MethodHandle[] counterClause = new MethodHandle[]{Fac.MH_zero, Fac.MH_inc}; 56 MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}; 57 MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause); 58 assertEquals(Fac.MT_fac, loop.type()); 59 assertEquals(120, loop.invoke(5)); 60 } 61 62 @Test 63 public static void testLoopFacNullInit() throws Throwable { 64 // null initializer for counter, should initialize to 0 65 MethodHandle[] counterClause = new MethodHandle[]{null, Fac.MH_inc}; 66 MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}; 67 MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause); 68 assertEquals(Fac.MT_fac, loop.type()); 69 assertEquals(120, loop.invoke(5)); 70 } 71 72 @Test 73 public static void testLoopNullInit() throws Throwable { 74 // null initializer for counter, should initialize to 0, one-clause loop 75 MethodHandle[] counterClause = new MethodHandle[]{null, Loop.MH_inc, Loop.MH_pred, Loop.MH_fin}; 76 MethodHandle loop = MethodHandles.loop(counterClause); 77 assertEquals(Loop.MT_loop, loop.type()); 78 assertEquals(10, loop.invoke(10)); 79 } 80 81 @Test 82 public static void testLoopVoid1() throws Throwable { 83 // construct a post-checked loop that only does one iteration and has a void body and void local state 84 MethodHandle loop = MethodHandles.loop(new MethodHandle[]{Empty.MH_f, Empty.MH_f, Empty.MH_pred, null}); 85 assertEquals(MethodType.methodType(void.class), loop.type()); 86 loop.invoke(); 87 } 88 89 @Test 90 public static void testLoopVoid2() throws Throwable { 91 // construct a post-checked loop that only does one iteration and has a void body and void local state, 92 // initialized implicitly from the step type 93 MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, null}); 94 assertEquals(MethodType.methodType(void.class), loop.type()); 95 loop.invoke(); 96 } 97 98 @Test 99 public static void testLoopVoid3() throws Throwable { 100 // construct a post-checked loop that only does one iteration and has a void body and void local state, 101 // and that has a void finalizer 102 MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, Empty.MH_f}); 103 assertEquals(MethodType.methodType(void.class), loop.type()); 104 loop.invoke(); 105 } 106 107 @Test 108 public static void testLoopFacWithVoidState() throws Throwable { 109 // like testLoopFac, but with additional void state that outputs a dot 110 MethodHandle[] counterClause = new MethodHandle[]{Fac.MH_zero, Fac.MH_inc}; 111 MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}; 112 MethodHandle[] dotClause = new MethodHandle[]{null, Fac.MH_dot}; 113 MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause, dotClause); 114 assertEquals(Fac.MT_fac, loop.type()); 115 assertEquals(120, loop.invoke(5)); 116 } 117 118 @Test 119 public static void testLoopVoidInt() throws Throwable { 120 // construct a post-checked loop that only does one iteration and has a void body and void local state, 121 // and that returns a constant 122 MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, Empty.MH_c}); 123 assertEquals(MethodType.methodType(int.class), loop.type()); 124 assertEquals(23, loop.invoke()); 125 } 126 127 @Test 128 public static void testLoopWithVirtuals() throws Throwable { 129 // construct a loop (to calculate factorial) that uses a mix of static and virtual methods 130 MethodHandle[] counterClause = new MethodHandle[]{null, LoopWithVirtuals.permute(LoopWithVirtuals.MH_inc)}; 131 MethodHandle[] accumulatorClause = new MethodHandle[]{ 132 // init function must indicate the loop arguments (there is no other means to determine them) 133 MethodHandles.dropArguments(LoopWithVirtuals.MH_one, 0, LoopWithVirtuals.class), 134 LoopWithVirtuals.permute(LoopWithVirtuals.MH_mult), 135 LoopWithVirtuals.permute(LoopWithVirtuals.MH_pred), 136 LoopWithVirtuals.permute(LoopWithVirtuals.MH_fin) 137 }; 138 MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause); 139 assertEquals(LoopWithVirtuals.MT_loop, loop.type()); 140 assertEquals(120, loop.invoke(new LoopWithVirtuals(), 5)); 141 } 142 143 @DataProvider 144 static Object[][] negativeTestData() { 145 MethodHandle i0 = MethodHandles.constant(int.class, 0); 146 MethodHandle ii = MethodHandles.dropArguments(i0, 0, int.class, int.class); 147 MethodHandle id = MethodHandles.dropArguments(i0, 0, int.class, double.class); 148 MethodHandle i3 = MethodHandles.dropArguments(i0, 0, int.class, int.class, int.class); 149 List<MethodHandle> inits = Arrays.asList(ii, id, i3); 150 List<Class<?>> ints = Arrays.asList(int.class, int.class, int.class); 151 List<MethodHandle> finis = Arrays.asList(Fac.MH_fin, Fac.MH_inc, Counted.MH_step); 152 List<MethodHandle> preds1 = Arrays.asList(null, null, null); 153 List<MethodHandle> preds2 = Arrays.asList(null, Fac.MH_fin, null); 154 MethodHandle eek = MethodHandles.dropArguments(i0, 0, int.class, int.class, double.class); 155 List<MethodHandle> nesteps = Arrays.asList(Fac.MH_inc, eek, Fac.MH_dot); 156 List<MethodHandle> nepreds = Arrays.asList(null, Fac.MH_pred, null); 157 List<MethodHandle> nefinis = Arrays.asList(null, Fac.MH_fin, null); 158 List<MethodHandle> lvsteps = Arrays.asList(LoopWithVirtuals.MH_inc, LoopWithVirtuals.MH_mult); 159 List<MethodHandle> lvpreds = Arrays.asList(null, LoopWithVirtuals.MH_pred); 160 List<MethodHandle> lvfinis = Arrays.asList(null, LoopWithVirtuals.MH_fin); 161 return new Object[][] { 162 {null, "null or no clauses passed"}, 163 {new MethodHandle[][]{}, "null or no clauses passed"}, 164 {new MethodHandle[][]{{null, Fac.MH_inc}, {Fac.MH_one, null, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}}, 165 "All loop clauses must be represented as MethodHandle arrays with at most 4 elements."}, 166 {new MethodHandle[][]{{null, Fac.MH_inc}, null}, "null clauses are not allowed"}, 167 {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_dot}}, 168 "clause 0: init and step return types must match: int != void"}, 169 {new MethodHandle[][]{{ii}, {id}, {i3}}, 170 "found non-effectively identical init parameter type lists: " + inits + 171 " (common suffix: " + ints + ")"}, 172 {new MethodHandle[][]{{null, Fac.MH_inc, null, Fac.MH_fin}, {null, Fac.MH_inc, null, Fac.MH_inc}, 173 {null, Counted.MH_start, null, Counted.MH_step}}, 174 "found non-identical finalizer return types: " + finis + " (return type: int)"}, 175 {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, null, Fac.MH_fin}, 176 {null, Fac.MH_dot}}, "no predicate found: " + preds1}, 177 {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, Fac.MH_fin, Fac.MH_fin}, 178 {null, Fac.MH_dot}}, "predicates must have boolean return type: " + preds2}, 179 {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, eek, Fac.MH_pred, Fac.MH_fin}, 180 {null, Fac.MH_dot}}, 181 "found non-effectively identical parameter type lists:\nstep: " + nesteps + 182 "\npred: " + nepreds + "\nfini: " + nefinis + " (common parameter sequence: " + ints + ")"}, 183 {new MethodHandle[][]{{null, LoopWithVirtuals.MH_inc}, 184 {LoopWithVirtuals.MH_one, LoopWithVirtuals.MH_mult, LoopWithVirtuals.MH_pred, LoopWithVirtuals.MH_fin}}, 185 "found non-effectively identical parameter type lists:\nstep: " + lvsteps + 186 "\npred: " + lvpreds + "\nfini: " + lvfinis + " (common parameter sequence: " + ints + ")"} 187 }; 188 } 189 190 static final MethodHandle MH_loop; 191 192 static { 193 try { 194 MH_loop = LOOKUP.findStatic(MethodHandles.class, "loop", methodType(MethodHandle.class, MethodHandle[][].class)); 195 } catch (NoSuchMethodException | IllegalAccessException e) { 196 throw new ExceptionInInitializerError(e); 197 } 198 } 199 200 @Test(dataProvider = "negativeTestData") 201 public static void testLoopNegative(MethodHandle[][] clauses, String expectedMessage) throws Throwable { 202 boolean caught = false; 203 try { 204 MH_loop.invokeWithArguments(clauses); 205 } catch (IllegalArgumentException iae) { 206 assertEquals(expectedMessage, iae.getMessage()); 207 caught = true; 208 } 209 assertTrue(caught); 210 } 211 212 @Test 213 public static void testWhileLoop() throws Throwable { 214 // int i = 0; while (i < limit) { ++i; } return i; => limit 215 MethodHandle loop = MethodHandles.whileLoop(While.MH_zero, While.MH_pred, While.MH_step); 216 assertEquals(While.MT_while, loop.type()); 217 assertEquals(23, loop.invoke(23)); 218 } 219 220 @Test 221 public static void testWhileLoopNoIteration() throws Throwable { 222 // a while loop that never executes its body because the predicate evaluates to false immediately 223 MethodHandle loop = MethodHandles.whileLoop(While.MH_initString, While.MH_predString, While.MH_stepString); 224 assertEquals(While.MT_string, loop.type()); 225 assertEquals("a", loop.invoke()); 226 } 227 228 @Test 229 public static void testDoWhileLoop() throws Throwable { 230 // int i = 0; do { ++i; } while (i < limit); return i; => limit 231 MethodHandle loop = MethodHandles.doWhileLoop(While.MH_zero, While.MH_step, While.MH_pred); 232 assertEquals(While.MT_while, loop.type()); 233 assertEquals(23, loop.invoke(23)); 234 } 235 236 @Test 237 public static void testWhileZip() throws Throwable { 238 MethodHandle loop = MethodHandles.doWhileLoop(While.MH_zipInitZip, While.MH_zipStep, While.MH_zipPred); 239 assertEquals(While.MT_zip, loop.type()); 240 List<String> a = Arrays.asList("a", "b", "c", "d"); 241 List<String> b = Arrays.asList("e", "f", "g", "h"); 242 List<String> zipped = Arrays.asList("a", "e", "b", "f", "c", "g", "d", "h"); 243 assertEquals(zipped, (List<String>) loop.invoke(a.iterator(), b.iterator())); 244 } 245 246 @Test 247 public static void testCountedLoop() throws Throwable { 248 // String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s; => a variation on a well known theme 249 MethodHandle fit13 = MethodHandles.constant(int.class, 13); 250 MethodHandle loop = MethodHandles.countedLoop(fit13, Counted.MH_start, Counted.MH_step); 251 assertEquals(Counted.MT_counted, loop.type()); 252 assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!")); 253 } 254 255 @Test 256 public static void testCountedArrayLoop() throws Throwable { 257 // int[] a = new int[]{0}; for (int i = 0; i < 13; ++i) { ++a[0]; } => a[0] == 13 258 MethodHandle fit13 = MethodHandles.dropArguments(MethodHandles.constant(int.class, 13), 0, int[].class); 259 MethodHandle loop = MethodHandles.countedLoop(fit13, null, Counted.MH_stepUpdateArray); 260 assertEquals(Counted.MT_arrayCounted, loop.type()); 261 int[] a = new int[]{0}; 262 loop.invoke(a); 263 assertEquals(13, a[0]); 264 } 265 266 @Test 267 public static void testCountedPrintingLoop() throws Throwable { 268 MethodHandle fit5 = MethodHandles.constant(int.class, 5); 269 MethodHandle loop = MethodHandles.countedLoop(fit5, null, Counted.MH_printHello); 270 assertEquals(Counted.MT_countedPrinting, loop.type()); 271 loop.invoke(); 272 } 273 274 @Test 275 public static void testCountedRangeLoop() throws Throwable { 276 // String s = "Lambdaman!"; for (int i = -5; i < 8; ++i) { s = "na " + s; } return s; => a well known theme 277 MethodHandle fitm5 = MethodHandles.dropArguments(Counted.MH_m5, 0, String.class); 278 MethodHandle fit8 = MethodHandles.dropArguments(Counted.MH_8, 0, String.class); 279 MethodHandle loop = MethodHandles.countedLoop(fitm5, fit8, Counted.MH_start, Counted.MH_step); 280 assertEquals(Counted.MT_counted, loop.type()); 281 assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!")); 282 } 283 284 @Test 285 public static void testIterateSum() throws Throwable { 286 // Integer[] a = new Integer[]{1,2,3,4,5,6}; int sum = 0; for (int e : a) { sum += e; } return sum; => 21 287 MethodHandle loop = MethodHandles.iteratedLoop(Iterate.MH_sumIterator, Iterate.MH_sumInit, Iterate.MH_sumStep); 288 assertEquals(Iterate.MT_sum, loop.type()); 289 assertEquals(21, loop.invoke(new Integer[]{1, 2, 3, 4, 5, 6})); 290 } 291 292 @Test 293 public static void testIterateReverse() throws Throwable { 294 MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_reverseInit, Iterate.MH_reverseStep); 295 assertEquals(Iterate.MT_reverse, loop.type()); 296 List<String> list = Arrays.asList("a", "b", "c", "d", "e"); 297 List<String> reversedList = Arrays.asList("e", "d", "c", "b", "a"); 298 assertEquals(reversedList, (List<String>) loop.invoke(list)); 299 } 300 301 @Test 302 public static void testIterateLength() throws Throwable { 303 MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_lengthInit, Iterate.MH_lengthStep); 304 assertEquals(Iterate.MT_length, loop.type()); 305 List<Double> list = Arrays.asList(23.0, 148.0, 42.0); 306 assertEquals(list.size(), (int) loop.invoke(list)); 307 } 308 309 @Test 310 public static void testIterateMap() throws Throwable { 311 MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_mapInit, Iterate.MH_mapStep); 312 assertEquals(Iterate.MT_map, loop.type()); 313 List<String> list = Arrays.asList("Hello", "world", "!"); 314 List<String> upList = Arrays.asList("HELLO", "WORLD", "!"); 315 assertEquals(upList, (List<String>) loop.invoke(list)); 316 } 317 318 @Test 319 public static void testIteratePrint() throws Throwable { 320 MethodHandle loop = MethodHandles.iteratedLoop(null, null, Iterate.MH_printStep); 321 assertEquals(Iterate.MT_print, loop.type()); 322 loop.invoke(Arrays.asList("hello", "world")); 323 } 324 325 @Test 326 public static void testIterateNullBody() { 327 boolean caught = false; 328 try { 329 MethodHandles.iteratedLoop(MethodHandles.identity(int.class), MethodHandles.identity(int.class), null); 330 } catch (IllegalArgumentException iae) { 331 assertEquals("iterated loop body must not be null", iae.getMessage()); 332 caught = true; 333 } 334 assertTrue(caught); 335 } 336 337 static class Empty { 338 339 static void f() { } 340 341 static boolean pred() { 342 return false; 343 } 344 345 static int c() { 346 return 23; 347 } 348 349 static final Class<Empty> EMPTY = Empty.class; 350 351 static final MethodType MT_f = methodType(void.class); 352 static final MethodType MT_pred = methodType(boolean.class); 353 static final MethodType MT_c = methodType(int.class); 354 355 static final MethodHandle MH_f; 356 static final MethodHandle MH_pred; 357 static final MethodHandle MH_c; 358 359 static { 360 try { 361 MH_f = LOOKUP.findStatic(EMPTY, "f", MT_f); 362 MH_pred = LOOKUP.findStatic(EMPTY, "pred", MT_pred); 363 MH_c = LOOKUP.findStatic(EMPTY, "c", MT_c); 364 } catch (Exception e) { 365 throw new ExceptionInInitializerError(e); 366 } 367 } 368 } 369 370 static class Fac { 371 372 static int zero(int k) { 373 return 0; 374 } 375 376 static int one(int k) { 377 return 1; 378 } 379 380 static boolean pred(int i, int acc, int k) { 381 return i < k; 382 } 383 384 static int inc(int i, int acc, int k) { 385 return i + 1; 386 } 387 388 static int mult(int i, int acc, int k) { 389 return i * acc; 390 } 391 392 static void dot(int i, int acc, int k) { 393 System.out.print('.'); 394 } 395 396 static int fin(int i, int acc, int k) { 397 return acc; 398 } 399 400 static final Class<Fac> FAC = Fac.class; 401 402 static final MethodType MT_init = methodType(int.class, int.class); 403 static final MethodType MT_fn = methodType(int.class, int.class, int.class, int.class); 404 static final MethodType MT_dot = methodType(void.class, int.class, int.class, int.class); 405 static final MethodType MT_pred = methodType(boolean.class, int.class, int.class, int.class); 406 407 static final MethodHandle MH_zero; 408 static final MethodHandle MH_one; 409 static final MethodHandle MH_pred; 410 static final MethodHandle MH_inc; 411 static final MethodHandle MH_mult; 412 static final MethodHandle MH_dot; 413 static final MethodHandle MH_fin; 414 415 static final MethodType MT_fac = methodType(int.class, int.class); 416 417 static { 418 try { 419 MH_zero = LOOKUP.findStatic(FAC, "zero", MT_init); 420 MH_one = LOOKUP.findStatic(FAC, "one", MT_init); 421 MH_pred = LOOKUP.findStatic(FAC, "pred", MT_pred); 422 MH_inc = LOOKUP.findStatic(FAC, "inc", MT_fn); 423 MH_mult = LOOKUP.findStatic(FAC, "mult", MT_fn); 424 MH_dot = LOOKUP.findStatic(FAC, "dot", MT_dot); 425 MH_fin = LOOKUP.findStatic(FAC, "fin", MT_fn); 426 } catch (Exception e) { 427 throw new ExceptionInInitializerError(e); 428 } 429 } 430 431 } 432 433 static class Loop { 434 435 static int inc(int i, int k) { 436 return i + 1; 437 } 438 439 static boolean pred(int i, int k) { 440 return i < k; 441 } 442 443 static int fin(int i, int k) { 444 return k; 445 } 446 447 static final Class<Loop> LOOP = Loop.class; 448 449 static final MethodType MT_inc = methodType(int.class, int.class, int.class); 450 static final MethodType MT_pred = methodType(boolean.class, int.class, int.class); 451 static final MethodType MT_fin = methodType(int.class, int.class, int.class); 452 453 static final MethodHandle MH_inc; 454 static final MethodHandle MH_pred; 455 static final MethodHandle MH_fin; 456 457 static final MethodType MT_loop = methodType(int.class, int.class); 458 459 static { 460 try { 461 MH_inc = LOOKUP.findStatic(LOOP, "inc", MT_inc); 462 MH_pred = LOOKUP.findStatic(LOOP, "pred", MT_pred); 463 MH_fin = LOOKUP.findStatic(LOOP, "fin", MT_fin); 464 } catch (Exception e) { 465 throw new ExceptionInInitializerError(e); 466 } 467 } 468 469 } 470 471 static class LoopWithVirtuals { 472 473 static int one(int k) { 474 return 1; 475 } 476 477 int inc(int i, int acc, int k) { 478 return i + 1; 479 } 480 481 int mult(int i, int acc, int k) { 482 return i * acc; 483 } 484 485 boolean pred(int i, int acc, int k) { 486 return i < k; 487 } 488 489 int fin(int i, int acc, int k) { 490 return acc; 491 } 492 493 static final Class<LoopWithVirtuals> LOOP_WITH_VIRTUALS = LoopWithVirtuals.class; 494 495 static final MethodType MT_one = methodType(int.class, int.class); 496 static final MethodType MT_inc = methodType(int.class, int.class, int.class, int.class); 497 static final MethodType MT_mult = methodType(int.class, int.class, int.class, int.class); 498 static final MethodType MT_pred = methodType(boolean.class, int.class, int.class, int.class); 499 static final MethodType MT_fin = methodType(int.class, int.class, int.class, int.class); 500 501 static final MethodHandle MH_one; 502 static final MethodHandle MH_inc; 503 static final MethodHandle MH_mult; 504 static final MethodHandle MH_pred; 505 static final MethodHandle MH_fin; 506 507 static final MethodType MT_loop = methodType(int.class, LOOP_WITH_VIRTUALS, int.class); 508 509 static { 510 try { 511 MH_one = LOOKUP.findStatic(LOOP_WITH_VIRTUALS, "one", MT_one); 512 MH_inc = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "inc", MT_inc); 513 MH_mult = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "mult", MT_mult); 514 MH_pred = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "pred", MT_pred); 515 MH_fin = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "fin", MT_fin); 516 } catch (Exception e) { 517 throw new ExceptionInInitializerError(e); 518 } 519 } 520 521 static MethodHandle permute(MethodHandle h) { 522 // The handles representing virtual methods need to be rearranged to match the required order of arguments 523 // (loop-local state comes first, then loop arguments). As the receiver comes first in the signature but is 524 // a loop argument, it must be moved to the appropriate position in the signature. 525 return MethodHandles.permuteArguments(h, 526 methodType(h.type().returnType(), int.class, int.class, LOOP_WITH_VIRTUALS, int.class), 2, 0, 1, 3); 527 } 528 529 } 530 531 static class While { 532 533 static int zero(int limit) { 534 return 0; 535 } 536 537 static boolean pred(int i, int limit) { 538 return i < limit; 539 } 540 541 static int step(int i, int limit) { 542 return i + 1; 543 } 544 545 static String initString() { 546 return "a"; 547 } 548 549 static boolean predString(String s) { 550 return s.length() != 1; 551 } 552 553 static String stepString(String s) { 554 return s + "a"; 555 } 556 557 static List<String> zipInitZip(Iterator<String> a, Iterator<String> b) { 558 return new ArrayList<>(); 559 } 560 561 static boolean zipPred(List<String> zip, Iterator<String> a, Iterator<String> b) { 562 return a.hasNext() && b.hasNext(); 563 } 564 565 static List<String> zipStep(List<String> zip, Iterator<String> a, Iterator<String> b) { 566 zip.add(a.next()); 567 zip.add(b.next()); 568 return zip; 569 } 570 571 static final Class<While> WHILE = While.class; 572 573 static final MethodType MT_zero = methodType(int.class, int.class); 574 static final MethodType MT_pred = methodType(boolean.class, int.class, int.class); 575 static final MethodType MT_fn = methodType(int.class, int.class, int.class); 576 static final MethodType MT_initString = methodType(String.class); 577 static final MethodType MT_predString = methodType(boolean.class, String.class); 578 static final MethodType MT_stepString = methodType(String.class, String.class); 579 static final MethodType MT_zipInitZip = methodType(List.class, Iterator.class, Iterator.class); 580 static final MethodType MT_zipPred = methodType(boolean.class, List.class, Iterator.class, Iterator.class); 581 static final MethodType MT_zipStep = methodType(List.class, List.class, Iterator.class, Iterator.class); 582 583 static final MethodHandle MH_zero; 584 static final MethodHandle MH_pred; 585 static final MethodHandle MH_step; 586 static final MethodHandle MH_initString; 587 static final MethodHandle MH_predString; 588 static final MethodHandle MH_stepString; 589 static final MethodHandle MH_zipInitZip; 590 static final MethodHandle MH_zipPred; 591 static final MethodHandle MH_zipStep; 592 593 static final MethodType MT_while = methodType(int.class, int.class); 594 static final MethodType MT_string = methodType(String.class); 595 static final MethodType MT_zip = methodType(List.class, Iterator.class, Iterator.class); 596 597 static { 598 try { 599 MH_zero = LOOKUP.findStatic(WHILE, "zero", MT_zero); 600 MH_pred = LOOKUP.findStatic(WHILE, "pred", MT_pred); 601 MH_step = LOOKUP.findStatic(WHILE, "step", MT_fn); 602 MH_initString = LOOKUP.findStatic(WHILE, "initString", MT_initString); 603 MH_predString = LOOKUP.findStatic(WHILE, "predString", MT_predString); 604 MH_stepString = LOOKUP.findStatic(WHILE, "stepString", MT_stepString); 605 MH_zipInitZip = LOOKUP.findStatic(WHILE, "zipInitZip", MT_zipInitZip); 606 MH_zipPred = LOOKUP.findStatic(WHILE, "zipPred", MT_zipPred); 607 MH_zipStep = LOOKUP.findStatic(WHILE, "zipStep", MT_zipStep); 608 } catch (Exception e) { 609 throw new ExceptionInInitializerError(e); 610 } 611 } 612 613 } 614 615 static class Counted { 616 617 static String start(String arg) { 618 return arg; 619 } 620 621 static String step(int counter, String v, String arg) { 622 return "na " + v; 623 } 624 625 static void stepUpdateArray(int counter, int[] a) { 626 ++a[0]; 627 } 628 629 static void printHello(int counter) { 630 System.out.print("hello"); 631 } 632 633 static final Class<Counted> COUNTED = Counted.class; 634 635 static final MethodType MT_start = methodType(String.class, String.class); 636 static final MethodType MT_step = methodType(String.class, int.class, String.class, String.class); 637 static final MethodType MT_stepUpdateArray = methodType(void.class, int.class, int[].class); 638 static final MethodType MT_printHello = methodType(void.class, int.class); 639 640 static final MethodHandle MH_13; 641 static final MethodHandle MH_m5; 642 static final MethodHandle MH_8; 643 static final MethodHandle MH_start; 644 static final MethodHandle MH_step; 645 static final MethodHandle MH_stepUpdateArray; 646 static final MethodHandle MH_printHello; 647 648 static final MethodType MT_counted = methodType(String.class, String.class); 649 static final MethodType MT_arrayCounted = methodType(void.class, int[].class); 650 static final MethodType MT_countedPrinting = methodType(void.class); 651 652 static { 653 try { 654 MH_13 = MethodHandles.constant(int.class, 13); 655 MH_m5 = MethodHandles.constant(int.class, -5); 656 MH_8 = MethodHandles.constant(int.class, 8); 657 MH_start = LOOKUP.findStatic(COUNTED, "start", MT_start); 658 MH_step = LOOKUP.findStatic(COUNTED, "step", MT_step); 659 MH_stepUpdateArray = LOOKUP.findStatic(COUNTED, "stepUpdateArray", MT_stepUpdateArray); 660 MH_printHello = LOOKUP.findStatic(COUNTED, "printHello", MT_printHello); 661 } catch (Exception e) { 662 throw new ExceptionInInitializerError(e); 663 } 664 } 665 666 } 667 668 static class Iterate { 669 670 static Iterator<Integer> sumIterator(Integer[] a) { 671 return Arrays.asList(a).iterator(); 672 } 673 674 static int sumInit(Integer[] a) { 675 return 0; 676 } 677 678 static int sumStep(int s, int e, Integer[] a) { 679 return s + e; 680 } 681 682 static List<String> reverseInit(List<String> l) { 683 return new ArrayList<>(); 684 } 685 686 static List<String> reverseStep(String e, List<String> r, List<String> l) { 687 r.add(0, e); 688 return r; 689 } 690 691 static int lengthInit(List<Double> l) { 692 return 0; 693 } 694 695 static int lengthStep(Object o, int len, List<Double> l) { 696 return len + 1; 697 } 698 699 static List<String> mapInit(List<String> l) { 700 return new ArrayList<>(); 701 } 702 703 static List<String> mapStep(String e, List<String> r, List<String> l) { 704 r.add(e.toUpperCase()); 705 return r; 706 } 707 708 static void printStep(String s, List<String> l) { 709 System.out.print(s); 710 } 711 712 static final Class<Iterate> ITERATE = Iterate.class; 713 714 static final MethodType MT_sumIterator = methodType(Iterator.class, Integer[].class); 715 716 static final MethodType MT_sumInit = methodType(int.class, Integer[].class); 717 static final MethodType MT_reverseInit = methodType(List.class, List.class); 718 static final MethodType MT_lenghInit = methodType(int.class, List.class); 719 static final MethodType MT_mapInit = methodType(List.class, List.class); 720 721 static final MethodType MT_sumStep = methodType(int.class, int.class, int.class, Integer[].class); 722 static final MethodType MT_reverseStep = methodType(List.class, String.class, List.class, List.class); 723 static final MethodType MT_lengthStep = methodType(int.class, Object.class, int.class, List.class); 724 static final MethodType MT_mapStep = methodType(List.class, String.class, List.class, List.class); 725 static final MethodType MT_printStep = methodType(void.class, String.class, List.class); 726 727 static final MethodHandle MH_sumIterator; 728 static final MethodHandle MH_sumInit; 729 static final MethodHandle MH_sumStep; 730 static final MethodHandle MH_printStep; 731 732 static final MethodHandle MH_reverseInit; 733 static final MethodHandle MH_reverseStep; 734 735 static final MethodHandle MH_lengthInit; 736 static final MethodHandle MH_lengthStep; 737 738 static final MethodHandle MH_mapInit; 739 static final MethodHandle MH_mapStep; 740 741 static final MethodType MT_sum = methodType(int.class, Integer[].class); 742 static final MethodType MT_reverse = methodType(List.class, List.class); 743 static final MethodType MT_length = methodType(int.class, List.class); 744 static final MethodType MT_map = methodType(List.class, List.class); 745 static final MethodType MT_print = methodType(void.class, List.class); 746 747 static { 748 try { 749 MH_sumIterator = LOOKUP.findStatic(ITERATE, "sumIterator", MT_sumIterator); 750 MH_sumInit = LOOKUP.findStatic(ITERATE, "sumInit", MT_sumInit); 751 MH_sumStep = LOOKUP.findStatic(ITERATE, "sumStep", MT_sumStep); 752 MH_reverseInit = LOOKUP.findStatic(ITERATE, "reverseInit", MT_reverseInit); 753 MH_reverseStep = LOOKUP.findStatic(ITERATE, "reverseStep", MT_reverseStep); 754 MH_lengthInit = LOOKUP.findStatic(ITERATE, "lengthInit", MT_lenghInit); 755 MH_lengthStep = LOOKUP.findStatic(ITERATE, "lengthStep", MT_lengthStep); 756 MH_mapInit = LOOKUP.findStatic(ITERATE, "mapInit", MT_mapInit); 757 MH_mapStep = LOOKUP.findStatic(ITERATE, "mapStep", MT_mapStep); 758 MH_printStep = LOOKUP.findStatic(ITERATE, "printStep", MT_printStep); 759 } catch (Exception e) { 760 throw new ExceptionInInitializerError(e); 761 } 762 } 763 764 } 765 766 }