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

The following examples show how to use java.lang.invoke.MethodHandles#countedLoop() . 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 6 votes vote down vote up
@Test
public static void testCountedLoopVoidInit() throws Throwable {
    MethodHandle fit5 = MethodHandles.constant(int.class, 5);
    for (int i = 0; i < 8; i++) {
        MethodHandle zero = MethodHandles.zero(void.class);
        MethodHandle init = fit5;
        MethodHandle body = Counted.MH_printHello;
        boolean useNull = (i & 1) != 0, addInitArg = (i & 2) != 0, addBodyArg = (i & 4) != 0;
        if (useNull)    zero = null;
        if (addInitArg) init = MethodHandles.dropArguments(init, 0, int.class);
        if (addBodyArg) body = MethodHandles.dropArguments(body, 1, int.class);
        System.out.println("testCountedLoopVoidInit i="+i+" : "+Arrays.asList(init, zero, body));
        MethodHandle loop = MethodHandles.countedLoop(init, zero, body);
        MethodType expectedType = Counted.MT_countedPrinting;
        if (addInitArg || addBodyArg)
            expectedType = expectedType.insertParameterTypes(0, int.class);
        assertEquals(expectedType, loop.type());
        if (addInitArg || addBodyArg)
            loop.invoke(99);
        else
            loop.invoke();
    }
}
 
Example 2
Source File: CountedLoopIterationCountsTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void run(int start, int end, int expectedIterations) throws Throwable {
    System.out.println("run from " + start + " to " + end);
    MethodHandle loop = MethodHandles.countedLoop(
            MethodHandles.constant(int.class, start), // iterate from given start (inclusive) ...
            MethodHandles.constant(int.class, end),   // ... to given end (exclusive)
            MH_m1,                                    // initialise loop variable to -1
            MH_step);                                 // increment loop counter by one in each iteration
    // The loop variable's value, and hence the loop result, will be "number of iterations" minus one.
    int r = (int) loop.invoke();
    if (r + 1 != expectedIterations) {
        System.out.println("expected " + expectedIterations + " iterations, but got " + r);
        failed = true;
    }
}
 
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 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.dropArguments(MethodHandles.constant(int.class, 13), 0, String.class);
    MethodHandle loop = MethodHandles.countedLoop(fit13, Counted.MH_start, Counted.MH_step);
    assertEquals(Counted.MT_counted, loop.type());
    assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!"));
}
 
Example 4
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public static void testCountedArrayLoop() throws Throwable {
    // int[] a = new int[]{0}; for (int i = 0; i < 13; ++i) { ++a[0]; } => a[0] == 13
    MethodHandle fit13 = MethodHandles.dropArguments(MethodHandles.constant(int.class, 13), 0, int[].class);
    MethodHandle loop = MethodHandles.countedLoop(fit13, null, Counted.MH_stepUpdateArray);
    assertEquals(Counted.MT_arrayCounted, loop.type());
    int[] a = new int[]{0};
    loop.invoke(a);
    assertEquals(13, a[0]);
}
 
Example 5
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public static void testCountedPrintingLoop() throws Throwable {
    MethodHandle fit5 = MethodHandles.constant(int.class, 5);
    MethodHandle loop = MethodHandles.countedLoop(fit5, null, Counted.MH_printHello);
    assertEquals(Counted.MT_countedPrinting, loop.type());
    loop.invoke();
}
 
Example 6
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public static void testCountedLoopNullBody() throws Throwable {
    MethodHandle h5 = MethodHandles.constant(int.class, 5);
    MethodHandle h13 = MethodHandles.constant(int.class, 13);
    MethodHandle loop = MethodHandles.countedLoop(h5, h13, null);
    assertEquals(methodType(int.class), loop.type());
    assertEquals(13, loop.invoke());
}
 
Example 7
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "countedLoopBodyParameters")
public static void testCountedLoopBodyParameters(MethodType countType, MethodType initType, MethodType bodyType) throws Throwable {
    MethodHandle loop = MethodHandles.countedLoop(
            MethodHandles.empty(countType),
            initType == null ? null : MethodHandles.empty(initType),
            MethodHandles.empty(bodyType));
    // The rule:  If body takes the minimum number of parameters, then take what countType offers.
    // The initType has to just roll with whatever the other two agree on.
    int innerParams = (bodyType.returnType() == void.class ? 1 : 2);
    MethodType expectType = bodyType.dropParameterTypes(0, innerParams);
    if (expectType.parameterCount() == 0)
        expectType = expectType.insertParameterTypes(0, countType.parameterList());
    assertEquals(expectType, loop.type());
}
 
