Java Code Examples for java.lang.invoke.MethodHandles#whileLoop()

The following examples show how to use java.lang.invoke.MethodHandles#whileLoop() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public static void testWhileLoopNoIteration() throws Throwable {
    // a while loop that never executes its body because the predicate evaluates to false immediately
    MethodHandle loop = MethodHandles.whileLoop(While.MH_initString, While.MH_predString, While.MH_stepString);
    assertEquals(While.MT_string, loop.type());
    assertEquals("a", loop.invoke());
}
 
Example 2
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public static void testWhileBadInit() throws Throwable {
    boolean caught = false;
    try {
        While w = new While();
        MethodHandle loop = MethodHandles.whileLoop(MethodHandles.empty(methodType(void.class, char.class)),
                                                    While.MH_voidPred.bindTo(w),
                                                    While.MH_voidBody.bindTo(w));
    } catch (IllegalArgumentException iae) {
        assertEquals("loop initializer must match: (char)void != (int)void", iae.getMessage());
        caught = true;
    }
    assertTrue(caught);
}
 
Example 3
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public static void testWhileVoidInit() throws Throwable {
    While w = new While();
    int v = 5;
    MethodHandle loop = MethodHandles.whileLoop(While.MH_voidInit.bindTo(w), While.MH_voidPred.bindTo(w),
            While.MH_voidBody.bindTo(w));
    assertEquals(While.MT_void, loop.type());
    loop.invoke(v);
    assertEquals(v, w.i);
}
 
Example 4
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider = "nullArgs", expectedExceptions = NullPointerException.class)
public static void testWhileNullArgs(MethodHandle pred, MethodHandle body) {
    MethodHandles.whileLoop(null, pred, body);
}
 
Example 5
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider = "nullArgs", expectedExceptions = NullPointerException.class)
public static void testDoWhileNullArgs(MethodHandle body, MethodHandle pred) {
    MethodHandles.whileLoop(null, body, pred);
}