当前位置: 首页>>代码示例>>Java>>正文


Java MethodHandles.identity方法代码示例

本文整理汇总了Java中java.lang.invoke.MethodHandles.identity方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandles.identity方法的具体用法?Java MethodHandles.identity怎么用?Java MethodHandles.identity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.lang.invoke.MethodHandles的用法示例。


在下文中一共展示了MethodHandles.identity方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testInterfaceCast0

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
public void testInterfaceCast0() throws Throwable {
    if (CAN_SKIP_WORKING)  return;
    startTest("interfaceCast");
    assert( (((Object)"foo") instanceof CharSequence));
    assert(!(((Object)"foo") instanceof Iterable));
    for (MethodHandle mh : new MethodHandle[]{
        MethodHandles.identity(String.class),
        MethodHandles.identity(CharSequence.class),
        MethodHandles.identity(Iterable.class)
    }) {
        if (verbosity > 0)  System.out.println("-- mh = "+mh);
        for (Class<?> ctype : new Class<?>[]{
            Object.class, String.class, CharSequence.class,
            Number.class, Iterable.class
        }) {
            if (verbosity > 0)  System.out.println("---- ctype = "+ctype.getName());
            //                           doret  docast
            testInterfaceCast(mh, ctype, false, false);
            testInterfaceCast(mh, ctype, true,  false);
            testInterfaceCast(mh, ctype, false, true);
            testInterfaceCast(mh, ctype, true,  true);
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:MethodHandlesTest.java

示例2: negativeTestData

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@DataProvider
static Object[][] negativeTestData() {
    MethodHandle intid = MethodHandles.identity(int.class);
    MethodHandle intco = MethodHandles.constant(int.class, 0);
    MethodHandle errTarget = MethodHandles.dropArguments(intco, 0, int.class, double.class, String.class, int.class);
    MethodHandle errCleanup = MethodHandles.dropArguments(MethodHandles.constant(int.class, 0), 0, Throwable.class,
            int.class, double.class, Object.class);
    MethodHandle voidTarget = TryFinally.MH_voidTarget;
    MethodHandle voidICleanup = MethodHandles.dropArguments(TryFinally.MH_voidCleanup, 1, int.class);
    return new Object[][]{
            {intid, MethodHandles.identity(double.class),
                    "target and return types must match: double != int"},
            {intid, MethodHandles.dropArguments(intid, 0, String.class),
                    "cleanup first argument and Throwable must match: (String,int)int != class java.lang.Throwable"},
            {intid, MethodHandles.dropArguments(intid, 0, Throwable.class, double.class),
                    "cleanup second argument and target return type must match: (Throwable,double,int)int != int"},
            {errTarget, errCleanup,
                    "cleanup parameters after (Throwable,result) and target parameter list prefix must match: " +
                            errCleanup.type() + " != " + errTarget.type()},
            {voidTarget, voidICleanup,
                    "cleanup parameters after (Throwable,result) and target parameter list prefix must match: " +
                            voidICleanup.type() + " != " + voidTarget.type()}
    };
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:25,代码来源:TryFinallyTest.java

示例3: testFilterReturnValue

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
void testFilterReturnValue(int nargs, Class<?> rtype) throws Throwable {
    countTest();
    MethodHandle target = varargsList(nargs, rtype);
    MethodHandle filter;
    if (List.class.isAssignableFrom(rtype) || rtype.isAssignableFrom(List.class))
        filter = varargsList(1);  // add another layer of list-ness
    else
        filter = MethodHandles.identity(rtype);
    filter = filter.asType(MethodType.methodType(target.type().returnType(), rtype));
    Object[] argsToPass = randomArgs(nargs, Object.class);
    if (verbosity >= 3)
        System.out.println("filter "+target+" to "+rtype.getSimpleName()+" with "+filter);
    MethodHandle target2 = MethodHandles.filterReturnValue(target, filter);
    if (verbosity >= 4)
        System.out.println("filtered target: "+target2);
    // Simulate expected effect of filter on return value:
    Object unfiltered = target.invokeWithArguments(argsToPass);
    Object expected = filter.invokeWithArguments(unfiltered);
    if (verbosity >= 4)
        System.out.println("unfiltered: "+unfiltered+" : "+unfiltered.getClass().getSimpleName());
    if (verbosity >= 4)
        System.out.println("expected: "+expected+" : "+expected.getClass().getSimpleName());
    Object result = target2.invokeWithArguments(argsToPass);
    if (verbosity >= 3)
        System.out.println("result: "+result+" : "+result.getClass().getSimpleName());
    if (!expected.equals(result))
        System.out.println("*** fail at n/rt = "+nargs+"/"+rtype.getSimpleName()+": "+Arrays.asList(argsToPass)+" => "+result+" != "+expected);
    assertEquals(expected, result);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:30,代码来源:MethodHandlesTest.java

示例4: identity

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Override
public MethodHandle identity(final Class<?> type) {
    final MethodHandle mh = MethodHandles.identity(type);
    return debug(mh, "identity", type);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:6,代码来源:MethodHandleFactory.java

示例5: testTryFinally0

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
public void testTryFinally0() throws Throwable {
    if (CAN_SKIP_WORKING) return;
    startTest("tryFinally");
    String inputMessage = "returned";
    String augmentedMessage = "augmented";
    String thrownMessage = "thrown";
    String rethrownMessage = "rethrown";
    // Test these cases:
    // * target returns, cleanup passes through
    // * target returns, cleanup augments
    // * target throws, cleanup augments and returns
    // * target throws, cleanup augments and rethrows
    MethodHandle target = MethodHandles.identity(String.class);
    MethodHandle targetThrow = MethodHandles.dropArguments(
            MethodHandles.throwException(String.class, Exception.class).bindTo(new Exception(thrownMessage)), 0, String.class);
    MethodHandle cleanupPassThrough = MethodHandles.dropArguments(MethodHandles.identity(String.class), 0,
            Throwable.class, String.class);
    MethodHandle cleanupAugment = MethodHandles.dropArguments(MethodHandles.constant(String.class, augmentedMessage),
            0, Throwable.class, String.class, String.class);
    MethodHandle cleanupCatch = MethodHandles.dropArguments(MethodHandles.constant(String.class, thrownMessage), 0,
            Throwable.class, String.class, String.class);
    MethodHandle cleanupThrow = MethodHandles.dropArguments(MethodHandles.throwException(String.class, Exception.class).
            bindTo(new Exception(rethrownMessage)), 0, Throwable.class, String.class, String.class);
    testTryFinally(target, cleanupPassThrough, inputMessage, inputMessage, false);
    testTryFinally(target, cleanupAugment, inputMessage, augmentedMessage, false);
    testTryFinally(targetThrow, cleanupCatch, inputMessage, thrownMessage, true);
    testTryFinally(targetThrow, cleanupThrow, inputMessage, rethrownMessage, true);
    // Test the same cases as above for void targets and cleanups.
    MethodHandles.Lookup lookup = MethodHandles.lookup();
    Class<?> C = this.getClass();
    MethodType targetType = methodType(void.class, String[].class);
    MethodType cleanupType = methodType(void.class, Throwable.class, String[].class);
    MethodHandle vtarget = lookup.findStatic(C, "vtarget", targetType);
    MethodHandle vtargetThrow = lookup.findStatic(C, "vtargetThrow", targetType);
    MethodHandle vcleanupPassThrough = lookup.findStatic(C, "vcleanupPassThrough", cleanupType);
    MethodHandle vcleanupAugment = lookup.findStatic(C, "vcleanupAugment", cleanupType);
    MethodHandle vcleanupCatch = lookup.findStatic(C, "vcleanupCatch", cleanupType);
    MethodHandle vcleanupThrow = lookup.findStatic(C, "vcleanupThrow", cleanupType);
    testTryFinally(vtarget, vcleanupPassThrough, inputMessage, inputMessage, false);
    testTryFinally(vtarget, vcleanupAugment, inputMessage, augmentedMessage, false);
    testTryFinally(vtargetThrow, vcleanupCatch, inputMessage, thrownMessage, true);
    testTryFinally(vtargetThrow, vcleanupThrow, inputMessage, rethrownMessage, true);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:44,代码来源:MethodHandlesTest.java


注:本文中的java.lang.invoke.MethodHandles.identity方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。