本文整理汇总了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);
}
}
}
示例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;
}
示例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);
}
示例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);
}
示例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);
}