当前位置: 首页>>代码示例>>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;未经允许,请勿转载。