本文整理匯總了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);
}