本文整理汇总了Java中java.lang.invoke.MethodHandles.throwException方法的典型用法代码示例。如果您正苦于以下问题:Java MethodHandles.throwException方法的具体用法?Java MethodHandles.throwException怎么用?Java MethodHandles.throwException使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.lang.invoke.MethodHandles
的用法示例。
在下文中一共展示了MethodHandles.throwException方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testThrowException
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
void testThrowException(Class<?> returnType, Throwable thrown) throws Throwable {
countTest();
Class<? extends Throwable> exType = thrown.getClass();
MethodHandle target = MethodHandles.throwException(returnType, exType);
//System.out.println("throwing with "+target+" : "+thrown);
MethodType expectedType = MethodType.methodType(returnType, exType);
assertEquals(expectedType, target.type());
target = target.asType(target.type().generic());
Throwable caught = null;
try {
Object res = target.invokeExact((Object) thrown);
fail("got "+res+" instead of throwing "+thrown);
} catch (Throwable ex) {
if (ex != thrown) {
if (ex instanceof Error) throw (Error)ex;
if (ex instanceof RuntimeException) throw (RuntimeException)ex;
}
caught = ex;
}
assertSame(thrown, caught);
}
示例2: throwException
import java.lang.invoke.MethodHandles; //导入方法依赖的package包/类
@Override
public MethodHandle throwException(final Class<?> returnType, final Class<? extends Throwable> exType) {
final MethodHandle mh = MethodHandles.throwException(returnType, exType);
return debug(mh, "throwException", returnType, exType);
}