return;
}
SourceInfo catchInfo = x.getCatchBlocks().get(0).getSourceInfo();
JLocal exVar = popTempLocal();
JBlock newCatchBlock = new JBlock(catchInfo);
{
// $e = Exceptions.caught($e)
JMethod caughtMethod = program.getIndexedMethod("Exceptions.caught");
JMethodCall call = new JMethodCall(catchInfo, null, caughtMethod);
call.addArg(new JLocalRef(catchInfo, exVar));
newCatchBlock.addStmt(program.createAssignmentStmt(catchInfo,
new JLocalRef(catchInfo, exVar), call));
}
/*
* Build up a series of if, else if statements to test the type of the
* exception object against the type of the user's catch block.
*
* Go backwards so we can nest the else statements in the correct order!
*/
// rethrow the current exception if no one caught it
JStatement cur = new JThrowStatement(catchInfo, new JLocalRef(catchInfo,
exVar));
for (int i = x.getCatchBlocks().size() - 1; i >= 0; --i) {
JBlock block = x.getCatchBlocks().get(i);
JLocalRef arg = x.getCatchArgs().get(i);
catchInfo = block.getSourceInfo();
JReferenceType argType = (JReferenceType) arg.getType();
// if ($e instanceof ArgType) { var userVar = $e; <user code> }
JExpression ifTest = new JInstanceOf(catchInfo, argType, new JLocalRef(
catchInfo, exVar));
JDeclarationStatement declaration = new JDeclarationStatement(
catchInfo, arg, new JLocalRef(catchInfo, exVar));
if (!block.getStatements().isEmpty()) {
// Only bother adding the assignment if the block is non-empty
block.addStmt(0, declaration);
}
// nest the previous as an else for me
cur = new JIfStatement(catchInfo, ifTest, block, cur);
}