use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.
the class NilCheckResolver method visit.
@Override
public boolean visit(SwitchStatement node) {
node.getExpression().accept(this);
pushLoopOrSwitchScope(null);
// Merge the case where no value is matched.
scope.next.mergeVars(EMPTY_VARS);
for (Statement stmt : node.getStatements()) {
stmt.accept(this);
}
popAndMerge();
return false;
}
use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.
the class NilCheckResolver method visitType.
private boolean visitType(AbstractTypeDeclaration node) {
for (BodyDeclaration decl : node.getBodyDeclarations()) {
decl.accept(this);
}
pushFirstScope();
for (Statement stmt : node.getClassInitStatements()) {
stmt.accept(this);
}
popLastScope();
return false;
}
use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.
the class SwitchRewriter method fixVariableDeclarations.
/**
* Moves all variable declarations above the first case statement.
*/
private void fixVariableDeclarations(SwitchStatement node) {
List<Statement> statements = node.getStatements();
Block block = new Block();
List<Statement> blockStmts = block.getStatements();
for (int i = 0; i < statements.size(); i++) {
Statement stmt = statements.get(i);
if (stmt instanceof VariableDeclarationStatement) {
VariableDeclarationStatement declStmt = (VariableDeclarationStatement) stmt;
statements.remove(i--);
List<VariableDeclarationFragment> fragments = declStmt.getFragments();
for (VariableDeclarationFragment decl : fragments) {
Expression initializer = decl.getInitializer();
if (initializer != null) {
Assignment assignment = new Assignment(new SimpleName(decl.getVariableElement()), TreeUtil.remove(initializer));
statements.add(++i, new ExpressionStatement(assignment));
}
}
blockStmts.add(declStmt);
}
}
if (blockStmts.size() > 0) {
// There is at least one variable declaration, so copy this switch
// statement into the new block and replace it in the parent list.
node.replaceWith(block);
blockStmts.add(node);
}
}
use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.
the class SwitchRewriter method endVisit.
@Override
public void endVisit(SwitchStatement node) {
fixVariableDeclarations(node);
fixStringValue(node);
fixEnumValue(node);
List<Statement> stmts = node.getStatements();
if (!stmts.isEmpty()) {
Statement lastStmt = stmts.get(stmts.size() - 1);
if (lastStmt instanceof SwitchCase) {
// Last switch case doesn't have an associated statement, so add an empty one
// with the same line number as the switch case to keep line numbers synced.
EmptyStatement emptyStmt = new EmptyStatement();
emptyStmt.setLineNumber(lastStmt.getLineNumber());
stmts.add(emptyStmt);
}
}
}
use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.
the class RewriterTest method testLabeledContinue.
public void testLabeledContinue() throws IOException {
List<Statement> stmts = translateStatements("int i = 0; outer: for (; i < 10; i++) { " + "for (int j = 0; j < 10; j++) { continue outer; }}");
assertEquals(2, stmts.size());
Statement s = stmts.get(1);
// not LabeledStatement
assertTrue(s instanceof ForStatement);
ForStatement fs = (ForStatement) s;
Statement forStmt = fs.getBody();
assertTrue(forStmt instanceof Block);
stmts = ((Block) forStmt).getStatements();
assertEquals(2, stmts.size());
Statement lastStmt = stmts.get(1);
assertTrue(lastStmt instanceof LabeledStatement);
assertTrue(((LabeledStatement) lastStmt).getBody() instanceof EmptyStatement);
}
Aggregations