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  * @bug 8150956
  30  * @bug 8150957
  31  * @bug 8152667
  32  * @bug 8153637
  33  * @bug 8154751
  34  * @bug 8154754
  35  * @run testng/othervm -ea -esa test.java.lang.invoke.LoopCombinatorTest
  36  */
  37 
  38 package test.java.lang.invoke;
  39 
  40 import java.lang.invoke.MethodHandle;
  41 import java.lang.invoke.MethodHandles;
  42 import java.lang.invoke.MethodHandles.Lookup;
  43 import java.lang.invoke.MethodType;
  44 import java.util.*;
  45 
  46 import static java.lang.invoke.MethodType.methodType;
  47 
  48 import static org.testng.AssertJUnit.*;
  49 
  50 import org.testng.annotations.*;
  51 
  52 /**
  53  * Tests for the loop combinators introduced in JEP 274.
  54  */
  55 public class LoopCombinatorTest {
  56 
  57     static final Lookup LOOKUP = MethodHandles.lookup();
  58 
  59     @Test
  60     public static void testLoopFac() throws Throwable {
  61         MethodHandle[] counterClause = new MethodHandle[]{Fac.MH_zero, Fac.MH_inc};
  62         MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin};
  63         MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
  64         assertEquals(Fac.MT_fac, loop.type());
  65         assertEquals(120, loop.invoke(5));
  66     }
  67 
  68     @Test
  69     public static void testLoopFacNullInit() throws Throwable {
  70         // null initializer for counter, should initialize to 0
  71         MethodHandle[] counterClause = new MethodHandle[]{null, Fac.MH_inc};
  72         MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin};
  73         MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
  74         assertEquals(Fac.MT_fac, loop.type());
  75         assertEquals(120, loop.invoke(5));
  76     }
  77 
  78     @Test
  79     public static void testLoopNullInit() throws Throwable {
  80         // null initializer for counter, should initialize to 0, one-clause loop
  81         MethodHandle[] counterClause = new MethodHandle[]{null, Loop.MH_inc, Loop.MH_pred, Loop.MH_fin};
  82         MethodHandle loop = MethodHandles.loop(counterClause);
  83         assertEquals(Loop.MT_loop, loop.type());
  84         assertEquals(10, loop.invoke(10));
  85     }
  86 
  87     @Test
  88     public static void testLoopVoid1() throws Throwable {
  89         // construct a post-checked loop that only does one iteration and has a void body and void local state
  90         MethodHandle loop = MethodHandles.loop(new MethodHandle[]{Empty.MH_f, Empty.MH_f, Empty.MH_pred, null});
  91         assertEquals(MethodType.methodType(void.class), loop.type());
  92         loop.invoke();
  93     }
  94 
  95     @Test
  96     public static void testLoopVoid2() throws Throwable {
  97         // construct a post-checked loop that only does one iteration and has a void body and void local state,
  98         // initialized implicitly from the step type
  99         MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, null});
 100         assertEquals(MethodType.methodType(void.class), loop.type());
 101         loop.invoke();
 102     }
 103 
 104     @Test
 105     public static void testLoopVoid3() throws Throwable {
 106         // construct a post-checked loop that only does one iteration and has a void body and void local state,
 107         // and that has a void finalizer
 108         MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, Empty.MH_f});
 109         assertEquals(MethodType.methodType(void.class), loop.type());
 110         loop.invoke();
 111     }
 112 
 113     @Test
 114     public static void testLoopFacWithVoidState() throws Throwable {
 115         // like testLoopFac, but with additional void state that outputs a dot
 116         MethodHandle[] counterClause = new MethodHandle[]{Fac.MH_zero, Fac.MH_inc};
 117         MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin};
 118         MethodHandle[] dotClause = new MethodHandle[]{null, Fac.MH_dot};
 119         MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause, dotClause);
 120         assertEquals(Fac.MT_fac, loop.type());
 121         assertEquals(120, loop.invoke(5));
 122     }
 123 
 124     @Test
 125     public static void testLoopVoidInt() throws Throwable {
 126         // construct a post-checked loop that only does one iteration and has a void body and void local state,
 127         // and that returns a constant
 128         MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, Empty.MH_c});
 129         assertEquals(MethodType.methodType(int.class), loop.type());
 130         assertEquals(23, loop.invoke());
 131     }
 132 
 133     @Test
 134     public static void testLoopWithVirtuals() throws Throwable {
 135         // construct a loop (to calculate factorial) that uses a mix of static and virtual methods
 136         MethodHandle[] counterClause = new MethodHandle[]{null, LoopWithVirtuals.permute(LoopWithVirtuals.MH_inc)};
 137         MethodHandle[] accumulatorClause = new MethodHandle[]{
 138                 // init function must indicate the loop arguments (there is no other means to determine them)
 139                 MethodHandles.dropArguments(LoopWithVirtuals.MH_one, 0, LoopWithVirtuals.class),
 140                 LoopWithVirtuals.permute(LoopWithVirtuals.MH_mult),
 141                 LoopWithVirtuals.permute(LoopWithVirtuals.MH_pred),
 142                 LoopWithVirtuals.permute(LoopWithVirtuals.MH_fin)
 143         };
 144         MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
 145         assertEquals(LoopWithVirtuals.MT_loop, loop.type());
 146         assertEquals(120, loop.invoke(new LoopWithVirtuals(), 5));
 147     }
 148 
 149     @DataProvider
 150     static Object[][] negativeTestData() {
 151         MethodHandle i0 = MethodHandles.constant(int.class, 0);
 152         MethodHandle ii = MethodHandles.dropArguments(i0, 0, int.class, int.class);
 153         MethodHandle id = MethodHandles.dropArguments(i0, 0, int.class, double.class);
 154         MethodHandle i3 = MethodHandles.dropArguments(i0, 0, int.class, int.class, int.class);
 155         List<MethodHandle> inits = Arrays.asList(ii, id, i3);
 156         List<Class<?>> ints = Arrays.asList(int.class, int.class, int.class);
 157         List<MethodHandle> finis = Arrays.asList(Fac.MH_fin, Fac.MH_inc, Counted.MH_step);
 158         List<MethodHandle> preds1 = Arrays.asList(null, null, null);
 159         List<MethodHandle> preds2 = Arrays.asList(null, Fac.MH_fin, null);
 160         MethodHandle eek = MethodHandles.dropArguments(i0, 0, int.class, int.class, double.class);
 161         List<MethodHandle> nesteps = Arrays.asList(Fac.MH_inc, eek, Fac.MH_dot);
 162         List<MethodHandle> nepreds = Arrays.asList(null, Fac.MH_pred, null);
 163         List<MethodHandle> nefinis = Arrays.asList(null, Fac.MH_fin, null);
 164         List<MethodHandle> lvsteps = Arrays.asList(LoopWithVirtuals.MH_inc, LoopWithVirtuals.MH_mult);
 165         List<MethodHandle> lvpreds = Arrays.asList(null, LoopWithVirtuals.MH_pred);
 166         List<MethodHandle> lvfinis = Arrays.asList(null, LoopWithVirtuals.MH_fin);
 167         return new Object[][] {
 168                 {null, "null or no clauses passed"},
 169                 {new MethodHandle[][]{}, "null or no clauses passed"},
 170                 {new MethodHandle[][]{{null, Fac.MH_inc}, {Fac.MH_one, null, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}},
 171                         "All loop clauses must be represented as MethodHandle arrays with at most 4 elements."},
 172                 {new MethodHandle[][]{{null, Fac.MH_inc}, null}, "null clauses are not allowed"},
 173                 {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_dot}},
 174                         "clause 0: init and step return types must match: int != void"},
 175                 {new MethodHandle[][]{{ii}, {id}, {i3}},
 176                         "found non-effectively identical init parameter type lists: " + inits +
 177                                 " (common suffix: " + ints + ")"},
 178                 {new MethodHandle[][]{{null, Fac.MH_inc, null, Fac.MH_fin}, {null, Fac.MH_inc, null, Fac.MH_inc},
 179                         {null, Counted.MH_start, null, Counted.MH_step}},
 180                         "found non-identical finalizer return types: " + finis + " (return type: int)"},
 181                 {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, null, Fac.MH_fin},
 182                         {null, Fac.MH_dot}}, "no predicate found: " + preds1},
 183                 {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, Fac.MH_fin, Fac.MH_fin},
 184                         {null, Fac.MH_dot}}, "predicates must have boolean return type: " + preds2},
 185                 {new MethodHandle[][]{{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, eek, Fac.MH_pred, Fac.MH_fin},
 186                         {null, Fac.MH_dot}},
 187                         "found non-effectively identical parameter type lists:\nstep: " + nesteps +
 188                                 "\npred: " + nepreds + "\nfini: " + nefinis + " (common parameter sequence: " + ints + ")"},
 189                 {new MethodHandle[][]{{null, LoopWithVirtuals.MH_inc},
 190                         {LoopWithVirtuals.MH_one, LoopWithVirtuals.MH_mult, LoopWithVirtuals.MH_pred, LoopWithVirtuals.MH_fin}},
 191                         "found non-effectively identical parameter type lists:\nstep: " + lvsteps +
 192                                 "\npred: " + lvpreds + "\nfini: " + lvfinis + " (common parameter sequence: " + ints + ")"}
 193         };
 194     }
 195 
 196     static final MethodHandle MH_loop;
 197 
 198     static {
 199         try {
 200             MH_loop = LOOKUP.findStatic(MethodHandles.class, "loop", methodType(MethodHandle.class, MethodHandle[][].class));
 201         } catch (NoSuchMethodException | IllegalAccessException e) {
 202             throw new ExceptionInInitializerError(e);
 203         }
 204     }
 205 
 206     @Test(dataProvider = "negativeTestData")
 207     public static void testLoopNegative(MethodHandle[][] clauses, String expectedMessage) throws Throwable {
 208         boolean caught = false;
 209         try {
 210             MH_loop.invokeWithArguments(clauses);
 211         } catch (IllegalArgumentException iae) {
 212             assertEquals(expectedMessage, iae.getMessage());
 213             caught = true;
 214         }
 215         assertTrue(caught);
 216     }
 217 
 218     @Test
 219     public static void testWhileLoop() throws Throwable {
 220         // int i = 0; while (i < limit) { ++i; } return i; => limit
 221         MethodHandle loop = MethodHandles.whileLoop(While.MH_zero, While.MH_pred, While.MH_step);
 222         assertEquals(While.MT_while, loop.type());
 223         assertEquals(23, loop.invoke(23));
 224     }
 225 
 226     @Test
 227     public static void testWhileLoopNoIteration() throws Throwable {
 228         // a while loop that never executes its body because the predicate evaluates to false immediately
 229         MethodHandle loop = MethodHandles.whileLoop(While.MH_initString, While.MH_predString, While.MH_stepString);
 230         assertEquals(While.MT_string, loop.type());
 231         assertEquals("a", loop.invoke());
 232     }
 233 
 234     @Test
 235     public static void testDoWhileLoop() throws Throwable {
 236         // int i = 0; do { ++i; } while (i < limit); return i; => limit
 237         MethodHandle loop = MethodHandles.doWhileLoop(While.MH_zero, While.MH_step, While.MH_pred);
 238         assertEquals(While.MT_while, loop.type());
 239         assertEquals(23, loop.invoke(23));
 240     }
 241 
 242     @Test
 243     public static void testDoWhileNullInit() throws Throwable {
 244         While w = new While();
 245         int v = 5;
 246         MethodHandle loop = MethodHandles.doWhileLoop(null, While.MH_voidBody.bindTo(w), While.MH_voidPred.bindTo(w));
 247         assertEquals(While.MT_void, loop.type());
 248         loop.invoke(v);
 249         assertEquals(v, w.i);
 250     }
 251 
 252     @Test
 253     public static void testWhileZip() throws Throwable {
 254         MethodHandle loop = MethodHandles.doWhileLoop(While.MH_zipInitZip, While.MH_zipStep, While.MH_zipPred);
 255         assertEquals(While.MT_zip, loop.type());
 256         List<String> a = Arrays.asList("a", "b", "c", "d");
 257         List<String> b = Arrays.asList("e", "f", "g", "h");
 258         List<String> zipped = Arrays.asList("a", "e", "b", "f", "c", "g", "d", "h");
 259         assertEquals(zipped, (List<String>) loop.invoke(a.iterator(), b.iterator()));
 260     }
 261 
 262     @Test
 263     public static void testWhileNullInit() throws Throwable {
 264         While w = new While();
 265         int v = 5;
 266         MethodHandle loop = MethodHandles.whileLoop(null, While.MH_voidPred.bindTo(w), While.MH_voidBody.bindTo(w));
 267         assertEquals(While.MT_void, loop.type());
 268         loop.invoke(v);
 269         assertEquals(v, w.i);
 270     }
 271 
 272     @Test
 273     public static void testWhileVoidInit() throws Throwable {
 274         While w = new While();
 275         int v = 5;
 276         MethodHandle loop = MethodHandles.whileLoop(While.MH_voidInit.bindTo(w), While.MH_voidPred.bindTo(w),
 277                 While.MH_voidBody.bindTo(w));
 278         assertEquals(While.MT_void, loop.type());
 279         loop.invoke(v);
 280         assertEquals(v, w.i);
 281     }
 282 
 283     @Test
 284     public static void testDoWhileVoidInit() throws Throwable {
 285         While w = new While();
 286         int v = 5;
 287         MethodHandle loop = MethodHandles.doWhileLoop(While.MH_voidInit.bindTo(w), While.MH_voidBody.bindTo(w),
 288                 While.MH_voidPred.bindTo(w));
 289         assertEquals(While.MT_void, loop.type());
 290         loop.invoke(v);
 291         assertEquals(v, w.i);
 292     }
 293 
 294     @Test
 295     public static void testCountedLoop() throws Throwable {
 296         // String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s; => a variation on a well known theme
 297         MethodHandle fit13 = MethodHandles.constant(int.class, 13);
 298         MethodHandle loop = MethodHandles.countedLoop(fit13, Counted.MH_start, Counted.MH_step);
 299         assertEquals(Counted.MT_counted, loop.type());
 300         assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!"));
 301     }
 302 
 303     @Test
 304     public static void testCountedLoopVoidInit() throws Throwable {
 305         MethodHandle fit5 = MethodHandles.constant(int.class, 5);
 306         MethodHandle loop = MethodHandles.countedLoop(fit5, MethodHandles.zero(void.class), Counted.MH_printHello);
 307         assertEquals(Counted.MT_countedPrinting, loop.type());
 308         loop.invoke();
 309     }
 310 
 311     @Test
 312     public static void testCountedArrayLoop() throws Throwable {
 313         // int[] a = new int[]{0}; for (int i = 0; i < 13; ++i) { ++a[0]; } => a[0] == 13
 314         MethodHandle fit13 = MethodHandles.dropArguments(MethodHandles.constant(int.class, 13), 0, int[].class);
 315         MethodHandle loop = MethodHandles.countedLoop(fit13, null, Counted.MH_stepUpdateArray);
 316         assertEquals(Counted.MT_arrayCounted, loop.type());
 317         int[] a = new int[]{0};
 318         loop.invoke(a);
 319         assertEquals(13, a[0]);
 320     }
 321 
 322     @Test
 323     public static void testCountedPrintingLoop() throws Throwable {
 324         MethodHandle fit5 = MethodHandles.constant(int.class, 5);
 325         MethodHandle loop = MethodHandles.countedLoop(fit5, null, Counted.MH_printHello);
 326         assertEquals(Counted.MT_countedPrinting, loop.type());
 327         loop.invoke();
 328     }
 329 
 330     @Test
 331     public static void testCountedLoopNullBody() throws Throwable {
 332         MethodHandle h5 = MethodHandles.constant(int.class, 5);
 333         MethodHandle h13 = MethodHandles.constant(int.class, 13);
 334         MethodHandle loop = MethodHandles.countedLoop(h5, h13, null);
 335         assertEquals(methodType(int.class), loop.type());
 336         assertEquals(13, loop.invoke());
 337     }
 338 
 339     @Test
 340     public static void testCountedLoopNullIterations() throws Throwable {
 341         MethodHandle loop = MethodHandles.countedLoop(null, null, null);
 342         assertEquals(methodType(void.class), loop.type());
 343         loop.invoke();
 344     }
 345 
 346     @Test
 347     public static void testCountedLoopNullInitAndBody() throws Throwable {
 348         MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, 5), null, null);
 349         assertEquals(methodType(void.class), loop.type());
 350         loop.invoke();
 351     }
 352 
 353     @DataProvider
 354     static Object[][] countedLoopBodyParameters() {
 355         return new Object[][] {
 356                 {methodType(String.class), methodType(String.class, int.class)},
 357                 {methodType(String.class, List.class), methodType(String.class, int.class)},
 358                 {methodType(String.class, List.class), methodType(String.class, int.class, String.class)}
 359         };
 360     }
 361 
 362     @Test(dataProvider = "countedLoopBodyParameters")
 363     public static void testCountedLoopBodyParameters(MethodType initType, MethodType bodyType) throws Throwable {
 364         MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, 5),
 365                 MethodHandles.empty(initType), MethodHandles.empty(bodyType));
 366         assertEquals(initType, loop.type());
 367     }
 368 
 369     @DataProvider
 370     static Object[][] countedLoopTypes() {
 371         return new Object[][]{{void.class}, {int.class}, {Object.class}, {String.class}, {List.class}};
 372     }
 373 
 374     @Test(dataProvider = "countedLoopTypes")
 375     public static void testCountedLoopBodyParametersNullInit(Class<?> t) throws Throwable {
 376         MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, 5), null,
 377                 MethodHandles.empty(methodType(t, int.class)));
 378         assertEquals(methodType(t), loop.type());
 379         loop.invoke();
 380     }
 381 
 382     @Test
 383     public static void testCountedLoopStateDefinedByBody() throws Throwable {
 384         MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, 5), null, Counted.MH_stateBody);
 385         assertEquals(Counted.MT_bodyDeterminesState, loop.type());
 386         assertEquals("sssssnull01234", loop.invoke());
 387     }
 388 
 389     @Test
 390     public static void testCountedLoopArgsDefinedByIterations() throws Throwable {
 391         MethodHandle loop = MethodHandles.countedLoop(
 392                 MethodHandles.dropArguments(MethodHandles.constant(int.class, 3), 0, String.class),
 393                 null, Counted.MH_append);
 394         assertEquals(Counted.MT_iterationsDefineArgs, loop.type());
 395         assertEquals("hello012", loop.invoke("hello"));
 396     }
 397 
 398     @Test
 399     public static void testCountedRangeLoop() throws Throwable {
 400         // String s = "Lambdaman!"; for (int i = -5; i < 8; ++i) { s = "na " + s; } return s; => a well known theme
 401         MethodHandle fitm5 = MethodHandles.dropArguments(Counted.MH_m5, 0, String.class);
 402         MethodHandle fit8 = MethodHandles.dropArguments(Counted.MH_8, 0, String.class);
 403         MethodHandle loop = MethodHandles.countedLoop(fitm5, fit8, Counted.MH_start, Counted.MH_step);
 404         assertEquals(Counted.MT_counted, loop.type());
 405         assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!"));
 406     }
 407 
 408     @Test
 409     public static void testCountedLoopCounterInit() throws Throwable {
 410         // int x = 0; for (int i = 0; i < 5; ++i) { x += i; } return x; => 10
 411         // (only if counter's first value in body is 0)
 412         MethodHandle iter = MethodHandles.constant(int.class, 5);
 413         MethodHandle init = MethodHandles.constant(int.class, 0);
 414         MethodHandle body = Counted.MH_addCounter;
 415         MethodHandle loop = MethodHandles.countedLoop(iter, init, body);
 416         assertEquals(Counted.MT_counterInit, loop.type());
 417         assertEquals(10, loop.invoke());
 418     }
 419 
 420     @Test
 421     public static void testCountedLoopEmpty() throws Throwable {
 422         // for (int i = 0; i < 5; ++i) { /* empty */ }
 423         MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, 5), null, null);
 424         assertEquals(methodType(void.class), loop.type());
 425         loop.invoke();
 426     }
 427 
 428     @Test
 429     public static void testCountedRangeLoopEmpty() throws Throwable {
 430         // for (int i = -5; i < 5; ++i) { /* empty */ }
 431         MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, -5),
 432                 MethodHandles.constant(int.class, 5), null, null);
 433         assertEquals(methodType(void.class), loop.type());
 434         loop.invoke();
 435     }
 436 
 437     @Test
 438     public static void testIterateSum() throws Throwable {
 439         // Integer[] a = new Integer[]{1,2,3,4,5,6}; int sum = 0; for (int e : a) { sum += e; } return sum; => 21
 440         MethodHandle loop = MethodHandles.iteratedLoop(Iterate.MH_sumIterator, Iterate.MH_sumInit, Iterate.MH_sumStep);
 441         assertEquals(Iterate.MT_sum, loop.type());
 442         assertEquals(21, loop.invoke(new Integer[]{1, 2, 3, 4, 5, 6}));
 443     }
 444 
 445     @Test
 446     public static void testIterateReverse() throws Throwable {
 447         MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_reverseInit, Iterate.MH_reverseStep);
 448         assertEquals(Iterate.MT_reverse, loop.type());
 449         List<String> list = Arrays.asList("a", "b", "c", "d", "e");
 450         List<String> reversedList = Arrays.asList("e", "d", "c", "b", "a");
 451         assertEquals(reversedList, (List<String>) loop.invoke(list));
 452     }
 453 
 454     @Test
 455     public static void testIterateLength() throws Throwable {
 456         MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_lengthInit, Iterate.MH_lengthStep);
 457         assertEquals(Iterate.MT_length, loop.type());
 458         List<Double> list = Arrays.asList(23.0, 148.0, 42.0);
 459         assertEquals(list.size(), (int) loop.invoke(list));
 460     }
 461 
 462     @Test
 463     public static void testIterateMap() throws Throwable {
 464         MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_mapInit, Iterate.MH_mapStep);
 465         assertEquals(Iterate.MT_map, loop.type());
 466         List<String> list = Arrays.asList("Hello", "world", "!");
 467         List<String> upList = Arrays.asList("HELLO", "WORLD", "!");
 468         assertEquals(upList, (List<String>) loop.invoke(list));
 469     }
 470 
 471     @Test
 472     public static void testIteratePrint() throws Throwable {
 473         MethodHandle loop = MethodHandles.iteratedLoop(null, null, Iterate.MH_printStep);
 474         assertEquals(Iterate.MT_print, loop.type());
 475         loop.invoke(Arrays.asList("hello", "world"));
 476     }
 477 
 478     @Test
 479     public static void testIterateNullBody() {
 480         boolean caught = false;
 481         try {
 482             MethodHandles.iteratedLoop(MethodHandles.empty(methodType(Iterator.class, int.class)),
 483                     MethodHandles.identity(int.class), null);
 484         } catch (IllegalArgumentException iae) {
 485             assertEquals("iterated loop body must not be null", iae.getMessage());
 486             caught = true;
 487         }
 488         assertTrue(caught);
 489     }
 490 
 491     @DataProvider
 492     static Object[][] wrongIteratorTypes() {
 493         return new Object[][]{{void.class}, {Object.class}, {Iterable.class}};
 494     }
 495 
 496     @Test(dataProvider = "wrongIteratorTypes")
 497     public static void testIterateVoidIterator(Class<?> it) {
 498         boolean caught = false;
 499         MethodType v = methodType(it);
 500         try {
 501             MethodHandles.iteratedLoop(MethodHandles.empty(v), null, MethodHandles.empty(v));
 502         } catch(IllegalArgumentException iae) {
 503             assertEquals("iteratedLoop first argument must have Iterator return type", iae.getMessage());
 504             caught = true;
 505         }
 506         assertTrue(caught);
 507     }
 508 
 509     @Test
 510     public static void testIterateVoidInit() throws Throwable {
 511         MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_voidInit, Iterate.MH_printStep);
 512         assertEquals(Iterate.MT_print, loop.type());
 513         loop.invoke(Arrays.asList("hello", "world"));
 514     }
 515 
 516     @DataProvider
 517     static Object[][] iterateParameters() {
 518         MethodType i = methodType(int.class);
 519         MethodType sil_i = methodType(int.class, String.class, int.class, List.class);
 520         MethodType sl_v = methodType(void.class, String.class, List.class);
 521         MethodType l_it = methodType(Iterator.class, List.class);
 522         MethodType li_it = methodType(Iterator.class, List.class, int.class);
 523         MethodType l_i = methodType(int.class, List.class);
 524         MethodType _it = methodType(Iterator.class);
 525         MethodType si_i = methodType(int.class, String.class, int.class);
 526         MethodType s_i = methodType(int.class, String.class);
 527         return new Object[][]{
 528                 {null, null, sl_v},
 529                 {null, i, sil_i},
 530                 {null, l_i, sil_i},
 531                 {l_it, null, sl_v},
 532                 {l_it, i, sil_i},
 533                 {li_it, l_i, sil_i},
 534                 {l_it, null, sil_i},
 535                 {li_it, null, sl_v},
 536                 {_it, l_i, si_i},
 537                 {_it, l_i, s_i}
 538         };
 539     }
 540 
 541     @Test(dataProvider = "iterateParameters")
 542     public static void testIterateParameters(MethodType it, MethodType in, MethodType bo) throws Throwable {
 543         MethodHandle iterator = it == null ? null : MethodHandles.empty(it);
 544         MethodHandle init = in == null ? null : MethodHandles.empty(in);
 545         MethodHandle loop = MethodHandles.iteratedLoop(iterator, init, MethodHandles.empty(bo));
 546         MethodType lt = loop.type();
 547         if (it == null && in == null) {
 548             assertEquals(bo.dropParameterTypes(0, 1), lt);
 549         } else if (it == null) {
 550             if (in.parameterCount() == 0) {
 551                 assertEquals(bo.dropParameterTypes(0, in.returnType() == void.class ? 1 : 2), lt);
 552             } else {
 553                 assertEquals(methodType(bo.returnType(), in.parameterArray()), lt);
 554             }
 555         } else if (in == null) {
 556             assertEquals(methodType(bo.returnType(), it.parameterArray()), lt);
 557         } else if (it.parameterCount() > in.parameterCount()) {
 558             assertEquals(methodType(bo.returnType(), it.parameterArray()), lt);
 559         } else if (it.parameterCount() < in.parameterCount()) {
 560             assertEquals(methodType(bo.returnType(), in.parameterArray()), lt);
 561         } else {
 562             // both it, in present; with equal parameter list lengths
 563             assertEquals(it.parameterList(), lt.parameterList());
 564             assertEquals(in.parameterList(), lt.parameterList());
 565             assertEquals(bo.returnType(), lt.returnType());
 566         }
 567     }
 568 
 569     @Test
 570     public static void testIteratorSubclass() throws Throwable {
 571         MethodHandle loop = MethodHandles.iteratedLoop(MethodHandles.empty(methodType(BogusIterator.class, List.class)),
 572                 null, MethodHandles.empty(methodType(void.class, String.class)));
 573         assertEquals(methodType(void.class, List.class), loop.type());
 574     }
 575 
 576     static class BogusIterator implements Iterator {
 577         @Override
 578         public boolean hasNext() {
 579             return false;
 580         }
 581         @Override
 582         public Object next() {
 583             return null;
 584         }
 585     }
 586 
 587     static class Empty {
 588 
 589         static void f() { }
 590 
 591         static boolean pred() {
 592             return false;
 593         }
 594 
 595         static int c() {
 596             return 23;
 597         }
 598 
 599         static final Class<Empty> EMPTY = Empty.class;
 600 
 601         static final MethodType MT_f = methodType(void.class);
 602         static final MethodType MT_pred = methodType(boolean.class);
 603         static final MethodType MT_c = methodType(int.class);
 604 
 605         static final MethodHandle MH_f;
 606         static final MethodHandle MH_pred;
 607         static final MethodHandle MH_c;
 608 
 609         static {
 610             try {
 611                 MH_f = LOOKUP.findStatic(EMPTY, "f", MT_f);
 612                 MH_pred = LOOKUP.findStatic(EMPTY, "pred", MT_pred);
 613                 MH_c = LOOKUP.findStatic(EMPTY, "c", MT_c);
 614             } catch (Exception e) {
 615                 throw new ExceptionInInitializerError(e);
 616             }
 617         }
 618     }
 619 
 620     static class Fac {
 621 
 622         static int zero(int k) {
 623             return 0;
 624         }
 625 
 626         static int one(int k) {
 627             return 1;
 628         }
 629 
 630         static boolean pred(int i, int acc, int k) {
 631             return i < k;
 632         }
 633 
 634         static int inc(int i, int acc, int k) {
 635             return i + 1;
 636         }
 637 
 638         static int mult(int i, int acc, int k) {
 639             return i * acc;
 640         }
 641 
 642         static void dot(int i, int acc, int k) {
 643             System.out.print('.');
 644         }
 645 
 646         static int fin(int i, int acc, int k) {
 647             return acc;
 648         }
 649 
 650         static final Class<Fac> FAC = Fac.class;
 651 
 652         static final MethodType MT_init = methodType(int.class, int.class);
 653         static final MethodType MT_fn = methodType(int.class, int.class, int.class, int.class);
 654         static final MethodType MT_dot = methodType(void.class, int.class, int.class, int.class);
 655         static final MethodType MT_pred = methodType(boolean.class, int.class, int.class, int.class);
 656 
 657         static final MethodHandle MH_zero;
 658         static final MethodHandle MH_one;
 659         static final MethodHandle MH_pred;
 660         static final MethodHandle MH_inc;
 661         static final MethodHandle MH_mult;
 662         static final MethodHandle MH_dot;
 663         static final MethodHandle MH_fin;
 664 
 665         static final MethodType MT_fac = methodType(int.class, int.class);
 666 
 667         static {
 668             try {
 669                 MH_zero = LOOKUP.findStatic(FAC, "zero", MT_init);
 670                 MH_one = LOOKUP.findStatic(FAC, "one", MT_init);
 671                 MH_pred = LOOKUP.findStatic(FAC, "pred", MT_pred);
 672                 MH_inc = LOOKUP.findStatic(FAC, "inc", MT_fn);
 673                 MH_mult = LOOKUP.findStatic(FAC, "mult", MT_fn);
 674                 MH_dot = LOOKUP.findStatic(FAC, "dot", MT_dot);
 675                 MH_fin = LOOKUP.findStatic(FAC, "fin", MT_fn);
 676             } catch (Exception e) {
 677                 throw new ExceptionInInitializerError(e);
 678             }
 679         }
 680 
 681     }
 682 
 683     static class Loop {
 684 
 685         static int inc(int i, int k) {
 686             return i + 1;
 687         }
 688 
 689         static boolean pred(int i, int k) {
 690             return i < k;
 691         }
 692 
 693         static int fin(int i, int k) {
 694             return k;
 695         }
 696 
 697         static final Class<Loop> LOOP = Loop.class;
 698 
 699         static final MethodType MT_inc = methodType(int.class, int.class, int.class);
 700         static final MethodType MT_pred = methodType(boolean.class, int.class, int.class);
 701         static final MethodType MT_fin = methodType(int.class, int.class, int.class);
 702 
 703         static final MethodHandle MH_inc;
 704         static final MethodHandle MH_pred;
 705         static final MethodHandle MH_fin;
 706 
 707         static final MethodType MT_loop = methodType(int.class, int.class);
 708 
 709         static {
 710             try {
 711                 MH_inc = LOOKUP.findStatic(LOOP, "inc", MT_inc);
 712                 MH_pred = LOOKUP.findStatic(LOOP, "pred", MT_pred);
 713                 MH_fin = LOOKUP.findStatic(LOOP, "fin", MT_fin);
 714             } catch (Exception e) {
 715                 throw new ExceptionInInitializerError(e);
 716             }
 717         }
 718 
 719     }
 720 
 721     static class LoopWithVirtuals {
 722 
 723         static int one(int k) {
 724             return 1;
 725         }
 726 
 727         int inc(int i, int acc, int k) {
 728             return i + 1;
 729         }
 730 
 731         int mult(int i, int acc, int k) {
 732             return i * acc;
 733         }
 734 
 735         boolean pred(int i, int acc, int k) {
 736             return i < k;
 737         }
 738 
 739         int fin(int i, int acc, int k) {
 740             return acc;
 741         }
 742 
 743         static final Class<LoopWithVirtuals> LOOP_WITH_VIRTUALS = LoopWithVirtuals.class;
 744 
 745         static final MethodType MT_one = methodType(int.class, int.class);
 746         static final MethodType MT_inc = methodType(int.class, int.class, int.class, int.class);
 747         static final MethodType MT_mult = methodType(int.class, int.class, int.class, int.class);
 748         static final MethodType MT_pred = methodType(boolean.class, int.class, int.class, int.class);
 749         static final MethodType MT_fin = methodType(int.class, int.class, int.class, int.class);
 750 
 751         static final MethodHandle MH_one;
 752         static final MethodHandle MH_inc;
 753         static final MethodHandle MH_mult;
 754         static final MethodHandle MH_pred;
 755         static final MethodHandle MH_fin;
 756 
 757         static final MethodType MT_loop = methodType(int.class, LOOP_WITH_VIRTUALS, int.class);
 758 
 759         static {
 760             try {
 761                 MH_one = LOOKUP.findStatic(LOOP_WITH_VIRTUALS, "one", MT_one);
 762                 MH_inc = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "inc", MT_inc);
 763                 MH_mult = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "mult", MT_mult);
 764                 MH_pred = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "pred", MT_pred);
 765                 MH_fin = LOOKUP.findVirtual(LOOP_WITH_VIRTUALS, "fin", MT_fin);
 766             } catch (Exception e) {
 767                 throw new ExceptionInInitializerError(e);
 768             }
 769         }
 770 
 771         static MethodHandle permute(MethodHandle h) {
 772             // The handles representing virtual methods need to be rearranged to match the required order of arguments
 773             // (loop-local state comes first, then loop arguments). As the receiver comes first in the signature but is
 774             // a loop argument, it must be moved to the appropriate position in the signature.
 775             return MethodHandles.permuteArguments(h,
 776                     methodType(h.type().returnType(), int.class, int.class, LOOP_WITH_VIRTUALS, int.class), 2, 0, 1, 3);
 777         }
 778 
 779     }
 780 
 781     static class While {
 782 
 783         static int zero(int limit) {
 784             return 0;
 785         }
 786 
 787         static boolean pred(int i, int limit) {
 788             return i < limit;
 789         }
 790 
 791         static int step(int i, int limit) {
 792             return i + 1;
 793         }
 794 
 795         static String initString() {
 796             return "a";
 797         }
 798 
 799         static boolean predString(String s) {
 800             return s.length() != 1;
 801         }
 802 
 803         static String stepString(String s) {
 804             return s + "a";
 805         }
 806 
 807         static List<String> zipInitZip(Iterator<String> a, Iterator<String> b) {
 808             return new ArrayList<>();
 809         }
 810 
 811         static boolean zipPred(List<String> zip, Iterator<String> a, Iterator<String> b) {
 812             return a.hasNext() && b.hasNext();
 813         }
 814 
 815         static List<String> zipStep(List<String> zip, Iterator<String> a, Iterator<String> b) {
 816             zip.add(a.next());
 817             zip.add(b.next());
 818             return zip;
 819         }
 820 
 821         private int i = 0;
 822 
 823         void voidInit(int k) {
 824             // empty
 825         }
 826 
 827         void voidBody(int k) {
 828             ++i;
 829         }
 830 
 831         boolean voidPred(int k) {
 832             return i < k;
 833         }
 834 
 835         static final Class<While> WHILE = While.class;
 836 
 837         static final MethodType MT_zero = methodType(int.class, int.class);
 838         static final MethodType MT_pred = methodType(boolean.class, int.class, int.class);
 839         static final MethodType MT_fn = methodType(int.class, int.class, int.class);
 840         static final MethodType MT_initString = methodType(String.class);
 841         static final MethodType MT_predString = methodType(boolean.class, String.class);
 842         static final MethodType MT_stepString = methodType(String.class, String.class);
 843         static final MethodType MT_zipInitZip = methodType(List.class, Iterator.class, Iterator.class);
 844         static final MethodType MT_zipPred = methodType(boolean.class, List.class, Iterator.class, Iterator.class);
 845         static final MethodType MT_zipStep = methodType(List.class, List.class, Iterator.class, Iterator.class);
 846         static final MethodType MT_voidInit = methodType(void.class, int.class);
 847         static final MethodType MT_voidBody = methodType(void.class, int.class);
 848         static final MethodType MT_voidPred = methodType(boolean.class, int.class);
 849 
 850         static final MethodHandle MH_zero;
 851         static final MethodHandle MH_pred;
 852         static final MethodHandle MH_step;
 853         static final MethodHandle MH_initString;
 854         static final MethodHandle MH_predString;
 855         static final MethodHandle MH_stepString;
 856         static final MethodHandle MH_zipInitZip;
 857         static final MethodHandle MH_zipPred;
 858         static final MethodHandle MH_zipStep;
 859         static final MethodHandle MH_voidInit;
 860         static final MethodHandle MH_voidBody;
 861         static final MethodHandle MH_voidPred;
 862 
 863         static final MethodType MT_while = methodType(int.class, int.class);
 864         static final MethodType MT_string = methodType(String.class);
 865         static final MethodType MT_zip = methodType(List.class, Iterator.class, Iterator.class);
 866         static final MethodType MT_void = methodType(void.class, int.class);
 867 
 868         static {
 869             try {
 870                 MH_zero = LOOKUP.findStatic(WHILE, "zero", MT_zero);
 871                 MH_pred = LOOKUP.findStatic(WHILE, "pred", MT_pred);
 872                 MH_step = LOOKUP.findStatic(WHILE, "step", MT_fn);
 873                 MH_initString = LOOKUP.findStatic(WHILE, "initString", MT_initString);
 874                 MH_predString = LOOKUP.findStatic(WHILE, "predString", MT_predString);
 875                 MH_stepString = LOOKUP.findStatic(WHILE, "stepString", MT_stepString);
 876                 MH_zipInitZip = LOOKUP.findStatic(WHILE, "zipInitZip", MT_zipInitZip);
 877                 MH_zipPred = LOOKUP.findStatic(WHILE, "zipPred", MT_zipPred);
 878                 MH_zipStep = LOOKUP.findStatic(WHILE, "zipStep", MT_zipStep);
 879                 MH_voidInit = LOOKUP.findVirtual(WHILE, "voidInit", MT_voidInit);
 880                 MH_voidBody = LOOKUP.findVirtual(WHILE, "voidBody", MT_voidBody);
 881                 MH_voidPred = LOOKUP.findVirtual(WHILE, "voidPred", MT_voidPred);
 882             } catch (Exception e) {
 883                 throw new ExceptionInInitializerError(e);
 884             }
 885         }
 886 
 887     }
 888 
 889     static class Counted {
 890 
 891         static String start(String arg) {
 892             return arg;
 893         }
 894 
 895         static String step(int counter, String v, String arg) {
 896             return "na " + v;
 897         }
 898 
 899         static void stepUpdateArray(int counter, int[] a) {
 900             ++a[0];
 901         }
 902 
 903         static void printHello(int counter) {
 904             System.out.print("hello");
 905         }
 906 
 907         static int addCounter(int counter, int x) {
 908             return x + counter;
 909         }
 910 
 911         static String stateBody(int counter, String s) {
 912             return "s" + s + counter;
 913         }
 914 
 915         static String append(int counter, String localState, String loopArg) {
 916             if (null == localState) {
 917                 return loopArg + counter;
 918             }
 919             return localState + counter;
 920         }
 921 
 922         static final Class<Counted> COUNTED = Counted.class;
 923 
 924         static final MethodType MT_start = methodType(String.class, String.class);
 925         static final MethodType MT_step = methodType(String.class, int.class, String.class, String.class);
 926         static final MethodType MT_stepUpdateArray = methodType(void.class, int.class, int[].class);
 927         static final MethodType MT_printHello = methodType(void.class, int.class);
 928         static final MethodType MT_addCounter = methodType(int.class, int.class, int.class);
 929         static final MethodType MT_stateBody = methodType(String.class, int.class, String.class);
 930         static final MethodType MT_append = methodType(String.class, int.class, String.class, String.class);
 931 
 932         static final MethodHandle MH_13;
 933         static final MethodHandle MH_m5;
 934         static final MethodHandle MH_8;
 935         static final MethodHandle MH_start;
 936         static final MethodHandle MH_step;
 937         static final MethodHandle MH_stepUpdateArray;
 938         static final MethodHandle MH_printHello;
 939         static final MethodHandle MH_addCounter;
 940         static final MethodHandle MH_stateBody;
 941         static final MethodHandle MH_append;
 942 
 943         static final MethodType MT_counted = methodType(String.class, String.class);
 944         static final MethodType MT_arrayCounted = methodType(void.class, int[].class);
 945         static final MethodType MT_countedPrinting = methodType(void.class);
 946         static final MethodType MT_counterInit = methodType(int.class);
 947         static final MethodType MT_bodyDeterminesState = methodType(String.class);
 948         static final MethodType MT_iterationsDefineArgs = methodType(String.class, String.class);
 949 
 950         static {
 951             try {
 952                 MH_13 = MethodHandles.constant(int.class, 13);
 953                 MH_m5 = MethodHandles.constant(int.class, -5);
 954                 MH_8 = MethodHandles.constant(int.class, 8);
 955                 MH_start = LOOKUP.findStatic(COUNTED, "start", MT_start);
 956                 MH_step = LOOKUP.findStatic(COUNTED, "step", MT_step);
 957                 MH_stepUpdateArray = LOOKUP.findStatic(COUNTED, "stepUpdateArray", MT_stepUpdateArray);
 958                 MH_printHello = LOOKUP.findStatic(COUNTED, "printHello", MT_printHello);
 959                 MH_addCounter = LOOKUP.findStatic(COUNTED, "addCounter", MT_addCounter);
 960                 MH_stateBody = LOOKUP.findStatic(COUNTED, "stateBody", MT_stateBody);
 961                 MH_append = LOOKUP.findStatic(COUNTED, "append", MT_append);
 962             } catch (Exception e) {
 963                 throw new ExceptionInInitializerError(e);
 964             }
 965         }
 966 
 967     }
 968 
 969     static class Iterate {
 970 
 971         static Iterator<Integer> sumIterator(Integer[] a) {
 972             return Arrays.asList(a).iterator();
 973         }
 974 
 975         static int sumInit(Integer[] a) {
 976             return 0;
 977         }
 978 
 979         static int sumStep(int s, int e, Integer[] a) {
 980             return s + e;
 981         }
 982 
 983         static List<String> reverseInit(List<String> l) {
 984             return new ArrayList<>();
 985         }
 986 
 987         static List<String> reverseStep(String e, List<String> r, List<String> l) {
 988             r.add(0, e);
 989             return r;
 990         }
 991 
 992         static int lengthInit(List<Double> l) {
 993             return 0;
 994         }
 995 
 996         static int lengthStep(Object o, int len, List<Double> l) {
 997             return len + 1;
 998         }
 999 
1000         static List<String> mapInit(List<String> l) {
1001             return new ArrayList<>();
1002         }
1003 
1004         static List<String> mapStep(String e, List<String> r, List<String> l) {
1005             r.add(e.toUpperCase());
1006             return r;
1007         }
1008 
1009         static void printStep(String s, List<String> l) {
1010             System.out.print(s);
1011         }
1012 
1013         static void voidInit() {
1014             // empty
1015         }
1016 
1017         static final Class<Iterate> ITERATE = Iterate.class;
1018 
1019         static final MethodType MT_sumIterator = methodType(Iterator.class, Integer[].class);
1020 
1021         static final MethodType MT_sumInit = methodType(int.class, Integer[].class);
1022         static final MethodType MT_reverseInit = methodType(List.class, List.class);
1023         static final MethodType MT_lenghInit = methodType(int.class, List.class);
1024         static final MethodType MT_mapInit = methodType(List.class, List.class);
1025 
1026         static final MethodType MT_sumStep = methodType(int.class, int.class, int.class, Integer[].class);
1027         static final MethodType MT_reverseStep = methodType(List.class, String.class, List.class, List.class);
1028         static final MethodType MT_lengthStep = methodType(int.class, Object.class, int.class, List.class);
1029         static final MethodType MT_mapStep = methodType(List.class, String.class, List.class, List.class);
1030         static final MethodType MT_printStep = methodType(void.class, String.class, List.class);
1031 
1032         static final MethodType MT_voidInit = methodType(void.class);
1033 
1034         static final MethodHandle MH_sumIterator;
1035         static final MethodHandle MH_sumInit;
1036         static final MethodHandle MH_sumStep;
1037         static final MethodHandle MH_printStep;
1038 
1039         static final MethodHandle MH_reverseInit;
1040         static final MethodHandle MH_reverseStep;
1041 
1042         static final MethodHandle MH_lengthInit;
1043         static final MethodHandle MH_lengthStep;
1044 
1045         static final MethodHandle MH_mapInit;
1046         static final MethodHandle MH_mapStep;
1047 
1048         static final MethodHandle MH_voidInit;
1049 
1050         static final MethodType MT_sum = methodType(int.class, Integer[].class);
1051         static final MethodType MT_reverse = methodType(List.class, List.class);
1052         static final MethodType MT_length = methodType(int.class, List.class);
1053         static final MethodType MT_map = methodType(List.class, List.class);
1054         static final MethodType MT_print = methodType(void.class, List.class);
1055 
1056         static {
1057             try {
1058                 MH_sumIterator = LOOKUP.findStatic(ITERATE, "sumIterator", MT_sumIterator);
1059                 MH_sumInit = LOOKUP.findStatic(ITERATE, "sumInit", MT_sumInit);
1060                 MH_sumStep = LOOKUP.findStatic(ITERATE, "sumStep", MT_sumStep);
1061                 MH_reverseInit = LOOKUP.findStatic(ITERATE, "reverseInit", MT_reverseInit);
1062                 MH_reverseStep = LOOKUP.findStatic(ITERATE, "reverseStep", MT_reverseStep);
1063                 MH_lengthInit = LOOKUP.findStatic(ITERATE, "lengthInit", MT_lenghInit);
1064                 MH_lengthStep = LOOKUP.findStatic(ITERATE, "lengthStep", MT_lengthStep);
1065                 MH_mapInit = LOOKUP.findStatic(ITERATE, "mapInit", MT_mapInit);
1066                 MH_mapStep = LOOKUP.findStatic(ITERATE, "mapStep", MT_mapStep);
1067                 MH_printStep = LOOKUP.findStatic(ITERATE, "printStep", MT_printStep);
1068                 MH_voidInit = LOOKUP.findStatic(ITERATE, "voidInit", MT_voidInit);
1069             } catch (Exception e) {
1070                 throw new ExceptionInInitializerError(e);
1071             }
1072         }
1073 
1074     }
1075 
1076 }