Search in sources :

Example 1 with SwitchStatement

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);
    }
}
Also used : Assignment(com.google.devtools.j2objc.ast.Assignment) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement)

Example 2 with SwitchStatement

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);
    }
}
Also used : SwitchCase(com.google.devtools.j2objc.ast.SwitchCase) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement)

Example 3 with SwitchStatement

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;
}
Also used : ForStatement(com.google.devtools.j2objc.ast.ForStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) ContinueStatement(com.google.devtools.j2objc.ast.ContinueStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) TryStatement(com.google.devtools.j2objc.ast.TryStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement)

Example 4 with SwitchStatement

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;
}
Also used : LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) ContinueStatement(com.google.devtools.j2objc.ast.ContinueStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) AssertStatement(com.google.devtools.j2objc.ast.AssertStatement) SynchronizedStatement(com.google.devtools.j2objc.ast.SynchronizedStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) TryStatement(com.google.devtools.j2objc.ast.TryStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement)

Example 5 with SwitchStatement

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);
    }
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Aggregations

Statement (com.google.devtools.j2objc.ast.Statement)6 SwitchStatement (com.google.devtools.j2objc.ast.SwitchStatement)6 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)5 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)4 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)4 Expression (com.google.devtools.j2objc.ast.Expression)3 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)3 Block (com.google.devtools.j2objc.ast.Block)2 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)2 ContinueStatement (com.google.devtools.j2objc.ast.ContinueStatement)2 DoStatement (com.google.devtools.j2objc.ast.DoStatement)2 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)2 ForStatement (com.google.devtools.j2objc.ast.ForStatement)2 IfStatement (com.google.devtools.j2objc.ast.IfStatement)2 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)2 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)2 SwitchCase (com.google.devtools.j2objc.ast.SwitchCase)2 ThrowStatement (com.google.devtools.j2objc.ast.ThrowStatement)2 TryStatement (com.google.devtools.j2objc.ast.TryStatement)2 WhileStatement (com.google.devtools.j2objc.ast.WhileStatement)2