Search in sources :

Example 1 with Statement

use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.

the class TreeConverter method needsImplicitSuperCall.

private static boolean needsImplicitSuperCall(MethodDeclaration node) {
    ExecutableElement method = node.getExecutableElement();
    if (!ElementUtil.isConstructor(method)) {
        return false;
    }
    TypeMirror superType = ElementUtil.getDeclaringClass(method).getSuperclass();
    if (TypeUtil.isNone(superType)) {
        // java.lang.Object supertype is null.
        return false;
    }
    List<Statement> stmts = node.getBody().getStatements();
    if (stmts.isEmpty()) {
        return true;
    }
    Statement firstStmt = stmts.get(0);
    return !(firstStmt instanceof SuperConstructorInvocation || firstStmt instanceof ConstructorInvocation);
}
Also used : ConstructorInvocation(com.google.devtools.j2objc.ast.ConstructorInvocation) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation) TypeMirror(javax.lang.model.type.TypeMirror) 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) AssertStatement(com.google.devtools.j2objc.ast.AssertStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) SynchronizedStatement(com.google.devtools.j2objc.ast.SynchronizedStatement) TryStatement(com.google.devtools.j2objc.ast.TryStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) TypeDeclarationStatement(com.google.devtools.j2objc.ast.TypeDeclarationStatement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) ExecutableElement(javax.lang.model.element.ExecutableElement) SuperConstructorInvocation(com.google.devtools.j2objc.ast.SuperConstructorInvocation)

Example 2 with Statement

use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.

the class GenerationTest method translateStatements.

/**
 * Translate a string of Java statement(s) into a list of
 * JDT DOM Statements.  Although JDT has support for statement
 * parsing, it doesn't resolve them.  The statements are therefore
 * wrapped in a type declaration so they having bindings.
 */
protected List<Statement> translateStatements(String stmts) {
    // Wrap statements in test class, so type resolution works.
    String source = "public class Test { void test() { " + stmts + "}}";
    CompilationUnit unit = translateType("Test", source);
    final List<Statement> statements = Lists.newArrayList();
    unit.accept(new TreeVisitor() {

        @Override
        public boolean visit(MethodDeclaration node) {
            if (ElementUtil.getName(node.getExecutableElement()).equals("test")) {
                statements.addAll(node.getBody().getStatements());
            }
            return false;
        }
    });
    return statements;
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) Statement(com.google.devtools.j2objc.ast.Statement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration)

Example 3 with Statement

use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.

the class Rewriter method endVisit.

@Override
public void endVisit(ForStatement node) {
    // nodes in the initializers.
    if (node.getInitializers().size() == 1) {
        Object initializer = node.getInitializer(0);
        if (initializer instanceof VariableDeclarationExpression) {
            List<VariableDeclarationFragment> fragments = ((VariableDeclarationExpression) initializer).getFragments();
            for (VariableDeclarationFragment fragment : fragments) {
                if (ElementUtil.hasAnnotation(fragment.getVariableElement(), AutoreleasePool.class)) {
                    Statement loopBody = node.getBody();
                    if (!(loopBody instanceof Block)) {
                        Block block = new Block();
                        node.setBody(block);
                        block.addStatement(loopBody);
                    }
                    ((Block) node.getBody()).setHasAutoreleasePool(true);
                }
            }
        }
    }
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) TryStatement(com.google.devtools.j2objc.ast.TryStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) Block(com.google.devtools.j2objc.ast.Block)

Example 4 with Statement

use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.

the class UnsequencedExpressionRewriter method visit.

@Override
public boolean visit(ForStatement node) {
    List<Expression> initializers = node.getInitializers();
    // expression or a list of initializer expressions.
    if (initializers.size() == 1 && initializers.get(0) instanceof VariableDeclarationExpression) {
        VariableDeclarationExpression decl = (VariableDeclarationExpression) initializers.get(0);
        extractVariableDeclarationFragments(decl.getFragments(), TreeUtil.asStatementList(node).subList(0, 0));
    } else {
        extractExpressionList(initializers, TreeUtil.asStatementList(node).subList(0, 0), false);
    }
    Expression expr = node.getExpression();
    if (expr != null) {
        newExpression(expr);
        expr.accept(this);
        List<VariableAccess> toExtract = getUnsequencedAccesses();
        if (!toExtract.isEmpty()) {
            // Convert "if (;cond;)" into "if (;;) { if (!(cond)) break; ...}".
            List<Statement> stmtList = TreeUtil.asStatementList(node.getBody()).subList(0, 0);
            extractOrderedAccesses(stmtList, currentTopNode, toExtract);
            stmtList.add(createLoopTermination(node.getExpression()));
            node.setExpression(null);
        }
    }
    extractExpressionList(node.getUpdaters(), TreeUtil.asStatementList(node.getBody()), true);
    node.getBody().accept(this);
    return false;
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) AssertStatement(com.google.devtools.j2objc.ast.AssertStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) SynchronizedStatement(com.google.devtools.j2objc.ast.SynchronizedStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression)

Example 5 with Statement

use of com.google.devtools.j2objc.ast.Statement in project j2objc by google.

the class UnsequencedExpressionRewriter method visit.

@Override
public boolean visit(IfStatement node) {
    visitAndExtract(node.getExpression(), node);
    node.getThenStatement().accept(this);
    Statement elseStmt = node.getElseStatement();
    if (elseStmt != null) {
        elseStmt.accept(this);
    }
    return false;
}
Also used : ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) AssertStatement(com.google.devtools.j2objc.ast.AssertStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) SynchronizedStatement(com.google.devtools.j2objc.ast.SynchronizedStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement)

Aggregations

Statement (com.google.devtools.j2objc.ast.Statement)40 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)23 ForStatement (com.google.devtools.j2objc.ast.ForStatement)22 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)22 Block (com.google.devtools.j2objc.ast.Block)21 IfStatement (com.google.devtools.j2objc.ast.IfStatement)20 WhileStatement (com.google.devtools.j2objc.ast.WhileStatement)17 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)15 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)15 SwitchStatement (com.google.devtools.j2objc.ast.SwitchStatement)15 DoStatement (com.google.devtools.j2objc.ast.DoStatement)14 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)13 ThrowStatement (com.google.devtools.j2objc.ast.ThrowStatement)13 Expression (com.google.devtools.j2objc.ast.Expression)12 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)12 SimpleName (com.google.devtools.j2objc.ast.SimpleName)12 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)11 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)11 VariableElement (javax.lang.model.element.VariableElement)11 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)9