< prev index next >

test/java/lang/invoke/LoopCombinatorTest.java

Print this page
rev 13800 : 8150957: j.l.i.MethodHandles.whileLoop(...) fails with IOOBE in the case init is null, step and pred have parameters

@@ -232,20 +232,36 @@
         assertEquals(While.MT_while, loop.type());
         assertEquals(23, loop.invoke(23));
     }
 
     @Test
+    public static void testDoWhileNullInit() throws Throwable {
+        While w = new While();
+        MethodHandle loop = MethodHandles.doWhileLoop(null, While.MH_voidBody.bindTo(w), While.MH_voidPred.bindTo(w));
+        assertEquals(While.MT_void, loop.type());
+        loop.invoke(5);
+    }
+
+    @Test
     public static void testWhileZip() throws Throwable {
         MethodHandle loop = MethodHandles.doWhileLoop(While.MH_zipInitZip, While.MH_zipStep, While.MH_zipPred);
         assertEquals(While.MT_zip, loop.type());
         List<String> a = Arrays.asList("a", "b", "c", "d");
         List<String> b = Arrays.asList("e", "f", "g", "h");
         List<String> zipped = Arrays.asList("a", "e", "b", "f", "c", "g", "d", "h");
         assertEquals(zipped, (List<String>) loop.invoke(a.iterator(), b.iterator()));
     }
 
     @Test
+    public static void testWhileNullInit() throws Throwable {
+        While w = new While();
+        MethodHandle loop = MethodHandles.whileLoop(null, While.MH_voidPred.bindTo(w), While.MH_voidBody.bindTo(w));
+        assertEquals(While.MT_void, loop.type());
+        loop.invoke(5);
+    }
+
+    @Test
     public static void testCountedLoop() throws Throwable {
         // String s = "Lambdaman!"; for (int i = 0; i < 13; ++i) { s = "na " + s; } return s; => a variation on a well known theme
         MethodHandle fit13 = MethodHandles.constant(int.class, 13);
         MethodHandle loop = MethodHandles.countedLoop(fit13, Counted.MH_start, Counted.MH_step);
         assertEquals(Counted.MT_counted, loop.type());

@@ -566,10 +582,20 @@
             zip.add(a.next());
             zip.add(b.next());
             return zip;
         }
 
+        private int i = 0;
+
+        void voidBody(int k) {
+            ++i;
+        }
+
+        boolean voidPred(int k) {
+            return i < k;
+        }
+
         static final Class<While> WHILE = While.class;
 
         static final MethodType MT_zero = methodType(int.class, int.class);
         static final MethodType MT_pred = methodType(boolean.class, int.class, int.class);
         static final MethodType MT_fn = methodType(int.class, int.class, int.class);

@@ -577,24 +603,29 @@
         static final MethodType MT_predString = methodType(boolean.class, String.class);
         static final MethodType MT_stepString = methodType(String.class, String.class);
         static final MethodType MT_zipInitZip = methodType(List.class, Iterator.class, Iterator.class);
         static final MethodType MT_zipPred = methodType(boolean.class, List.class, Iterator.class, Iterator.class);
         static final MethodType MT_zipStep = methodType(List.class, List.class, Iterator.class, Iterator.class);
+        static final MethodType MT_voidBody = methodType(void.class, int.class);
+        static final MethodType MT_voidPred = methodType(boolean.class, int.class);
 
         static final MethodHandle MH_zero;
         static final MethodHandle MH_pred;
         static final MethodHandle MH_step;
         static final MethodHandle MH_initString;
         static final MethodHandle MH_predString;
         static final MethodHandle MH_stepString;
         static final MethodHandle MH_zipInitZip;
         static final MethodHandle MH_zipPred;
         static final MethodHandle MH_zipStep;
+        static final MethodHandle MH_voidBody;
+        static final MethodHandle MH_voidPred;
 
         static final MethodType MT_while = methodType(int.class, int.class);
         static final MethodType MT_string = methodType(String.class);
         static final MethodType MT_zip = methodType(List.class, Iterator.class, Iterator.class);
+        static final MethodType MT_void = methodType(void.class, int.class);
 
         static {
             try {
                 MH_zero = LOOKUP.findStatic(WHILE, "zero", MT_zero);
                 MH_pred = LOOKUP.findStatic(WHILE, "pred", MT_pred);

@@ -603,10 +634,12 @@
                 MH_predString = LOOKUP.findStatic(WHILE, "predString", MT_predString);
                 MH_stepString = LOOKUP.findStatic(WHILE, "stepString", MT_stepString);
                 MH_zipInitZip = LOOKUP.findStatic(WHILE, "zipInitZip", MT_zipInitZip);
                 MH_zipPred = LOOKUP.findStatic(WHILE, "zipPred", MT_zipPred);
                 MH_zipStep = LOOKUP.findStatic(WHILE, "zipStep", MT_zipStep);
+                MH_voidBody = LOOKUP.findVirtual(WHILE, "voidBody", MT_voidBody);
+                MH_voidPred = LOOKUP.findVirtual(WHILE, "voidPred", MT_voidPred);
             } catch (Exception e) {
                 throw new ExceptionInInitializerError(e);
             }
         }
 
< prev index next >