Java Code Examples for java.lang.invoke.MethodHandles#iteratedLoop()
The following examples show how to use
java.lang.invoke.MethodHandles#iteratedLoop() .
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 |
@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; } }
Example 2
Source File: LoopCombinatorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
@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)); } }
Example 3
Source File: LoopCombinatorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@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})); }
Example 4
Source File: LoopCombinatorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@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)); } }
Example 5
Source File: LoopCombinatorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@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")); }
Example 6
Source File: LoopCombinatorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@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); }
Example 7
Source File: LoopCombinatorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 5 votes |
@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")); }
Example 8
Source File: LoopCombinatorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
@Test(expectedExceptions = NullPointerException.class) public static void testIterateNullBody() { MethodHandles.iteratedLoop(MethodHandles.empty(methodType(Iterator.class, int.class)), MethodHandles.identity(int.class), null); }
Example 9
Source File: LoopCombinatorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
@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()); } } }
Example 10
Source File: LoopCombinatorTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
@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()); }