本文整理匯總了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();
}
}
示例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;
}
}
示例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!"));
}
示例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]);
}
示例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();
}
示例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());
}
示例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());
}
示例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());
}
示例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"));
}
示例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!"));
}
示例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());
}
示例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();
}
示例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();
}
示例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);
}
示例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();
}