InstructionHandle tryEnd = emitReturn(il, methodReturnType);
//
// catch...
//
InstructionHandle rethrowLocation = il.append(new ATHROW());
Class[] exceptions = method.getExceptionTypes();
boolean handle_throwable_exception = true;
boolean handle_runtime_exception = true;
if (exceptions != null) {
for (int i = 0; i < exceptions.length; i++) {
Class ex = exceptions[i];
if (ex == java.lang.Throwable.class)
handle_throwable_exception = false;
if (ex == java.lang.RuntimeException.class
|| ex == java.lang.Exception.class)
handle_runtime_exception = false;
mg.addExceptionHandler(tryStart, tryEnd, rethrowLocation,
(ObjectType) translate(ex));
}
}
// A RuntimeException should not cause an
// UndeclaredThrowableException, so we catch and re-throw it
// that before throwable.
if (handle_throwable_exception && handle_runtime_exception) {
mg.addExceptionHandler(tryStart, tryEnd, rethrowLocation,
new ObjectType("java.lang.RuntimeException"));
}
// If anything else is thrown, it is wrapped in an
// UndeclaredThrowable
if (handle_throwable_exception) {
InstructionHandle handlerStart = il.append(new ASTORE(1));
il
.append(new NEW(
cp
.addClass("java.lang.reflect.UndeclaredThrowableException")));
il.append(InstructionConstants.DUP);
il.append(new ALOAD(1));
il.append(new INVOKESPECIAL(cp.addMethodref(
"java.lang.reflect.UndeclaredThrowableException", "<init>",
"(Ljava/lang/Throwable;)V")));
il.append(new ATHROW());
mg.addExceptionHandler(tryStart, tryEnd, handlerStart,
new ObjectType("java.lang.Throwable"));
}