Example 8
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public static void testCountedLoopStateInitializedToNull() throws Throwable {
    MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, 5),
            MethodHandles.empty(methodType(String.class)), Counted.MH_stateBody);
    assertEquals(Counted.MT_bodyDeterminesState, loop.type());
    assertEquals("sssssnull01234", loop.invoke());
}
 
Example 9
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public static void testCountedLoopArgsDefinedByIterations() throws Throwable {
    MethodHandle iterations =
            MethodHandles.dropArguments(MethodHandles.constant(int.class, 3), 0, String.class);
    MethodHandle loop = MethodHandles.countedLoop(iterations,
            MethodHandles.empty(iterations.type().changeReturnType(String.class)), Counted.MH_append);
    assertEquals(Counted.MT_iterationsDefineArgs, loop.type());
    assertEquals("hello012", loop.invoke("hello"));
}
 
Example 10
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public static void testCountedRangeLoop() throws Throwable {
    // String s = "Lambdaman!"; for (int i = -5; i < 8; ++i) { s = "na " + s; } return s; => a well known theme
    MethodHandle fitm5 = MethodHandles.dropArguments(Counted.MH_m5, 0, String.class);
    MethodHandle fit8 = MethodHandles.dropArguments(Counted.MH_8, 0, String.class);
    MethodHandle loop = MethodHandles.countedLoop(fitm5, fit8, Counted.MH_start, Counted.MH_step);
    assertEquals(Counted.MT_counted, loop.type());
    assertEquals("na na na na na na na na na na na na na Lambdaman!", loop.invoke("Lambdaman!"));
}
 
Example 11
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public static void testCountedLoopCounterInit() throws Throwable {
    // int x = 0; for (int i = 0; i < 5; ++i) { x += i; } return x; => 10
    // (only if counter's first value in body is 0)
    MethodHandle iter = MethodHandles.constant(int.class, 5);
    MethodHandle init = MethodHandles.constant(int.class, 0);
    MethodHandle body = Counted.MH_addCounter;
    MethodHandle loop = MethodHandles.countedLoop(iter, init, body);
    assertEquals(Counted.MT_counterInit, loop.type());
    assertEquals(10, loop.invoke());
}
 
Example 12
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public static void testCountedLoopEmpty() throws Throwable {
    // for (int i = 0; i < 5; ++i) { /* empty */ }
    MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, 5), null,
            MethodHandles.empty(methodType(void.class, int.class)));
    assertEquals(methodType(void.class), loop.type());
    loop.invoke();
}
 
Example 13
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public static void testCountedRangeLoopEmpty() throws Throwable {
    // for (int i = -5; i < 5; ++i) { /* empty */ }
    MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, -5),
            MethodHandles.constant(int.class, 5), null, MethodHandles.empty(methodType(void.class, int.class)));
    assertEquals(methodType(void.class), loop.type());
    loop.invoke();
}
 
Example 14
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test(dataProvider = "countedLoopNegativeData")
public static void testCountedLoopNegative(MethodHandle start, MethodHandle end, MethodHandle init,
                                           MethodHandle body, String msg) {
    if (true)  return;  //%%%FIXME%%%%
    boolean caught = false;
    try {
        MethodHandles.countedLoop(start, end, init, body);
    } catch (IllegalArgumentException iae) {
        assertEquals(msg, iae.getMessage());
        caught = true;
    }
    assertTrue(caught);
}
 
Example 15
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public static void testCountedLoopNullIterations() throws Throwable {
    MethodHandle loop = MethodHandles.countedLoop(null, null, null);
    assertEquals(methodType(void.class), loop.type());
    loop.invoke();
}
 
Example 16
Source File: LoopCombinatorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class)
public static void testCountedLoopNullInitAndBody() throws Throwable {
    MethodHandle loop = MethodHandles.countedLoop(MethodHandles.constant(int.class, 5), null, null);
    assertEquals(methodType(void.class), loop.type());
    loop.invoke();
}