当前位置: 首页>>代码示例>>Java>>正文


Java MethodHandles.countedLoop方法代码示例

本文整理汇总了Java中java.lang.invoke.MethodHandles.countedLoop方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandles.countedLoop方法的具体用法?Java MethodHandles.countedLoop怎么用?Java MethodHandles.countedLoop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.lang.invoke.MethodHandles的用法示例。


在下文中一共展示了MethodHandles.countedLoop方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testCountedLoopVoidInit

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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();
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:LoopCombinatorTest.java

示例2: run

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
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;
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:CountedLoopIterationCountsTest.java

示例3: testCountedLoop

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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!"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:LoopCombinatorTest.java

示例4: testCountedArrayLoop

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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]);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:11,代码来源:LoopCombinatorTest.java

示例5: testCountedPrintingLoop

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:LoopCombinatorTest.java

示例6: testCountedLoopNullBody

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:LoopCombinatorTest.java

示例7: testCountedLoopBodyParameters

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:15,代码来源:LoopCombinatorTest.java

示例8: testCountedLoopStateInitializedToNull

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:8,代码来源:LoopCombinatorTest.java

示例9: testCountedLoopArgsDefinedByIterations

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:LoopCombinatorTest.java

示例10: testCountedRangeLoop

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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!"));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:10,代码来源:LoopCombinatorTest.java

示例11: testCountedLoopCounterInit

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:12,代码来源:LoopCombinatorTest.java

示例12: testCountedLoopEmpty

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:LoopCombinatorTest.java

示例13: testCountedRangeLoopEmpty

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:LoopCombinatorTest.java

示例14: testCountedLoopNegative

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:14,代码来源:LoopCombinatorTest.java

示例15: testCountedLoopNullIterations

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:7,代码来源:LoopCombinatorTest.java


注:本文中的java.lang.invoke.MethodHandles.countedLoop方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。