/*
* Remove any dead statements after an abrupt change in code flow and
* promote safe statements within nested blocks to this block.
*/
for (int i = 0; i < x.getStatements().size(); i++) {
JStatement stmt = x.getStatements().get(i);
if (stmt instanceof JBlock) {
/*
* Promote a sub-block's children to the current block, unless the
* sub-block contains local declarations as children.
*/
JBlock block = (JBlock) stmt;
if (canPromoteBlock(block)) {
x.removeStmt(i);
x.addStmts(i, block.getStatements());
i--;
didChange = true;
continue;
}
}
if (stmt instanceof JExpressionStatement) {
JExpressionStatement stmtExpr = (JExpressionStatement) stmt;
if (stmtExpr.getExpr() instanceof JMultiExpression) {
// Promote a multi's expressions to the current block
x.removeStmt(i);
int start = i;
JMultiExpression multi = ((JMultiExpression) stmtExpr.getExpr());
for (JExpression expr : multi.exprs) {
x.addStmt(i++, expr.makeStatement());
}
i = start - 1;
continue;
}
}
if (stmt.unconditionalControlBreak()) {
// Abrupt change in flow, chop the remaining items from this block
for (int j = i + 1; j < x.getStatements().size();) {
x.removeStmt(j);
didChange = true;
}