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


Java MethodHandles.catchException方法代碼示例

本文整理匯總了Java中java.lang.invoke.MethodHandles.catchException方法的典型用法代碼示例。如果您正苦於以下問題:Java MethodHandles.catchException方法的具體用法?Java MethodHandles.catchException怎麽用?Java MethodHandles.catchException使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.lang.invoke.MethodHandles的用法示例。


在下文中一共展示了MethodHandles.catchException方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: test

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
public static void test() throws Throwable {
    List<Class<?>> ptypes = new LinkedList<>();
    ptypes.add(Object[].class);

    // We use MAX_MH_ARITY - 1 here to account for the Object[] argument.
    for (int i = 1; i < MAX_MH_ARITY - 1; i++) {
        ptypes.add(0, Object.class);

        MethodHandle targetWithArgs = target.asType(
                MethodType.methodType(Object.class, ptypes));
        MethodHandle handlerWithArgs = handler.asType(
                MethodType.methodType(Object.class, ptypes));
        handlerWithArgs = MethodHandles.dropArguments(
                handlerWithArgs, 0, MyException.class);

        MethodHandle gwc1 = MethodHandles.catchException(
                targetWithArgs, MyException.class, handlerWithArgs);

        // The next line throws an IllegalArgumentException if there is a bug.
        MethodHandle gwc2 = MethodHandles.catchException(
                gwc1, MyException.class, handlerWithArgs);

        // This is only to verify that the method handles can actually be invoked and do the right thing.
        firstArg = new Object();
        Object o = gwc2.asSpreader(Object[].class, ptypes.size() - 1)
                       .invoke(firstArg, new Object[i]);
        if (o != firstArg) {
            throw new AssertionError("return value different than expected: "
                    + o + " != " + firstArg);
        }
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:33,代碼來源:TestCatchExceptionWithVarargs.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: catchException

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
@Override
public MethodHandle catchException(final MethodHandle target, final Class<? extends Throwable> exType, final MethodHandle handler) {
    final MethodHandle mh = MethodHandles.catchException(target, exType, handler);
    return debug(mh, "catchException", exType);
}
 
開發者ID:SunburstApps,項目名稱:OpenJSharp,代碼行數:6,代碼來源:MethodHandleFactory.java

示例4: runTest

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
private void runTest() {
    if (Helper.IS_VERBOSE) {
        System.out.printf("CatchException(%s, isVararg=%b argsCount=%d "
                + "dropped=%d)%n",
                testCase, thrower.isVarargsCollector(),
                argsCount, dropped);
    }

    Helper.clear();

    Object[] args = Helper.randomArgs(
            argsCount, thrower.type().parameterArray());
    Object arg0 = Helper.MISSING_ARG;
    Object arg1 = testCase.thrown;
    if (argsCount > 0) {
        arg0 = args[0];
    }
    if (argsCount > 1) {
        args[1] = arg1;
    }
    Asserts.assertEQ(nargs, thrower.type().parameterCount());
    if (argsCount < nargs) {
        Object[] appendArgs = {arg0, arg1};
        appendArgs = Arrays.copyOfRange(appendArgs, argsCount, nargs);
        thrower = MethodHandles.insertArguments(
                thrower, argsCount, appendArgs);
    }
    Asserts.assertEQ(argsCount, thrower.type().parameterCount());

    MethodHandle target = MethodHandles.catchException(
            testCase.filter(thrower), testCase.throwableClass,
            testCase.filter(catcher));

    Asserts.assertEQ(thrower.type(), target.type());
    Asserts.assertEQ(argsCount, target.type().parameterCount());

    Object returned;
    try {
        returned = target.invokeWithArguments(args);
    } catch (Throwable ex) {
        if (CodeCacheOverflowProcessor.isThrowableCausedByVME(ex)) {
            // This error will be treated by CodeCacheOverflowProcessor
            // to prevent the test from failing because of code cache overflow.
            throw new Error(ex);
        }
        testCase.assertCatch(ex);
        returned = ex;
    }

    testCase.assertReturn(returned, arg0, arg1, dropped, args);
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:52,代碼來源:CatchExceptionTest.java

示例5: runTest

import java.lang.invoke.MethodHandles; //導入方法依賴的package包/類
private void runTest() {
    if (Helper.IS_VERBOSE) {
        System.out.printf("CatchException(%s, isVararg=%b argsCount=%d " +
                        "dropped=%d)%n",
                testCase, thrower.isVarargsCollector(), argsCount, dropped);
    }

    Helper.clear();

    Object[] args = Helper.randomArgs(
            argsCount, thrower.type().parameterArray());
    Object arg0 = Helper.MISSING_ARG;
    Object arg1 = testCase.thrown;
    if (argsCount > 0) {
        arg0 = args[0];
    }
    if (argsCount > 1) {
        args[1] = arg1;
    }
    Asserts.assertEQ(nargs, thrower.type().parameterCount());
    if (argsCount < nargs) {
        Object[] appendArgs = {arg0, arg1};
        appendArgs = Arrays.copyOfRange(appendArgs, argsCount, nargs);
        thrower = MethodHandles.insertArguments(
                thrower, argsCount, appendArgs);
    }
    Asserts.assertEQ(argsCount, thrower.type().parameterCount());

    MethodHandle target = MethodHandles.catchException(
            testCase.filter(thrower), testCase.throwableClass,
            testCase.filter(catcher));

    Asserts.assertEQ(thrower.type(), target.type());
    Asserts.assertEQ(argsCount, target.type().parameterCount());

    Object returned;
    try {
        returned = target.invokeWithArguments(args);
    } catch (Throwable ex) {
        if (CodeCacheOverflowProcessor.isThrowableCausedByVME(ex)) {
            // This error will be treated by CodeCacheOverflowProcessor
            // to prevent the test from failing because of code cache overflow.
            throw new Error(ex);
        }
        testCase.assertCatch(ex);
        returned = ex;
    }

    testCase.assertReturn(returned, arg0, arg1, dropped, args);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:51,代碼來源:CatchExceptionTest.java


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