use of com.google.devtools.j2objc.ast.EmptyStatement in project j2objc by google.
the class LabelRewriter method endVisit.
@Override
public void endVisit(LabeledStatement node) {
Statement loopBody = getLoopBody(node.getBody());
final String labelIdentifier = node.getLabel().getIdentifier();
final boolean[] hasContinue = new boolean[1];
final boolean[] hasBreak = new boolean[1];
node.accept(new TreeVisitor() {
@Override
public void endVisit(ContinueStatement node) {
if (node.getLabel() != null && node.getLabel().getIdentifier().equals(labelIdentifier)) {
hasContinue[0] = true;
node.setLabel(new SimpleName("continue_" + labelIdentifier));
}
}
@Override
public void endVisit(BreakStatement node) {
if (node.getLabel() != null && node.getLabel().getIdentifier().equals(labelIdentifier)) {
hasBreak[0] = true;
node.setLabel(new SimpleName("break_" + labelIdentifier));
}
}
});
if (hasContinue[0]) {
assert loopBody != null : "Continue statements must be inside a loop.";
LabeledStatement newLabelStmt = new LabeledStatement("continue_" + labelIdentifier);
newLabelStmt.setBody(new EmptyStatement());
// Put the loop body into an inner block so the continue label is outside
// the scope of any variable initializations.
Block newBlock = new Block();
loopBody.replaceWith(newBlock);
newBlock.addStatement(loopBody);
newBlock.addStatement(newLabelStmt);
}
if (hasBreak[0]) {
LabeledStatement newLabelStmt = new LabeledStatement("break_" + labelIdentifier);
newLabelStmt.setBody(new EmptyStatement());
TreeUtil.insertAfter(node, newLabelStmt);
}
if (hasContinue[0] || hasBreak[0]) {
// Replace this node with its statement, thus deleting the label.
node.replaceWith(TreeUtil.remove(node.getBody()));
}
}
Aggregations