本文整理汇总了Java中java.lang.invoke.MethodHandles.iteratedLoop方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandles.iteratedLoop方法的具体用法?Java MethodHandles.iteratedLoop怎么用?Java MethodHandles.iteratedLoop使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandles
的用法示例。
在下文中一共展示了MethodHandles.iteratedLoop方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testIterateLength
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test(dataProvider = "iteratorInits")
public static void testIterateLength(MethodHandle iterator) throws Throwable {
MethodHandle body = Iterate.MH_lengthStep;
MethodHandle init = Iterate.MH_lengthInit;
MethodType expectedType = Iterate.MT_length;
int barity = body.type().parameterCount();
Class<?> iteratorSource = iterator == null ? null : iterator.type().parameterType(0);
if (iterator != null && iteratorSource != body.type().parameterType(barity-1)) {
// adjust body to accept the other type
body = body.asType(body.type().changeParameterType(barity-1, iteratorSource));
init = init.asType(init.type().changeParameterType(0, iteratorSource));
expectedType = expectedType.changeParameterType(0, iteratorSource);
}
for (;; init = snip(init)) {
System.out.println("testIterateLength.init = "+init);
MethodHandle loop = MethodHandles.iteratedLoop(iterator, init, body);
assertEquals(expectedType, loop.type());
List<Double> list = Arrays.asList(23.0, 148.0, 42.0);
assertEquals(list.size(), (int) loop.invoke(list));
if (init == null) break;
}
}
示例2: testIterateMap
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test(dataProvider = "iteratorInits")
public static void testIterateMap(MethodHandle iterator) throws Throwable {
MethodHandle body = Iterate.MH_mapStep;
MethodHandle init = Iterate.MH_mapInit;
MethodType expectedType = Iterate.MT_map;
int barity = body.type().parameterCount();
Class<?> iteratorSource = iterator == null ? null : iterator.type().parameterType(0);
if (iterator != null && iteratorSource != body.type().parameterType(barity-1)) {
// adjust body to accept the other type
body = body.asType(body.type().changeParameterType(barity-1, iteratorSource));
init = init.asType(init.type().changeParameterType(0, iteratorSource));
expectedType = expectedType.changeParameterType(0, iteratorSource);
}
for (; init != null; init = snip(init)) {
System.out.println("testIterateMap.init = "+init);
MethodHandle loop = MethodHandles.iteratedLoop(iterator, init, body);
assertEquals(expectedType, loop.type());
List<String> list = Arrays.asList("Hello", "world", "!");
List<String> upList = Arrays.asList("HELLO", "WORLD", "!");
assertEquals(upList, (List<String>) loop.invoke(list));
}
}
示例3: testIterateSum
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testIterateSum() throws Throwable {
// Integer[] a = new Integer[]{1,2,3,4,5,6}; int sum = 0; for (int e : a) { sum += e; } return sum; => 21
MethodHandle loop = MethodHandles.iteratedLoop(Iterate.MH_sumIterator, Iterate.MH_sumInit, Iterate.MH_sumStep);
assertEquals(Iterate.MT_sum, loop.type());
assertEquals(21, loop.invoke(new Integer[]{1, 2, 3, 4, 5, 6}));
}
示例4: testIterateReverse
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test(dataProvider = "iteratorInits")
public static void testIterateReverse(MethodHandle iterator) throws Throwable {
// this test uses List as its loop state type; don't try to change that
if (iterator != null)
iterator = iterator.asType(iterator.type().changeParameterType(0, List.class));
for (int i = 0; i < 4; i++) {
MethodHandle init = Iterate.MH_reverseInit, body = Iterate.MH_reverseStep;
boolean snipInit = (i & 1) != 0, snipBody = (i & 2) != 0;
if (snipInit) init = snip(init);
if (snipBody) body = snip(body);
if (!snipInit && snipBody && iterator == null) {
// Body does not determine (A...), so the default guy just picks Iterable.
// If body insisted on (List), the default guy would adjust himself.
// Init has no authority to change the (A...), so must patch init.
// All according to plan!
init = slap(snip(init), Iterable.class);
}
System.out.println("testIterateReverse i="+i+" : "+Arrays.asList(iterator, init, body));
MethodHandle loop = MethodHandles.iteratedLoop(iterator, init, body);
MethodType expectedType = Iterate.MT_reverse;
if (iterator == null && i >= 2)
expectedType = expectedType.changeParameterType(0, Iterable.class);
assertEquals(expectedType, loop.type());
List<String> list = Arrays.asList("a", "b", "c", "d", "e");
List<String> reversedList = Arrays.asList("e", "d", "c", "b", "a");
assertEquals(reversedList, (List<String>) loop.invoke(list));
}
}
示例5: testIteratePrint
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test(dataProvider = "iteratorInits")
public static void testIteratePrint(MethodHandle iterator) throws Throwable {
MethodHandle body = Iterate.MH_printStep;
MethodType expectedType = Iterate.MT_print;
int barity = body.type().parameterCount();
Class<?> iteratorSource = iterator == null ? null : iterator.type().parameterType(0);
if (iterator != null && iteratorSource != body.type().parameterType(barity-1)) {
// adjust body to accept the other type
body = body.asType(body.type().changeParameterType(barity-1, iteratorSource));
expectedType = expectedType.changeParameterType(0, iteratorSource);
}
MethodHandle loop = MethodHandles.iteratedLoop(iterator, null, body);
assertEquals(expectedType, loop.type());
loop.invoke(Arrays.asList("hello", "world"));
}
示例6: testIterateVoidIterator
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test(dataProvider = "wrongIteratorTypes")
public static void testIterateVoidIterator(Class<?> it) {
boolean caught = false;
MethodType v = methodType(it);
try {
MethodHandles.iteratedLoop(MethodHandles.empty(v), null, MethodHandles.empty(v));
} catch(IllegalArgumentException iae) {
assertEqualsFIXME("iteratedLoop first argument must have Iterator return type", iae.getMessage());
caught = true;
}
assertTrue(caught);
}
示例7: testIterateVoidInit
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test(dataProvider = "iteratorInits")
public static void testIterateVoidInit(MethodHandle iterator) throws Throwable {
// this test uses List as its loop state type; don't try to change that
if (iterator != null)
iterator = iterator.asType(iterator.type().changeParameterType(0, List.class));
MethodHandle loop = MethodHandles.iteratedLoop(iterator, Iterate.MH_voidInit, Iterate.MH_printStep);
assertEquals(Iterate.MT_print, loop.type());
loop.invoke(Arrays.asList("hello", "world"));
}
示例8: testIterateNullBody
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test(expectedExceptions = NullPointerException.class)
public static void testIterateNullBody() {
MethodHandles.iteratedLoop(MethodHandles.empty(methodType(Iterator.class, int.class)),
MethodHandles.identity(int.class), null);
}
示例9: testIterateParameters
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test(dataProvider = "iterateParameters")
public static void testIterateParameters(MethodType it, MethodType in, MethodType bo, String msg) {
boolean negative = !msg.isEmpty();
MethodHandle iterator = it == null ? null : MethodHandles.empty(it);
MethodHandle init = in == null ? null : MethodHandles.empty(in);
boolean caught = false;
MethodHandle loop = null;
try {
loop = MethodHandles.iteratedLoop(iterator, init, MethodHandles.empty(bo));
} catch (Throwable t) {
if (!negative) {
throw t;
}
assertEqualsFIXME(msg, t.getMessage());
caught = true;
}
if (negative) {
assertTrue(caught);
} else {
MethodType lt = loop.type();
if (it == null && in == null) {
assertEquals(bo.dropParameterTypes(0, 1), lt);
} else if (it == null) {
if (in.parameterCount() == 0) {
assertEquals(bo.dropParameterTypes(0, in.returnType() == void.class ? 1 : 2), lt);
} else {
assertEquals(methodType(bo.returnType(), in.parameterArray()), lt);
}
} else if (in == null) {
assertEquals(methodType(bo.returnType(), it.parameterArray()), lt);
} else if (it.parameterCount() > in.parameterCount()) {
assertEquals(methodType(bo.returnType(), it.parameterArray()), lt);
} else if (it.parameterCount() < in.parameterCount()) {
assertEquals(methodType(bo.returnType(), in.parameterArray()), lt);
} else {
// both it, in present; with equal parameter list lengths
assertEquals(it.parameterList(), lt.parameterList());
assertEquals(in.parameterList(), lt.parameterList());
assertEquals(bo.returnType(), lt.returnType());
}
}
}
示例10: testIteratorSubclass
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Test
public static void testIteratorSubclass() throws Throwable {
MethodHandle loop = MethodHandles.iteratedLoop(MethodHandles.empty(methodType(BogusIterator.class, List.class)),
null, MethodHandles.empty(methodType(void.class, String.class, List.class)));
assertEquals(methodType(void.class, List.class), loop.type());
}