*
* switch(i) { case 1: a(); b(); break; default: c(); d(); }
*
* becomes if (i == 1) { a(); b(); } else { c(); d(); }
*/
JCaseStatement caseStatement = (JCaseStatement) body.statements.get(0);
JStatement statement = body.statements.get(1);
FindBreakContinueStatementsVisitor visitor = new FindBreakContinueStatementsVisitor();
visitor.accept(statement);
if (visitor.hasBreakContinueStatements()) {
// Cannot optimize.
return;
}
if (caseStatement.getExpr() != null) {
// Create an if statement equivalent to the single-case switch.
JBinaryOperation compareOperation = new JBinaryOperation(program,
x.getSourceInfo(), program.getTypePrimitiveBoolean(),
JBinaryOperator.EQ, x.getExpr(), caseStatement.getExpr());
JBlock block = new JBlock(program, x.getSourceInfo());
block.statements.add(statement);
JIfStatement ifStatement = new JIfStatement(program,
x.getSourceInfo(), compareOperation, block, null);
ctx.replaceMe(ifStatement);