本文整理汇总了Java中java.lang.invoke.MethodHandles.loop方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandles.loop方法的具体用法?Java MethodHandles.loop怎么用?Java MethodHandles.loop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandles
的用法示例。
在下文中一共展示了MethodHandles.loop方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testLoopFac
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testLoopFac() throws Throwable {
MethodHandle[] counterClause = new MethodHandle[]{Fac.MH_zero, Fac.MH_inc};
MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin};
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
assertEquals(Fac.MT_fac, loop.type());
assertEquals(120, loop.invoke(5));
}
示例2: testLoopFacNullInit
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testLoopFacNullInit() throws Throwable {
// null initializer for counter, should initialize to 0
MethodHandle[] counterClause = new MethodHandle[]{null, Fac.MH_inc};
MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin};
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
assertEquals(Fac.MT_fac, loop.type());
assertEquals(120, loop.invoke(5));
}
示例3: testLoopNullInit
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testLoopNullInit() throws Throwable {
// null initializer for counter, should initialize to 0, one-clause loop
MethodHandle[] counterClause = new MethodHandle[]{null, Loop.MH_inc, Loop.MH_pred, Loop.MH_fin};
MethodHandle loop = MethodHandles.loop(counterClause);
assertEquals(Loop.MT_loop, loop.type());
assertEquals(10, loop.invoke(10));
}
示例4: testLoopVoid1
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testLoopVoid1() throws Throwable {
// construct a post-checked loop that only does one iteration and has a void body and void local state
MethodHandle loop = MethodHandles.loop(new MethodHandle[]{Empty.MH_f, Empty.MH_f, Empty.MH_pred, null});
assertEquals(MethodType.methodType(void.class), loop.type());
loop.invoke();
}
示例5: testLoopVoid2
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testLoopVoid2() throws Throwable {
// construct a post-checked loop that only does one iteration and has a void body and void local state,
// initialized implicitly from the step type
MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, null});
assertEquals(MethodType.methodType(void.class), loop.type());
loop.invoke();
}
示例6: testLoopVoid3
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testLoopVoid3() throws Throwable {
// construct a post-checked loop that only does one iteration and has a void body and void local state,
// and that has a void finalizer
MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, Empty.MH_f});
assertEquals(MethodType.methodType(void.class), loop.type());
loop.invoke();
}
示例7: testLoopFacWithVoidState
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testLoopFacWithVoidState() throws Throwable {
// like testLoopFac, but with additional void state that outputs a dot
MethodHandle[] counterClause = new MethodHandle[]{Fac.MH_zero, Fac.MH_inc};
MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin};
MethodHandle[] dotClause = new MethodHandle[]{null, Fac.MH_dot};
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause, dotClause);
assertEquals(Fac.MT_fac, loop.type());
assertEquals(120, loop.invoke(5));
}
示例8: testLoopVoidInt
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testLoopVoidInt() throws Throwable {
// construct a post-checked loop that only does one iteration and has a void body and void local state,
// and that returns a constant
MethodHandle loop = MethodHandles.loop(new MethodHandle[]{null, Empty.MH_f, Empty.MH_pred, Empty.MH_c});
assertEquals(MethodType.methodType(int.class), loop.type());
assertEquals(23, loop.invoke());
}
示例9: testLoopWithVirtuals
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testLoopWithVirtuals() throws Throwable {
// construct a loop (to calculate factorial) that uses a mix of static and virtual methods
MethodHandle[] counterClause = new MethodHandle[]{null, LoopWithVirtuals.permute(LoopWithVirtuals.MH_inc)};
MethodHandle[] accumulatorClause = new MethodHandle[]{
// init function must indicate the loop arguments (there is no other means to determine them)
MethodHandles.dropArguments(LoopWithVirtuals.MH_one, 0, LoopWithVirtuals.class),
LoopWithVirtuals.permute(LoopWithVirtuals.MH_mult),
LoopWithVirtuals.permute(LoopWithVirtuals.MH_pred),
LoopWithVirtuals.permute(LoopWithVirtuals.MH_fin)
};
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
assertEquals(LoopWithVirtuals.MT_loop, loop.type());
assertEquals(120, loop.invoke(new LoopWithVirtuals(), 5));
}
示例10: testLoopOmitPred
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testLoopOmitPred() throws Throwable {
// construct a loop to calculate factorial that omits a predicate
MethodHandle[] counterClause = new MethodHandle[]{null, Fac.MH_inc, null, Fac.MH_fin};
MethodHandle[] accumulatorClause = new MethodHandle[]{Fac.MH_one, Fac.MH_mult, Fac.MH_pred, Fac.MH_fin};
MethodHandle loop = MethodHandles.loop(counterClause, accumulatorClause);
assertEquals(Fac.MT_fac, loop.type());
assertEquals(120, loop.invoke(5));
}
示例11: forEach
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
public MethodHandle forEach(Consumer<?> consumer) {
clauses.add(new MethodHandle[] { null, dropArguments(ACCEPT.bindTo(consumer), 0, types) });
return MethodHandles.loop(clauses.toArray(new MethodHandle[0][]));
}
示例12: testLongSignature
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
static void testLongSignature(int loopArgs, boolean excessive, boolean run) {
int nClauses = ARG_LIMIT - loopArgs + (excessive ? 1 : 0);
System.out.print((excessive ? "(EXCESSIVE)" : "(LONG )") + " arguments: " + loopArgs + ", clauses: " + nClauses + " -> ");
// extend init to denote what arguments the loop should accept
Class<?>[] argTypes = new Class<?>[loopArgs];
Arrays.fill(argTypes, int.class);
MethodHandle init = MethodHandles.dropArguments(INIT, 0, argTypes);
// build clauses
MethodHandle[][] clauses = new MethodHandle[nClauses][];
MethodHandle[] clause = {init, STEP, PRED_T, FINI};
MethodHandle[] fclause = {init, STEP, PRED_F, FINI};
Arrays.fill(clauses, clause);
clauses[nClauses - 1] = fclause; // make the last clause terminate the loop
try {
MethodHandle loop = MethodHandles.loop(clauses);
if (excessive) {
throw new AssertionError("loop construction should have failed");
} else if (run) {
int r;
if (loopArgs == 0) {
r = (int) loop.invoke();
} else {
Object[] args = new Object[loopArgs];
Arrays.fill(args, 0);
r = (int) loop.invokeWithArguments(args);
}
System.out.println("SUCCEEDED (OK) -> " + r);
} else {
System.out.println("SUCCEEDED (OK)");
}
} catch (IllegalArgumentException iae) {
if (excessive) {
System.out.println("FAILED (OK)");
} else {
iae.printStackTrace(System.out);
throw new AssertionError("loop construction should not have failed (see above)");
}
} catch (Throwable t) {
t.printStackTrace(System.out);
throw new AssertionError("unexpected failure (see above)");
}
}