use of com.google.devtools.j2objc.ast.SwitchStatement 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.SwitchStatement 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();
Statement lastStmt = stmts.get(stmts.size() - 1);
if (!stmts.isEmpty() && 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.SwitchStatement 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.SwitchStatement in project j2objc by google.
the class StatementGenerator method visit.
@Override
public boolean visit(SwitchStatement node) {
Expression expr = node.getExpression();
buffer.append("switch (");
expr.accept(this);
buffer.append(") ");
buffer.append("{\n");
buffer.indent();
for (Statement stmt : node.getStatements()) {
buffer.printIndent();
stmt.accept(this);
}
buffer.unindent();
buffer.printIndent();
buffer.append("}\n");
return false;
}
use of com.google.devtools.j2objc.ast.SwitchStatement in project j2objc by google.
the class SwitchRewriterTest method testVariableDeclarationsInSwitchStatement2.
public void testVariableDeclarationsInSwitchStatement2() throws IOException {
CompilationUnit unit = translateType("A", "public class A { public void doSomething(int i) { switch (i) { " + "case 1: int j = i * 2; log(j); break; " + "case 2: log(i); break; " + "case 3: log(i); int k = i, l = 42; break; }}" + "private void log(int i) {}}");
TypeDeclaration testType = (TypeDeclaration) unit.getTypes().get(0);
// First MethodDeclaration is the implicit default constructor.
MethodDeclaration method = TreeUtil.getMethodDeclarationsList(testType).get(1);
List<Statement> stmts = method.getBody().getStatements();
assertEquals(1, stmts.size());
Block block = (Block) stmts.get(0);
stmts = block.getStatements();
if (options.isJDT()) {
assertEquals(3, stmts.size());
assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(2) instanceof SwitchStatement);
} else {
assertEquals(4, stmts.size());
assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(2) instanceof VariableDeclarationStatement);
assertTrue(stmts.get(3) instanceof SwitchStatement);
}
}
Aggregations