8150957: j.l.i.MethodHandles.whileLoop(...) fails with IOOBE in the case init is null, step and pred have parameters
authormhaupt
Thu, 03 Mar 2016 14:29:00 +0100
changeset 36227 0149fa1848eb
parent 36226 37fb5281509b
child 36228 a16a22218e23
8150957: j.l.i.MethodHandles.whileLoop(...) fails with IOOBE in the case init is null, step and pred have parameters Reviewed-by: psandoz
jdk/test/java/lang/invoke/LoopCombinatorTest.java
--- a/jdk/test/java/lang/invoke/LoopCombinatorTest.java	Thu Mar 03 12:07:45 2016 +0000
+++ b/jdk/test/java/lang/invoke/LoopCombinatorTest.java	Thu Mar 03 14:29:00 2016 +0100
@@ -234,6 +234,16 @@
     }
 
     @Test
+    public static void testDoWhileNullInit() throws Throwable {
+        While w = new While();
+        int v = 5;
+        MethodHandle loop = MethodHandles.doWhileLoop(null, While.MH_voidBody.bindTo(w), While.MH_voidPred.bindTo(w));
+        assertEquals(While.MT_void, loop.type());
+        loop.invoke(v);
+        assertEquals(v, w.i);
+    }
+
+    @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());
@@ -244,6 +254,16 @@
     }
 
     @Test
+    public static void testWhileNullInit() throws Throwable {
+        While w = new While();
+        int v = 5;
+        MethodHandle loop = MethodHandles.whileLoop(null, While.MH_voidPred.bindTo(w), While.MH_voidBody.bindTo(w));
+        assertEquals(While.MT_void, loop.type());
+        loop.invoke(v);
+        assertEquals(v, w.i);
+    }
+
+    @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);
@@ -568,6 +588,16 @@
             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);
@@ -579,6 +609,8 @@
         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;
@@ -589,10 +621,13 @@
         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 {
@@ -605,6 +640,8 @@
                 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);
             }