當前位置: 首頁>>代碼示例>>Java>>正文


Java MethodHandles.iteratedLoop方法代碼示例

本文整理匯總了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;
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:23,代碼來源:LoopCombinatorTest.java

示例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));
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:23,代碼來源:LoopCombinatorTest.java

示例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}));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:8,代碼來源:LoopCombinatorTest.java

示例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));
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:29,代碼來源:LoopCombinatorTest.java

示例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"));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:16,代碼來源:LoopCombinatorTest.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:LoopCombinatorTest.java

示例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"));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:10,代碼來源:LoopCombinatorTest.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:6,代碼來源:LoopCombinatorTest.java

示例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());
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:43,代碼來源:LoopCombinatorTest.java

示例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());
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:7,代碼來源:LoopCombinatorTest.java


注:本文中的java.lang.invoke.MethodHandles.iteratedLoop方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。