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


Java MethodHandles.guardWithTest方法代码示例

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


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

示例1: compose

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
/**
 * Composes the invocation, switchpoint, and the guard into a composite method handle that knows how to fall back.
 * @param switchpointFallback the fallback method handle in case switchpoint is invalidated.
 * @param guardFallback the fallback method handle in case guard returns false.
 * @param catchFallback the fallback method in case the exception handler triggers
 * @return a composite method handle.
 */
public MethodHandle compose(final MethodHandle guardFallback, final MethodHandle switchpointFallback, final MethodHandle catchFallback) {
    final MethodHandle guarded =
            guard == null ?
                    invocation :
                    MethodHandles.guardWithTest(
                            guard,
                            invocation,
                            guardFallback);

    final MethodHandle catchGuarded =
            exception == null ?
                    guarded :
                    MH.catchException(
                            guarded,
                            exception,
                            MethodHandles.dropArguments(
                                catchFallback,
                                0,
                                exception));

    if (switchPoints == null) {
        return catchGuarded;
    }

    MethodHandle spGuarded = catchGuarded;
    for (final SwitchPoint sp : switchPoints) {
        spGuarded = sp.guardWithTest(spGuarded, switchpointFallback);
    }

    return spGuarded;
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:39,代码来源:GuardedInvocation.java

示例2: compose

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
/**
 * Composes the invocation, guard, switch points, and the exception into a
 * composite method handle that knows how to fall back when the guard fails
 * or the invocation is invalidated.
 * @param switchpointFallback the fallback method handle in case a switch
 * point is invalidated.
 * @param guardFallback the fallback method handle in case guard returns
 * false.
 * @param catchFallback the fallback method in case the exception handler
 * triggers.
 * @return a composite method handle.
 */
public MethodHandle compose(final MethodHandle guardFallback, final MethodHandle switchpointFallback, final MethodHandle catchFallback) {
    final MethodHandle guarded =
            guard == null ?
                    invocation :
                    MethodHandles.guardWithTest(
                            guard,
                            invocation,
                            guardFallback);

    final MethodHandle catchGuarded =
            exception == null ?
                    guarded :
                    MethodHandles.catchException(
                            guarded,
                            exception,
                            MethodHandles.dropArguments(
                                catchFallback,
                                0,
                                exception));

    if (switchPoints == null) {
        return catchGuarded;
    }

    MethodHandle spGuarded = catchGuarded;
    for (final SwitchPoint sp : switchPoints) {
        spGuarded = sp.guardWithTest(spGuarded, switchpointFallback);
    }

    return spGuarded;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:44,代码来源:GuardedInvocation.java

示例3: guardWithTest

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

示例4: guardWithTest

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
MethodHandle guardWithTest(final MethodHandle test) {
    return MethodHandles.guardWithTest(test, method1, method2);
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:4,代码来源:AbstractJavaLinker.java

示例5: testGuardWithTest

import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
void testGuardWithTest(int nargs, int testDrops, Class<?> argClass) throws Throwable {
    countTest();
    int nargs1 = Math.min(3, nargs);
    MethodHandle test = PRIVATE.findVirtual(Object.class, "equals", MethodType.methodType(boolean.class, Object.class));
    MethodHandle target = PRIVATE.findStatic(MethodHandlesTest.class, "targetIfEquals", MethodType.genericMethodType(nargs1));
    MethodHandle fallback = PRIVATE.findStatic(MethodHandlesTest.class, "fallbackIfNotEquals", MethodType.genericMethodType(nargs1));
    while (test.type().parameterCount() > nargs)
        // 0: test = constant(MISSING_ARG.equals(MISSING_ARG))
        // 1: test = lambda (_) MISSING_ARG.equals(_)
        test = MethodHandles.insertArguments(test, 0, MISSING_ARG);
    if (argClass != Object.class) {
        test = changeArgTypes(test, argClass);
        target = changeArgTypes(target, argClass);
        fallback = changeArgTypes(fallback, argClass);
    }
    int testArgs = nargs - testDrops;
    assert(testArgs >= 0);
    test = addTrailingArgs(test, Math.min(testArgs, nargs), argClass);
    target = addTrailingArgs(target, nargs, argClass);
    fallback = addTrailingArgs(fallback, nargs, argClass);
    Object[][] argLists = {
        { },
        { "foo" }, { MISSING_ARG },
        { "foo", "foo" }, { "foo", "bar" },
        { "foo", "foo", "baz" }, { "foo", "bar", "baz" }
    };
    for (Object[] argList : argLists) {
        Object[] argList1 = argList;
        if (argList.length != nargs) {
            if (argList.length != nargs1)  continue;
            argList1 = Arrays.copyOf(argList, nargs);
            Arrays.fill(argList1, nargs1, nargs, MISSING_ARG_2);
        }
        MethodHandle test1 = test;
        if (test1.type().parameterCount() > testArgs) {
            int pc = test1.type().parameterCount();
            test1 = MethodHandles.insertArguments(test, testArgs, Arrays.copyOfRange(argList1, testArgs, pc));
        }
        MethodHandle mh = MethodHandles.guardWithTest(test1, target, fallback);
        assertEquals(target.type(), mh.type());
        boolean equals;
        switch (nargs) {
        case 0:   equals = true; break;
        case 1:   equals = MISSING_ARG.equals(argList[0]); break;
        default:  equals = argList[0].equals(argList[1]); break;
        }
        String willCall = (equals ? "targetIfEquals" : "fallbackIfNotEquals");
        if (verbosity >= 3)
            System.out.println(logEntry(willCall, argList));
        Object result = mh.invokeWithArguments(argList1);
        assertCalled(willCall, argList);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:54,代码来源:MethodHandlesTest.java


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