jdk/test/java/lang/invoke/T8139885.java
changeset 36630 463ac0c1ec5e
parent 36629 765906c6f2a7
parent 36430 953635dbec06
child 36631 1742198594f8
equal deleted inserted replaced
36629:765906c6f2a7 36630:463ac0c1ec5e
     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 8143798
       
    29  * @run testng/othervm -ea -esa test.java.lang.invoke.T8139885
       
    30  */
       
    31 
       
    32 package test.java.lang.invoke;
       
    33 
       
    34 import java.io.StringWriter;
       
    35 import java.lang.invoke.MethodHandle;
       
    36 import java.lang.invoke.MethodHandles;
       
    37 import java.lang.invoke.MethodHandles.Lookup;
       
    38 import java.lang.invoke.MethodType;
       
    39 import java.lang.invoke.WrongMethodTypeException;
       
    40 import java.util.*;
       
    41 
       
    42 import static java.lang.invoke.MethodType.methodType;
       
    43 
       
    44 import static org.testng.AssertJUnit.*;
       
    45 
       
    46 import org.testng.annotations.*;
       
    47 
       
    48 /**
       
    49  * Example-scale and negative tests for JEP 274 extensions.
       
    50  */
       
    51 public class T8139885 {
       
    52 
       
    53     static final Lookup LOOKUP = MethodHandles.lookup();
       
    54 
       
    55     //
       
    56     // Tests.
       
    57     //
       
    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 testLoopVoid1() throws Throwable {
       
    80         // construct a post-checked loop that only does one iteration and has a void body and void local state
       
    81         MethodHandle loop = MethodHandles.loop(new MethodHandle[]{Empty.MH_f, Empty.MH_f, Empty.MH_pred, null});
       
    82         assertEquals(MethodType.methodType(void.class), loop.type());
       
    83         loop.invoke();
       
    84     }
       
    85 
       
    86     @Test
       
    87     public static void testLoopVoid2() throws Throwable {
       
    88         // construct a post-checked loop that only does one iteration and has a void body and void local state,
       
    89         // initialized implicitly from the step type
       
    90         MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, 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 testLoopFacWithVoidState() throws Throwable {
       
    97         // like testLoopFac, but with additional void state that outputs a dot
       
    98         MethodHandle[] counterClause = new MethodHandle[]{Fac.MH_zero, Fac.MH_inc};
       
    99         MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin};
       
   100         MethodHandle[] dotClause = new MethodHandle[]{null, Fac.MH_dot};
       
   101         MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause, dotClause);
       
   102         assertEquals(Fac.MT_fac, loop.type());
       
   103         assertEquals(120, loop.invoke(5));
       
   104     }
       
   105 
       
   106     @Test
       
   107     public static void testLoopNegative() throws Throwable {
       
   108         MethodHandle mh_loop =
       
   109                 LOOKUP.findStatic(MethodHandles.class, "loop", methodType(MethodHandle.class, MethodHandle[][].class));
       
   110         MethodHandle i0 = MethodHandles.constant(int.class, 0);
       
   111         MethodHandle ii = MethodHandles.dropArguments(i0, 0, int.class, int.class);
       
   112         MethodHandle id = MethodHandles.dropArguments(i0, 0, int.class, double.class);
       
   113         MethodHandle i3 = MethodHandles.dropArguments(i0, 0, int.class, int.class, int.class);
       
   114         List<MethodHandle> inits = Arrays.asList(ii, id, i3);
       
   115         List<Class<?>> ints = Arrays.asList(int.class, int.class, int.class);
       
   116         List<MethodHandle> finis = Arrays.asList(Fac.MH_fin, Fac.MH_inc, Counted.MH_step);
       
   117         List<MethodHandle> preds1 = Arrays.asList(null, null, null);
       
   118         List<MethodHandle> preds2 = Arrays.asList(null, Fac.MH_fin, null);
       
   119         MethodHandle eek = MethodHandles.dropArguments(i0, 0, int.class, int.class, double.class);
       
   120         List<MethodHandle> nesteps = Arrays.asList(Fac.MH_inc, eek, Fac.MH_dot);
       
   121         List<MethodHandle> nepreds = Arrays.asList(null, Fac.MH_pred, null);
       
   122         List<MethodHandle> nefinis = Arrays.asList(null, Fac.MH_fin, null);
       
   123         MethodHandle[][][] cases = {
       
   124                 null,
       
   125                 {},
       
   126                 {{null, Fac.MH_inc}, {Fac.MH_one, null, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin}},
       
   127                 {{null, Fac.MH_inc}, null},
       
   128                 {{Fac.MH_zero, Fac.MH_dot}},
       
   129                 {{ii}, {id}, {i3}},
       
   130                 {{null, Fac.MH_inc, null, Fac.MH_fin}, {null, Fac.MH_inc, null, Fac.MH_inc},
       
   131                         {null, Counted.MH_start, null, Counted.MH_step}},
       
   132                 {{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, null, Fac.MH_fin}, {null, Fac.MH_dot}},
       
   133                 {{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, Fac.MH_mult, Fac.MH_fin, Fac.MH_fin}, {null, Fac.MH_dot}},
       
   134                 {{Fac.MH_zero, Fac.MH_inc}, {Fac.MH_one, eek, Fac.MH_pred, Fac.MH_fin}, {null, Fac.MH_dot}}
       
   135         };
       
   136         String[] messages = {
       
   137                 "null or no clauses passed",
       
   138                 "null or no clauses passed",
       
   139                 "All loop clauses must be represented as MethodHandle arrays with at most 4 elements.",
       
   140                 "null clauses are not allowed",
       
   141                 "clause 0: init and step return types must match: int != void",
       
   142                 "found non-effectively identical init parameter type lists: " + inits + " (common suffix: " + ints + ")",
       
   143                 "found non-identical finalizer return types: " + finis + " (return type: int)",
       
   144                 "no predicate found: " + preds1,
       
   145                 "predicates must have boolean return type: " + preds2,
       
   146                 "found non-effectively identical parameter type lists:\nstep: " + nesteps + "\npred: " + nepreds +
       
   147                         "\nfini: " + nefinis + " (common parameter sequence: " + ints + ")"
       
   148         };
       
   149         for (int i = 0; i < cases.length; ++i) {
       
   150             boolean caught = false;
       
   151             try {
       
   152                 mh_loop.invokeWithArguments(cases[i]);
       
   153             } catch (IllegalArgumentException iae) {
       
   154                 assertEquals(messages[i], iae.getMessage());
       
   155                 caught = true;
       
   156             }
       
   157             assertTrue(caught);
       
   158         }
       
   159     }
       
   160 
       
   161     @Test
       
   162     public static void testWhileLoop() throws Throwable {
       
   163         // int i = 0; while (i < limit) { ++i; } return i; => limit
       
   164         MethodHandle loop = MethodHandles.whileLoop(While.MH_zero, While.MH_pred, While.MH_step);
       
   165         assertEquals(While.MT_while, loop.type());
       
   166         assertEquals(23, loop.invoke(23));
       
   167     }
       
   168 
       
   169     @Test
       
   170     public static void testWhileLoopNoIteration() throws Throwable {
       
   171         // a while loop that never executes its body because the predicate evaluates to false immediately
       
   172         MethodHandle loop = MethodHandles.whileLoop(While.MH_initString, While.MH_predString, While.MH_stepString);
       
   173         assertEquals(While.MT_string, loop.type());
       
   174         assertEquals("a", loop.invoke());
       
   175     }
       
   176 
       
   177     @Test
       
   178     public static void testDoWhileLoop() throws Throwable {
       
   179         // int i = 0; do { ++i; } while (i < limit); return i; => limit
       
   180         MethodHandle loop = MethodHandles.doWhileLoop(While.MH_zero, While.MH_step, While.MH_pred);
       
   181         assertEquals(While.MT_while, loop.type());
       
   182         assertEquals(23, loop.invoke(23));
       
   183     }
       
   184 
       
   185     @Test
       
   186     public static void testWhileZip() throws Throwable {
       
   187         MethodHandle loop = MethodHandles.doWhileLoop(While.MH_zipInitZip, While.MH_zipStep, While.MH_zipPred);
       
   188         assertEquals(While.MT_zip, loop.type());
       
   189         List<String> a = Arrays.asList("a", "b", "c", "d");
       
   190         List<String> b = Arrays.asList("e", "f", "g", "h");
       
   191         List<String> zipped = Arrays.asList("a", "e", "b", "f", "c", "g", "d", "h");
       
   192         assertEquals(zipped, (List<String>) loop.invoke(a.iterator(), b.iterator()));
       
   193     }
       
   194 
       
   195     @Test
       
   196     public static void testCountedLoop() throws Throwable {
       
   197         // String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s; => a variation on a well known theme
       
   198         MethodHandle fit13 = MethodHandles.constant(int.class, 13);
       
   199         MethodHandle loop = MethodHandles.countedLoop(fit13, Counted.MH_start, Counted.MH_step);
       
   200         assertEquals(Counted.MT_counted, loop.type());
       
   201         assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!"));
       
   202     }
       
   203 
       
   204     @Test
       
   205     public static void testCountedArrayLoop() throws Throwable {
       
   206         // int[] a = new int[]{0}; for (int i = 0; i < 13; ++i) { ++a[0]; } => a[0] == 13
       
   207         MethodHandle fit13 = MethodHandles.dropArguments(MethodHandles.constant(int.class, 13), 0, int[].class);
       
   208         MethodHandle loop = MethodHandles.countedLoop(fit13, null, Counted.MH_stepUpdateArray);
       
   209         assertEquals(Counted.MT_arrayCounted, loop.type());
       
   210         int[] a = new int[]{0};
       
   211         loop.invoke(a);
       
   212         assertEquals(13, a[0]);
       
   213     }
       
   214 
       
   215     @Test
       
   216     public static void testCountedPrintingLoop() throws Throwable {
       
   217         MethodHandle fit5 = MethodHandles.constant(int.class, 5);
       
   218         MethodHandle loop = MethodHandles.countedLoop(fit5, null, Counted.MH_printHello);
       
   219         assertEquals(Counted.MT_countedPrinting, loop.type());
       
   220         loop.invoke();
       
   221     }
       
   222 
       
   223     @Test
       
   224     public static void testCountedRangeLoop() throws Throwable {
       
   225         // String s = "Lambdaman!"; for (int i = -5; i < 8; ++i) { s = "na " + s; } return s; => a well known theme
       
   226         MethodHandle fitm5 = MethodHandles.dropArguments(Counted.MH_m5, 0, String.class);
       
   227         MethodHandle fit8 = MethodHandles.dropArguments(Counted.MH_8, 0, String.class);
       
   228         MethodHandle loop = MethodHandles.countedLoop(fitm5, fit8, Counted.MH_start, Counted.MH_step);
       
   229         assertEquals(Counted.MT_counted, loop.type());
       
   230         assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!"));
       
   231     }
       
   232 
       
   233     @Test
       
   234     public static void testIterateSum() throws Throwable {
       
   235         // Integer[] a = new Integer[]{1,2,3,4,5,6}; int sum = 0; for (int e : a) { sum += e; } return sum; => 21
       
   236         MethodHandle loop = MethodHandles.iteratedLoop(Iterate.MH_sumIterator, Iterate.MH_sumInit, Iterate.MH_sumStep);
       
   237         assertEquals(Iterate.MT_sum, loop.type());
       
   238         assertEquals(21, loop.invoke(new Integer[]{1, 2, 3, 4, 5, 6}));
       
   239     }
       
   240 
       
   241     @Test
       
   242     public static void testIterateReverse() throws Throwable {
       
   243         MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_reverseInit, Iterate.MH_reverseStep);
       
   244         assertEquals(Iterate.MT_reverse, loop.type());
       
   245         List<String> list = Arrays.asList("a", "b", "c", "d", "e");
       
   246         List<String> reversedList = Arrays.asList("e", "d", "c", "b", "a");
       
   247         assertEquals(reversedList, (List<String>) loop.invoke(list));
       
   248     }
       
   249 
       
   250     @Test
       
   251     public static void testIterateLength() throws Throwable {
       
   252         MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_lengthInit, Iterate.MH_lengthStep);
       
   253         assertEquals(Iterate.MT_length, loop.type());
       
   254         List<Double> list = Arrays.asList(23.0, 148.0, 42.0);
       
   255         assertEquals(list.size(), (int) loop.invoke(list));
       
   256     }
       
   257 
       
   258     @Test
       
   259     public static void testIterateMap() throws Throwable {
       
   260         MethodHandle loop = MethodHandles.iteratedLoop(null, Iterate.MH_mapInit, Iterate.MH_mapStep);
       
   261         assertEquals(Iterate.MT_map, loop.type());
       
   262         List<String> list = Arrays.asList("Hello", "world", "!");
       
   263         List<String> upList = Arrays.asList("HELLO", "WORLD", "!");
       
   264         assertEquals(upList, (List<String>) loop.invoke(list));
       
   265     }
       
   266 
       
   267     @Test
       
   268     public static void testIteratePrint() throws Throwable {
       
   269         MethodHandle loop = MethodHandles.iteratedLoop(null, null, Iterate.MH_printStep);
       
   270         assertEquals(Iterate.MT_print, loop.type());
       
   271         loop.invoke(Arrays.asList("hello", "world"));
       
   272     }
       
   273 
       
   274     @Test
       
   275     public static void testIterateNullBody() {
       
   276         boolean caught = false;
       
   277         try {
       
   278             MethodHandles.iteratedLoop(MethodHandles.identity(int.class), MethodHandles.identity(int.class), null);
       
   279         } catch (IllegalArgumentException iae) {
       
   280             assertEquals("iterated loop body must not be null", iae.getMessage());
       
   281             caught = true;
       
   282         }
       
   283         assertTrue(caught);
       
   284     }
       
   285 
       
   286     @Test
       
   287     public static void testTryFinally() throws Throwable {
       
   288         MethodHandle hello = MethodHandles.tryFinally(TryFinally.MH_greet, TryFinally.MH_exclaim);
       
   289         assertEquals(TryFinally.MT_hello, hello.type());
       
   290         assertEquals("Hello, world!", hello.invoke("world"));
       
   291     }
       
   292 
       
   293     @Test
       
   294     public static void testTryFinallyVoid() throws Throwable {
       
   295         MethodHandle tfVoid = MethodHandles.tryFinally(TryFinally.MH_print, TryFinally.MH_printMore);
       
   296         assertEquals(TryFinally.MT_printHello, tfVoid.type());
       
   297         tfVoid.invoke("world");
       
   298     }
       
   299 
       
   300     @Test
       
   301     public static void testTryFinallySublist() throws Throwable {
       
   302         MethodHandle helloMore = MethodHandles.tryFinally(TryFinally.MH_greetMore, TryFinally.MH_exclaimMore);
       
   303         assertEquals(TryFinally.MT_moreHello, helloMore.type());
       
   304         assertEquals("Hello, world and universe (but world first)!", helloMore.invoke("world", "universe"));
       
   305     }
       
   306 
       
   307     @Test
       
   308     public static void testTryFinallyNegative() {
       
   309         MethodHandle intid = MethodHandles.identity(int.class);
       
   310         MethodHandle intco = MethodHandles.constant(int.class, 0);
       
   311         MethodHandle errTarget = MethodHandles.dropArguments(intco, 0, int.class, double.class, String.class, int.class);
       
   312         MethodHandle errCleanup = MethodHandles.dropArguments(MethodHandles.constant(int.class, 0), 0, Throwable.class,
       
   313                 int.class, double.class, Object.class);
       
   314         MethodHandle[][] cases = {
       
   315                 {intid, MethodHandles.identity(double.class)},
       
   316                 {intid, MethodHandles.dropArguments(intid, 0, String.class)},
       
   317                 {intid, MethodHandles.dropArguments(intid, 0, Throwable.class, double.class)},
       
   318                 {errTarget, errCleanup}
       
   319         };
       
   320         String[] messages = {
       
   321                 "target and return types must match: double != int",
       
   322                 "cleanup first argument and Throwable must match: (String,int)int != class java.lang.Throwable",
       
   323                 "cleanup second argument and target return type must match: (Throwable,double,int)int != int",
       
   324                 "cleanup parameters after (Throwable,result) and target parameter list prefix must match: " +
       
   325                         errCleanup.type() + " != " + errTarget.type()
       
   326         };
       
   327         for (int i = 0; i < cases.length; ++i) {
       
   328             boolean caught = false;
       
   329             try {
       
   330                 MethodHandles.tryFinally(cases[i][0], cases[i][1]);
       
   331             } catch (IllegalArgumentException iae) {
       
   332                 assertEquals(messages[i], iae.getMessage());
       
   333                 caught = true;
       
   334             }
       
   335             assertTrue(caught);
       
   336         }
       
   337     }
       
   338 
       
   339     @Test
       
   340     public static void testFold0a() throws Throwable {
       
   341         // equivalence to foldArguments(MethodHandle,MethodHandle)
       
   342         MethodHandle fold = MethodHandles.foldArguments(Fold.MH_multer, 0, Fold.MH_adder);
       
   343         assertEquals(Fold.MT_folded1, fold.type());
       
   344         assertEquals(720, (int) fold.invoke(3, 4, 5));
       
   345     }
       
   346 
       
   347     @Test
       
   348     public static void testFold1a() throws Throwable {
       
   349         // test foldArguments for folding position 1
       
   350         MethodHandle fold = MethodHandles.foldArguments(Fold.MH_multer, 1, Fold.MH_adder1);
       
   351         assertEquals(Fold.MT_folded1, fold.type());
       
   352         assertEquals(540, (int) fold.invoke(3, 4, 5));
       
   353     }
       
   354 
       
   355     @Test
       
   356     public static void testFold0b() throws Throwable {
       
   357         // test foldArguments equivalence with multiple types
       
   358         MethodHandle fold = MethodHandles.foldArguments(Fold.MH_str, 0, Fold.MH_comb);
       
   359         assertEquals(Fold.MT_folded2, fold.type());
       
   360         assertEquals(23, (int) fold.invoke("true", true, 23));
       
   361     }
       
   362 
       
   363     @Test
       
   364     public static void testFold1b() throws Throwable {
       
   365         // test folgArguments for folding position 1, with multiple types
       
   366         MethodHandle fold = MethodHandles.foldArguments(Fold.MH_str, 1, Fold.MH_comb2);
       
   367         assertEquals(Fold.MT_folded3, fold.type());
       
   368         assertEquals(1, (int) fold.invoke(true, true, 1));
       
   369         assertEquals(-1, (int) fold.invoke(true, false, -1));
       
   370     }
       
   371 
       
   372     @Test
       
   373     public static void testFoldArgumentsExample() throws Throwable {
       
   374         // test the JavaDoc foldArguments-with-pos example
       
   375         StringWriter swr = new StringWriter();
       
   376         MethodHandle trace = LOOKUP.findVirtual(StringWriter.class, "write", methodType(void.class, String.class)).bindTo(swr);
       
   377         MethodHandle cat = LOOKUP.findVirtual(String.class, "concat", methodType(String.class, String.class));
       
   378         assertEquals("boojum", (String) cat.invokeExact("boo", "jum"));
       
   379         MethodHandle catTrace = MethodHandles.foldArguments(cat, 1, trace);
       
   380         assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
       
   381         assertEquals("jum", swr.toString());
       
   382     }
       
   383 
       
   384     @Test
       
   385     public static void testAsSpreader() throws Throwable {
       
   386         MethodHandle spreader = SpreadCollect.MH_forSpreading.asSpreader(1, int[].class, 3);
       
   387         assertEquals(SpreadCollect.MT_spreader, spreader.type());
       
   388         assertEquals("A456B", (String) spreader.invoke("A", new int[]{4, 5, 6}, "B"));
       
   389     }
       
   390 
       
   391     @Test
       
   392     public static void testAsSpreaderExample() throws Throwable {
       
   393         // test the JavaDoc asSpreader-with-pos example
       
   394         MethodHandle compare = LOOKUP.findStatic(Objects.class, "compare", methodType(int.class, Object.class, Object.class, Comparator.class));
       
   395         MethodHandle compare2FromArray = compare.asSpreader(0, Object[].class, 2);
       
   396         Object[] ints = new Object[]{3, 9, 7, 7};
       
   397         Comparator<Integer> cmp = (a, b) -> a - b;
       
   398         assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 0, 2), cmp) < 0);
       
   399         assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 1, 3), cmp) > 0);
       
   400         assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 2, 4), cmp) == 0);
       
   401     }
       
   402 
       
   403     @Test
       
   404     public static void testAsSpreaderIllegalPos() throws Throwable {
       
   405         int[] illegalPos = {-7, 3, 19};
       
   406         int caught = 0;
       
   407         for (int p : illegalPos) {
       
   408             try {
       
   409                 SpreadCollect.MH_forSpreading.asSpreader(p, Object[].class, 3);
       
   410             } catch (IllegalArgumentException iae) {
       
   411                 assertEquals("bad spread position", iae.getMessage());
       
   412                 ++caught;
       
   413             }
       
   414         }
       
   415         assertEquals(illegalPos.length, caught);
       
   416     }
       
   417 
       
   418     @Test
       
   419     public static void testAsSpreaderIllegalMethodType() throws Throwable {
       
   420         MethodHandle h = MethodHandles.dropArguments(MethodHandles.constant(String.class, ""), 0, int.class, int.class);
       
   421         boolean caught = false;
       
   422         try {
       
   423             MethodHandle s = h.asSpreader(String[].class, 1);
       
   424         } catch (WrongMethodTypeException wmte) {
       
   425             caught = true;
       
   426         }
       
   427         assertTrue(caught);
       
   428     }
       
   429 
       
   430     @Test
       
   431     public static void testAsCollector() throws Throwable {
       
   432         MethodHandle collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 1);
       
   433         assertEquals(SpreadCollect.MT_collector1, collector.type());
       
   434         assertEquals("A4B", (String) collector.invoke("A", 4, "B"));
       
   435         collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 2);
       
   436         assertEquals(SpreadCollect.MT_collector2, collector.type());
       
   437         assertEquals("A45B", (String) collector.invoke("A", 4, 5, "B"));
       
   438         collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 3);
       
   439         assertEquals(SpreadCollect.MT_collector3, collector.type());
       
   440         assertEquals("A456B", (String) collector.invoke("A", 4, 5, 6, "B"));
       
   441     }
       
   442 
       
   443     @Test
       
   444     public static void testAsCollectorInvokeWithArguments() throws Throwable {
       
   445         MethodHandle collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 1);
       
   446         assertEquals(SpreadCollect.MT_collector1, collector.type());
       
   447         assertEquals("A4B", (String) collector.invokeWithArguments("A", 4, "B"));
       
   448         collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 2);
       
   449         assertEquals(SpreadCollect.MT_collector2, collector.type());
       
   450         assertEquals("A45B", (String) collector.invokeWithArguments("A", 4, 5, "B"));
       
   451         collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 3);
       
   452         assertEquals(SpreadCollect.MT_collector3, collector.type());
       
   453         assertEquals("A456B", (String) collector.invokeWithArguments("A", 4, 5, 6, "B"));
       
   454     }
       
   455 
       
   456     @Test
       
   457     public static void testAsCollectorLeading() throws Throwable {
       
   458         MethodHandle collector = SpreadCollect.MH_forCollectingLeading.asCollector(0, int[].class, 1);
       
   459         assertEquals(SpreadCollect.MT_collectorLeading1, collector.type());
       
   460         assertEquals("7Q", (String) collector.invoke(7, "Q"));
       
   461         collector = SpreadCollect.MH_forCollectingLeading.asCollector(0, int[].class, 2);
       
   462         assertEquals(SpreadCollect.MT_collectorLeading2, collector.type());
       
   463         assertEquals("78Q", (String) collector.invoke(7, 8, "Q"));
       
   464         collector = SpreadCollect.MH_forCollectingLeading.asCollector(0, int[].class, 3);
       
   465         assertEquals(SpreadCollect.MT_collectorLeading3, collector.type());
       
   466         assertEquals("789Q", (String) collector.invoke(7, 8, 9, "Q"));
       
   467     }
       
   468 
       
   469     @Test
       
   470     public static void testAsCollectorLeadingInvokeWithArguments() throws Throwable {
       
   471         MethodHandle collector = SpreadCollect.MH_forCollectingLeading.asCollector(0, int[].class, 1);
       
   472         assertEquals(SpreadCollect.MT_collectorLeading1, collector.type());
       
   473         assertEquals("7Q", (String) collector.invokeWithArguments(7, "Q"));
       
   474         collector = SpreadCollect.MH_forCollectingLeading.asCollector(0, int[].class, 2);
       
   475         assertEquals(SpreadCollect.MT_collectorLeading2, collector.type());
       
   476         assertEquals("78Q", (String) collector.invokeWithArguments(7, 8, "Q"));
       
   477         collector = SpreadCollect.MH_forCollectingLeading.asCollector(0, int[].class, 3);
       
   478         assertEquals(SpreadCollect.MT_collectorLeading3, collector.type());
       
   479         assertEquals("789Q", (String) collector.invokeWithArguments(7, 8, 9, "Q"));
       
   480     }
       
   481 
       
   482     @Test
       
   483     public static void testAsCollectorNone() throws Throwable {
       
   484         MethodHandle collector = SpreadCollect.MH_forCollecting.asCollector(1, int[].class, 0);
       
   485         assertEquals(SpreadCollect.MT_collector0, collector.type());
       
   486         assertEquals("AB", (String) collector.invoke("A", "B"));
       
   487     }
       
   488 
       
   489     @Test
       
   490     public static void testAsCollectorIllegalPos() throws Throwable {
       
   491         int[] illegalPos = {-1, 17};
       
   492         int caught = 0;
       
   493         for (int p : illegalPos) {
       
   494             try {
       
   495                 SpreadCollect.MH_forCollecting.asCollector(p, int[].class, 0);
       
   496             } catch (IllegalArgumentException iae) {
       
   497                 assertEquals("bad collect position", iae.getMessage());
       
   498                 ++caught;
       
   499             }
       
   500         }
       
   501         assertEquals(illegalPos.length, caught);
       
   502     }
       
   503 
       
   504     @Test
       
   505     public static void testAsCollectorExample() throws Throwable {
       
   506         // test the JavaDoc asCollector-with-pos example
       
   507         StringWriter swr = new StringWriter();
       
   508         MethodHandle swWrite = LOOKUP.
       
   509                 findVirtual(StringWriter.class, "write", methodType(void.class, char[].class, int.class, int.class)).
       
   510                 bindTo(swr);
       
   511         MethodHandle swWrite4 = swWrite.asCollector(0, char[].class, 4);
       
   512         swWrite4.invoke('A', 'B', 'C', 'D', 1, 2);
       
   513         assertEquals("BC", swr.toString());
       
   514         swWrite4.invoke('P', 'Q', 'R', 'S', 0, 4);
       
   515         assertEquals("BCPQRS", swr.toString());
       
   516         swWrite4.invoke('W', 'X', 'Y', 'Z', 3, 1);
       
   517         assertEquals("BCPQRSZ", swr.toString());
       
   518     }
       
   519 
       
   520     @Test
       
   521     public static void testFindSpecial() throws Throwable {
       
   522         FindSpecial.C c = new FindSpecial.C();
       
   523         assertEquals("I1.m", c.m());
       
   524         MethodType t = MethodType.methodType(String.class);
       
   525         MethodHandle ci1m = LOOKUP.findSpecial(FindSpecial.I1.class, "m", t, FindSpecial.C.class);
       
   526         assertEquals("I1.m", (String) ci1m.invoke(c));
       
   527     }
       
   528 
       
   529     @Test
       
   530     public static void testFindSpecialAbstract() throws Throwable {
       
   531         FindSpecial.C c = new FindSpecial.C();
       
   532         assertEquals("q", c.q());
       
   533         MethodType t = MethodType.methodType(String.class);
       
   534         boolean caught = false;
       
   535         try {
       
   536             MethodHandle ci3q = LOOKUP.findSpecial(FindSpecial.I3.class, "q", t, FindSpecial.C.class);
       
   537         } catch (Throwable thrown) {
       
   538             if (!(thrown instanceof IllegalAccessException) || !FindSpecial.ABSTRACT_ERROR.equals(thrown.getMessage())) {
       
   539                 throw new AssertionError(thrown.getMessage(), thrown);
       
   540             }
       
   541             caught = true;
       
   542         }
       
   543         assertTrue(caught);
       
   544     }
       
   545 
       
   546     @Test
       
   547     public static void testFindClassCNFE() throws Throwable {
       
   548         boolean caught = false;
       
   549         try {
       
   550             LOOKUP.findClass("does.not.Exist");
       
   551         } catch (ClassNotFoundException cnfe) {
       
   552             caught = true;
       
   553         }
       
   554         assertTrue(caught);
       
   555     }
       
   556 
       
   557     //
       
   558     // Methods used to assemble tests.
       
   559     //
       
   560 
       
   561     static class Empty {
       
   562 
       
   563         static void f() { }
       
   564 
       
   565         static boolean pred() {
       
   566             return false;
       
   567         }
       
   568 
       
   569         static final Class<Empty> EMPTY = Empty.class;
       
   570 
       
   571         static final MethodType MT_f = methodType(void.class);
       
   572         static final MethodType MT_pred = methodType(boolean.class);
       
   573 
       
   574         static final MethodHandle MH_f;
       
   575         static final MethodHandle MH_pred;
       
   576 
       
   577         static {
       
   578             try {
       
   579                 MH_f = LOOKUP.findStatic(EMPTY, "f", MT_f);
       
   580                 MH_pred = LOOKUP.findStatic(EMPTY, "pred", MT_pred);
       
   581             } catch (Exception e) {
       
   582                 throw new ExceptionInInitializerError(e);
       
   583             }
       
   584         }
       
   585     }
       
   586 
       
   587     static class Fac {
       
   588 
       
   589         static int zero(int k) {
       
   590             return 0;
       
   591         }
       
   592 
       
   593         static int one(int k) {
       
   594             return 1;
       
   595         }
       
   596 
       
   597         static boolean pred(int i, int acc, int k) {
       
   598             return i < k;
       
   599         }
       
   600 
       
   601         static int inc(int i, int acc, int k) {
       
   602             return i + 1;
       
   603         }
       
   604 
       
   605         static int mult(int i, int acc, int k) {
       
   606             return i * acc;
       
   607         }
       
   608 
       
   609         static void dot(int i, int acc, int k) {
       
   610             System.out.print('.');
       
   611         }
       
   612 
       
   613         static int fin(int i, int acc, int k) {
       
   614             return acc;
       
   615         }
       
   616 
       
   617         static final Class<Fac> FAC = Fac.class;
       
   618 
       
   619         static final MethodType MT_init = methodType(int.class, int.class);
       
   620         static final MethodType MT_fn = methodType(int.class, int.class, int.class, int.class);
       
   621         static final MethodType MT_dot = methodType(void.class, int.class, int.class, int.class);
       
   622         static final MethodType MT_pred = methodType(boolean.class, int.class, int.class, int.class);
       
   623 
       
   624         static final MethodHandle MH_zero;
       
   625         static final MethodHandle MH_one;
       
   626         static final MethodHandle MH_pred;
       
   627         static final MethodHandle MH_inc;
       
   628         static final MethodHandle MH_mult;
       
   629         static final MethodHandle MH_dot;
       
   630         static final MethodHandle MH_fin;
       
   631 
       
   632         static final MethodType MT_fac = methodType(int.class, int.class);
       
   633 
       
   634         static {
       
   635             try {
       
   636                 MH_zero = LOOKUP.findStatic(FAC, "zero", MT_init);
       
   637                 MH_one = LOOKUP.findStatic(FAC, "one", MT_init);
       
   638                 MH_pred = LOOKUP.findStatic(FAC, "pred", MT_pred);
       
   639                 MH_inc = LOOKUP.findStatic(FAC, "inc", MT_fn);
       
   640                 MH_mult = LOOKUP.findStatic(FAC, "mult", MT_fn);
       
   641                 MH_dot = LOOKUP.findStatic(FAC, "dot", MT_dot);
       
   642                 MH_fin = LOOKUP.findStatic(FAC, "fin", MT_fn);
       
   643             } catch (Exception e) {
       
   644                 throw new ExceptionInInitializerError(e);
       
   645             }
       
   646         }
       
   647 
       
   648     }
       
   649 
       
   650     static class While {
       
   651 
       
   652         static int zero(int limit) {
       
   653             return 0;
       
   654         }
       
   655 
       
   656         static boolean pred(int i, int limit) {
       
   657             return i < limit;
       
   658         }
       
   659 
       
   660         static int step(int i, int limit) {
       
   661             return i + 1;
       
   662         }
       
   663 
       
   664         static String initString() {
       
   665             return "a";
       
   666         }
       
   667 
       
   668         static boolean predString(String s) {
       
   669             return s.length() != 1;
       
   670         }
       
   671 
       
   672         static String stepString(String s) {
       
   673             return s + "a";
       
   674         }
       
   675 
       
   676         static List<String> zipInitZip(Iterator<String> a, Iterator<String> b) {
       
   677             return new ArrayList<>();
       
   678         }
       
   679 
       
   680         static boolean zipPred(List<String> zip, Iterator<String> a, Iterator<String> b) {
       
   681             return a.hasNext() && b.hasNext();
       
   682         }
       
   683 
       
   684         static List<String> zipStep(List<String> zip, Iterator<String> a, Iterator<String> b) {
       
   685             zip.add(a.next());
       
   686             zip.add(b.next());
       
   687             return zip;
       
   688         }
       
   689 
       
   690         static final Class<While> WHILE = While.class;
       
   691 
       
   692         static final MethodType MT_zero = methodType(int.class, int.class);
       
   693         static final MethodType MT_pred = methodType(boolean.class, int.class, int.class);
       
   694         static final MethodType MT_fn = methodType(int.class, int.class, int.class);
       
   695         static final MethodType MT_initString = methodType(String.class);
       
   696         static final MethodType MT_predString = methodType(boolean.class, String.class);
       
   697         static final MethodType MT_stepString = methodType(String.class, String.class);
       
   698         static final MethodType MT_zipInitZip = methodType(List.class, Iterator.class, Iterator.class);
       
   699         static final MethodType MT_zipPred = methodType(boolean.class, List.class, Iterator.class, Iterator.class);
       
   700         static final MethodType MT_zipStep = methodType(List.class, List.class, Iterator.class, Iterator.class);
       
   701 
       
   702         static final MethodHandle MH_zero;
       
   703         static final MethodHandle MH_pred;
       
   704         static final MethodHandle MH_step;
       
   705         static final MethodHandle MH_initString;
       
   706         static final MethodHandle MH_predString;
       
   707         static final MethodHandle MH_stepString;
       
   708         static final MethodHandle MH_zipInitZip;
       
   709         static final MethodHandle MH_zipPred;
       
   710         static final MethodHandle MH_zipStep;
       
   711 
       
   712         static final MethodType MT_while = methodType(int.class, int.class);
       
   713         static final MethodType MT_string = methodType(String.class);
       
   714         static final MethodType MT_zip = methodType(List.class, Iterator.class, Iterator.class);
       
   715 
       
   716         static {
       
   717             try {
       
   718                 MH_zero = LOOKUP.findStatic(WHILE, "zero", MT_zero);
       
   719                 MH_pred = LOOKUP.findStatic(WHILE, "pred", MT_pred);
       
   720                 MH_step = LOOKUP.findStatic(WHILE, "step", MT_fn);
       
   721                 MH_initString = LOOKUP.findStatic(WHILE, "initString", MT_initString);
       
   722                 MH_predString = LOOKUP.findStatic(WHILE, "predString", MT_predString);
       
   723                 MH_stepString = LOOKUP.findStatic(WHILE, "stepString", MT_stepString);
       
   724                 MH_zipInitZip = LOOKUP.findStatic(WHILE, "zipInitZip", MT_zipInitZip);
       
   725                 MH_zipPred = LOOKUP.findStatic(WHILE, "zipPred", MT_zipPred);
       
   726                 MH_zipStep = LOOKUP.findStatic(WHILE, "zipStep", MT_zipStep);
       
   727             } catch (Exception e) {
       
   728                 throw new ExceptionInInitializerError(e);
       
   729             }
       
   730         }
       
   731 
       
   732     }
       
   733 
       
   734     static class Counted {
       
   735 
       
   736         static String start(String arg) {
       
   737             return arg;
       
   738         }
       
   739 
       
   740         static String step(int counter, String v, String arg) {
       
   741             return "na " + v;
       
   742         }
       
   743 
       
   744         static void stepUpdateArray(int counter, int[] a) {
       
   745             ++a[0];
       
   746         }
       
   747 
       
   748         static void printHello(int counter) {
       
   749             System.out.print("hello");
       
   750         }
       
   751 
       
   752         static final Class<Counted> COUNTED = Counted.class;
       
   753 
       
   754         static final MethodType MT_start = methodType(String.class, String.class);
       
   755         static final MethodType MT_step = methodType(String.class, int.class, String.class, String.class);
       
   756         static final MethodType MT_stepUpdateArray = methodType(void.class, int.class, int[].class);
       
   757         static final MethodType MT_printHello = methodType(void.class, int.class);
       
   758 
       
   759         static final MethodHandle MH_13;
       
   760         static final MethodHandle MH_m5;
       
   761         static final MethodHandle MH_8;
       
   762         static final MethodHandle MH_start;
       
   763         static final MethodHandle MH_step;
       
   764         static final MethodHandle MH_stepUpdateArray;
       
   765         static final MethodHandle MH_printHello;
       
   766 
       
   767         static final MethodType MT_counted = methodType(String.class, String.class);
       
   768         static final MethodType MT_arrayCounted = methodType(void.class, int[].class);
       
   769         static final MethodType MT_countedPrinting = methodType(void.class);
       
   770 
       
   771         static {
       
   772             try {
       
   773                 MH_13 = MethodHandles.constant(int.class, 13);
       
   774                 MH_m5 = MethodHandles.constant(int.class, -5);
       
   775                 MH_8 = MethodHandles.constant(int.class, 8);
       
   776                 MH_start = LOOKUP.findStatic(COUNTED, "start", MT_start);
       
   777                 MH_step = LOOKUP.findStatic(COUNTED, "step", MT_step);
       
   778                 MH_stepUpdateArray = LOOKUP.findStatic(COUNTED, "stepUpdateArray", MT_stepUpdateArray);
       
   779                 MH_printHello = LOOKUP.findStatic(COUNTED, "printHello", MT_printHello);
       
   780             } catch (Exception e) {
       
   781                 throw new ExceptionInInitializerError(e);
       
   782             }
       
   783         }
       
   784 
       
   785     }
       
   786 
       
   787     static class Iterate {
       
   788 
       
   789         static Iterator<Integer> sumIterator(Integer[] a) {
       
   790             return Arrays.asList(a).iterator();
       
   791         }
       
   792 
       
   793         static int sumInit(Integer[] a) {
       
   794             return 0;
       
   795         }
       
   796 
       
   797         static int sumStep(int s, int e, Integer[] a) {
       
   798             return s + e;
       
   799         }
       
   800 
       
   801         static List<String> reverseInit(List<String> l) {
       
   802             return new ArrayList<>();
       
   803         }
       
   804 
       
   805         static List<String> reverseStep(String e, List<String> r, List<String> l) {
       
   806             r.add(0, e);
       
   807             return r;
       
   808         }
       
   809 
       
   810         static int lengthInit(List<Double> l) {
       
   811             return 0;
       
   812         }
       
   813 
       
   814         static int lengthStep(Object o, int len, List<Double> l) {
       
   815             return len + 1;
       
   816         }
       
   817 
       
   818         static List<String> mapInit(List<String> l) {
       
   819             return new ArrayList<>();
       
   820         }
       
   821 
       
   822         static List<String> mapStep(String e, List<String> r, List<String> l) {
       
   823             r.add(e.toUpperCase());
       
   824             return r;
       
   825         }
       
   826 
       
   827         static void printStep(String s, List<String> l) {
       
   828             System.out.print(s);
       
   829         }
       
   830 
       
   831         static final Class<Iterate> ITERATE = Iterate.class;
       
   832 
       
   833         static final MethodType MT_sumIterator = methodType(Iterator.class, Integer[].class);
       
   834 
       
   835         static final MethodType MT_sumInit = methodType(int.class, Integer[].class);
       
   836         static final MethodType MT_reverseInit = methodType(List.class, List.class);
       
   837         static final MethodType MT_lenghInit = methodType(int.class, List.class);
       
   838         static final MethodType MT_mapInit = methodType(List.class, List.class);
       
   839 
       
   840         static final MethodType MT_sumStep = methodType(int.class, int.class, int.class, Integer[].class);
       
   841         static final MethodType MT_reverseStep = methodType(List.class, String.class, List.class, List.class);
       
   842         static final MethodType MT_lengthStep = methodType(int.class, Object.class, int.class, List.class);
       
   843         static final MethodType MT_mapStep = methodType(List.class, String.class, List.class, List.class);
       
   844         static final MethodType MT_printStep = methodType(void.class, String.class, List.class);
       
   845 
       
   846         static final MethodHandle MH_sumIterator;
       
   847         static final MethodHandle MH_sumInit;
       
   848         static final MethodHandle MH_sumStep;
       
   849         static final MethodHandle MH_printStep;
       
   850 
       
   851         static final MethodHandle MH_reverseInit;
       
   852         static final MethodHandle MH_reverseStep;
       
   853 
       
   854         static final MethodHandle MH_lengthInit;
       
   855         static final MethodHandle MH_lengthStep;
       
   856 
       
   857         static final MethodHandle MH_mapInit;
       
   858         static final MethodHandle MH_mapStep;
       
   859 
       
   860         static final MethodType MT_sum = methodType(int.class, Integer[].class);
       
   861         static final MethodType MT_reverse = methodType(List.class, List.class);
       
   862         static final MethodType MT_length = methodType(int.class, List.class);
       
   863         static final MethodType MT_map = methodType(List.class, List.class);
       
   864         static final MethodType MT_print = methodType(void.class, List.class);
       
   865 
       
   866         static {
       
   867             try {
       
   868                 MH_sumIterator = LOOKUP.findStatic(ITERATE, "sumIterator", MT_sumIterator);
       
   869                 MH_sumInit = LOOKUP.findStatic(ITERATE, "sumInit", MT_sumInit);
       
   870                 MH_sumStep = LOOKUP.findStatic(ITERATE, "sumStep", MT_sumStep);
       
   871                 MH_reverseInit = LOOKUP.findStatic(ITERATE, "reverseInit", MT_reverseInit);
       
   872                 MH_reverseStep = LOOKUP.findStatic(ITERATE, "reverseStep", MT_reverseStep);
       
   873                 MH_lengthInit = LOOKUP.findStatic(ITERATE, "lengthInit", MT_lenghInit);
       
   874                 MH_lengthStep = LOOKUP.findStatic(ITERATE, "lengthStep", MT_lengthStep);
       
   875                 MH_mapInit = LOOKUP.findStatic(ITERATE, "mapInit", MT_mapInit);
       
   876                 MH_mapStep = LOOKUP.findStatic(ITERATE, "mapStep", MT_mapStep);
       
   877                 MH_printStep = LOOKUP.findStatic(ITERATE, "printStep", MT_printStep);
       
   878             } catch (Exception e) {
       
   879                 throw new ExceptionInInitializerError(e);
       
   880             }
       
   881         }
       
   882 
       
   883     }
       
   884 
       
   885     static class TryFinally {
       
   886 
       
   887         static String greet(String whom) {
       
   888             return "Hello, " + whom;
       
   889         }
       
   890 
       
   891         static String exclaim(Throwable t, String r, String whom) {
       
   892             return r + "!";
       
   893         }
       
   894 
       
   895         static void print(String what) {
       
   896             System.out.print("Hello, " + what);
       
   897         }
       
   898 
       
   899         static void printMore(Throwable t, String what) {
       
   900             System.out.println("!");
       
   901         }
       
   902 
       
   903         static String greetMore(String first, String second) {
       
   904             return "Hello, " + first + " and " + second;
       
   905         }
       
   906 
       
   907         static String exclaimMore(Throwable t, String r, String first) {
       
   908             return r + " (but " + first + " first)!";
       
   909         }
       
   910 
       
   911         static final Class<TryFinally> TRY_FINALLY = TryFinally.class;
       
   912 
       
   913         static final MethodType MT_greet = methodType(String.class, String.class);
       
   914         static final MethodType MT_exclaim = methodType(String.class, Throwable.class, String.class, String.class);
       
   915         static final MethodType MT_print = methodType(void.class, String.class);
       
   916         static final MethodType MT_printMore = methodType(void.class, Throwable.class, String.class);
       
   917         static final MethodType MT_greetMore = methodType(String.class, String.class, String.class);
       
   918         static final MethodType MT_exclaimMore = methodType(String.class, Throwable.class, String.class, String.class);
       
   919 
       
   920         static final MethodHandle MH_greet;
       
   921         static final MethodHandle MH_exclaim;
       
   922         static final MethodHandle MH_print;
       
   923         static final MethodHandle MH_printMore;
       
   924         static final MethodHandle MH_greetMore;
       
   925         static final MethodHandle MH_exclaimMore;
       
   926 
       
   927         static final MethodType MT_hello = methodType(String.class, String.class);
       
   928         static final MethodType MT_printHello = methodType(void.class, String.class);
       
   929         static final MethodType MT_moreHello = methodType(String.class, String.class, String.class);
       
   930 
       
   931         static {
       
   932             try {
       
   933                 MH_greet = LOOKUP.findStatic(TRY_FINALLY, "greet", MT_greet);
       
   934                 MH_exclaim = LOOKUP.findStatic(TRY_FINALLY, "exclaim", MT_exclaim);
       
   935                 MH_print = LOOKUP.findStatic(TRY_FINALLY, "print", MT_print);
       
   936                 MH_printMore = LOOKUP.findStatic(TRY_FINALLY, "printMore", MT_printMore);
       
   937                 MH_greetMore = LOOKUP.findStatic(TRY_FINALLY, "greetMore", MT_greetMore);
       
   938                 MH_exclaimMore = LOOKUP.findStatic(TRY_FINALLY, "exclaimMore", MT_exclaimMore);
       
   939             } catch (Exception e) {
       
   940                 throw new ExceptionInInitializerError(e);
       
   941             }
       
   942         }
       
   943 
       
   944     }
       
   945 
       
   946     static class Fold {
       
   947 
       
   948         static int adder(int a, int b, int c) {
       
   949             return a + b + c;
       
   950         }
       
   951 
       
   952         static int adder1(int a, int b) {
       
   953             return a + b;
       
   954         }
       
   955 
       
   956         static int multer(int x, int q, int r, int s) {
       
   957             return x * q * r * s;
       
   958         }
       
   959 
       
   960         static int str(boolean b1, String s, boolean b2, int x) {
       
   961             return b1 && s.equals(String.valueOf(b2)) ? x : -x;
       
   962         }
       
   963 
       
   964         static boolean comb(String s, boolean b2) {
       
   965             return !s.equals(b2);
       
   966         }
       
   967 
       
   968         static String comb2(boolean b2, int x) {
       
   969             int ib = b2 ? 1 : 0;
       
   970             return ib == x ? "true" : "false";
       
   971         }
       
   972 
       
   973         static final Class<Fold> FOLD = Fold.class;
       
   974 
       
   975         static final MethodType MT_adder = methodType(int.class, int.class, int.class, int.class);
       
   976         static final MethodType MT_adder1 = methodType(int.class, int.class, int.class);
       
   977         static final MethodType MT_multer = methodType(int.class, int.class, int.class, int.class, int.class);
       
   978         static final MethodType MT_str = methodType(int.class, boolean.class, String.class, boolean.class, int.class);
       
   979         static final MethodType MT_comb = methodType(boolean.class, String.class, boolean.class);
       
   980         static final MethodType MT_comb2 = methodType(String.class, boolean.class, int.class);
       
   981 
       
   982         static final MethodHandle MH_adder;
       
   983         static final MethodHandle MH_adder1;
       
   984         static final MethodHandle MH_multer;
       
   985         static final MethodHandle MH_str;
       
   986         static final MethodHandle MH_comb;
       
   987         static final MethodHandle MH_comb2;
       
   988 
       
   989         static final MethodType MT_folded1 = methodType(int.class, int.class, int.class, int.class);
       
   990         static final MethodType MT_folded2 = methodType(int.class, String.class, boolean.class, int.class);
       
   991         static final MethodType MT_folded3 = methodType(int.class, boolean.class, boolean.class, int.class);
       
   992 
       
   993         static {
       
   994             try {
       
   995                 MH_adder = LOOKUP.findStatic(FOLD, "adder", MT_adder);
       
   996                 MH_adder1 = LOOKUP.findStatic(FOLD, "adder1", MT_adder1);
       
   997                 MH_multer = LOOKUP.findStatic(FOLD, "multer", MT_multer);
       
   998                 MH_str = LOOKUP.findStatic(FOLD, "str", MT_str);
       
   999                 MH_comb = LOOKUP.findStatic(FOLD, "comb", MT_comb);
       
  1000                 MH_comb2 = LOOKUP.findStatic(FOLD, "comb2", MT_comb2);
       
  1001             } catch (Exception e) {
       
  1002                 throw new ExceptionInInitializerError(e);
       
  1003             }
       
  1004         }
       
  1005     }
       
  1006 
       
  1007     static class SpreadCollect {
       
  1008 
       
  1009         static String forSpreading(String s1, int i1, int i2, int i3, String s2) {
       
  1010             return s1 + i1 + i2 + i3 + s2;
       
  1011         }
       
  1012 
       
  1013         static String forCollecting(String s1, int[] is, String s2) {
       
  1014             StringBuilder sb = new StringBuilder(s1);
       
  1015             for (int i : is) {
       
  1016                 sb.append(i);
       
  1017             }
       
  1018             return sb.append(s2).toString();
       
  1019         }
       
  1020 
       
  1021         static String forCollectingLeading(int[] is, String s) {
       
  1022             return forCollecting("", is, s);
       
  1023         }
       
  1024 
       
  1025         static final Class<SpreadCollect> SPREAD_COLLECT = SpreadCollect.class;
       
  1026 
       
  1027         static final MethodType MT_forSpreading = methodType(String.class, String.class, int.class, int.class, int.class, String.class);
       
  1028         static final MethodType MT_forCollecting = methodType(String.class, String.class, int[].class, String.class);
       
  1029         static final MethodType MT_forCollectingLeading = methodType(String.class, int[].class, String.class);
       
  1030 
       
  1031         static final MethodHandle MH_forSpreading;
       
  1032         static final MethodHandle MH_forCollecting;
       
  1033         static final MethodHandle MH_forCollectingLeading;
       
  1034 
       
  1035         static final MethodType MT_spreader = methodType(String.class, String.class, int[].class, String.class);
       
  1036         static final MethodType MT_collector0 = methodType(String.class, String.class, String.class);
       
  1037         static final MethodType MT_collector1 = methodType(String.class, String.class, int.class, String.class);
       
  1038         static final MethodType MT_collector2 = methodType(String.class, String.class, int.class, int.class, String.class);
       
  1039         static final MethodType MT_collector3 = methodType(String.class, String.class, int.class, int.class, int.class, String.class);
       
  1040         static final MethodType MT_collectorLeading1 = methodType(String.class, int.class, String.class);
       
  1041         static final MethodType MT_collectorLeading2 = methodType(String.class, int.class, int.class, String.class);
       
  1042         static final MethodType MT_collectorLeading3 = methodType(String.class, int.class, int.class, int.class, String.class);
       
  1043 
       
  1044         static final String NONE_ERROR = "zero array length in MethodHandle.asCollector";
       
  1045 
       
  1046         static {
       
  1047             try {
       
  1048                 MH_forSpreading = LOOKUP.findStatic(SPREAD_COLLECT, "forSpreading", MT_forSpreading);
       
  1049                 MH_forCollecting = LOOKUP.findStatic(SPREAD_COLLECT, "forCollecting", MT_forCollecting);
       
  1050                 MH_forCollectingLeading = LOOKUP.findStatic(SPREAD_COLLECT, "forCollectingLeading", MT_forCollectingLeading);
       
  1051             } catch (Exception e) {
       
  1052                 throw new ExceptionInInitializerError(e);
       
  1053             }
       
  1054         }
       
  1055 
       
  1056     }
       
  1057 
       
  1058     static class FindSpecial {
       
  1059 
       
  1060         interface I1 {
       
  1061             default String m() {
       
  1062                 return "I1.m";
       
  1063             }
       
  1064         }
       
  1065 
       
  1066         interface I2 {
       
  1067             default String m() {
       
  1068                 return "I2.m";
       
  1069             }
       
  1070         }
       
  1071 
       
  1072         interface I3 {
       
  1073             String q();
       
  1074         }
       
  1075 
       
  1076         static class C implements I1, I2, I3 {
       
  1077             public String m() {
       
  1078                 return I1.super.m();
       
  1079             }
       
  1080             public String q() {
       
  1081                 return "q";
       
  1082             }
       
  1083         }
       
  1084 
       
  1085         static final String ABSTRACT_ERROR = "no such method: test.java.lang.invoke.T8139885$FindSpecial$I3.q()String/invokeSpecial";
       
  1086 
       
  1087     }
       
  1088 
       
  1089     //
       
  1090     // Auxiliary methods.
       
  1091     //
       
  1092 
       
  1093     static MethodHandle[] mha(MethodHandle... mhs) {
       
  1094         return mhs;
       
  1095     }
       
  1096 
       
  1097 }