Search in sources :

Example 6 with VariableDeclarationExpression

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

the class TreeConverter method convertVariableExpression.

private VariableDeclarationExpression convertVariableExpression(VariableTree node, TreePath parent) {
    TreePath path = getTreePath(parent, node);
    VariableElement element = (VariableElement) getElement(path);
    boolean isVarargs = (((JCVariableDecl) node).sym.flags() & Flags.VARARGS) > 0;
    Type newType = convertType(getTypeMirror(path), getPosition(node), isVarargs);
    VariableDeclarationFragment fragment = new VariableDeclarationFragment();
    fragment.setVariableElement(element).setInitializer((Expression) convert(node.getInitializer(), path));
    return new VariableDeclarationExpression().setType(newType).addFragment(fragment);
}
Also used : ParameterizedType(com.google.devtools.j2objc.ast.ParameterizedType) Type(com.google.devtools.j2objc.ast.Type) SimpleType(com.google.devtools.j2objc.ast.SimpleType) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(com.google.devtools.j2objc.ast.ArrayType) PrimitiveType(com.google.devtools.j2objc.ast.PrimitiveType) UnionType(com.google.devtools.j2objc.ast.UnionType) ExecutableType(javax.lang.model.type.ExecutableType) TreePath(com.sun.source.util.TreePath) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) VariableElement(javax.lang.model.element.VariableElement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 7 with VariableDeclarationExpression

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

the class TreeConverter method convertForLoop.

private TreeNode convertForLoop(JCTree.JCForLoop node) {
    ForStatement newNode = new ForStatement().setExpression((Expression) convert(node.getCondition())).setBody((Statement) convert(node.getStatement()));
    VariableDeclarationExpression lastVar = null;
    for (JCTree.JCStatement initializer : node.getInitializer()) {
        if (initializer.getKind() == Kind.VARIABLE) {
            JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) initializer;
            VariableDeclarationExpression newVar = convertVariableExpression(var);
            if (lastVar == null) {
                newNode.addInitializer(newVar);
                lastVar = newVar;
            } else {
                lastVar.addFragment(TreeUtil.remove(newVar.getFragment(0)));
            }
        } else {
            assert initializer.getKind() == Kind.EXPRESSION_STATEMENT;
            newNode.addInitializer((Expression) convert(((JCTree.JCExpressionStatement) initializer).getExpression()));
        }
    }
    for (JCTree.JCExpressionStatement updater : node.getUpdate()) {
        newNode.addUpdater((Expression) convert(updater.getExpression()));
    }
    return newNode;
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTree(com.sun.tools.javac.tree.JCTree) ForStatement(com.google.devtools.j2objc.ast.ForStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 8 with VariableDeclarationExpression

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

the class MethodTranslator method visitForStatement.

@Override
public TreeNode visitForStatement(com.strobel.decompiler.languages.java.ast.ForStatement node, Void data) {
    ForStatement forStatement = new ForStatement();
    for (com.strobel.decompiler.languages.java.ast.Statement init : node.getInitializers()) {
        TreeNode stmt = init.acceptVisitor(this, null);
        if (stmt instanceof VariableDeclarationStatement) {
            VariableDeclarationStatement varDeclStmt = (VariableDeclarationStatement) stmt;
            VariableDeclarationExpression varDeclExpr = new VariableDeclarationExpression().setType(Type.newType(varDeclStmt.getTypeMirror()));
            for (VariableDeclarationFragment varFrag : varDeclStmt.getFragments()) {
                varDeclExpr.addFragment(varFrag.copy());
            }
            forStatement.addInitializer(varDeclExpr);
        } else {
            forStatement.addInitializer(((ExpressionStatement) stmt).getExpression().copy());
        }
    }
    forStatement.setExpression((Expression) node.getCondition().acceptVisitor(this, null));
    for (com.strobel.decompiler.languages.java.ast.Statement updater : node.getIterators()) {
        TreeNode stmt = updater.acceptVisitor(this, null);
        forStatement.addUpdater(((ExpressionStatement) stmt).getExpression().copy());
    }
    forStatement.setBody((Statement) node.getEmbeddedStatement().acceptVisitor(this, null));
    return forStatement;
}
Also used : TreeNode(com.google.devtools.j2objc.ast.TreeNode) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement)

Example 9 with VariableDeclarationExpression

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

the class TreeConverter method convertForLoop.

private TreeNode convertForLoop(ForLoopTree node, TreePath parent) {
    TreePath path = getTreePath(parent, node);
    ForStatement newNode = new ForStatement().setExpression((Expression) convert(node.getCondition(), path)).setBody((Statement) convert(node.getStatement(), path));
    VariableDeclarationExpression lastVar = null;
    for (StatementTree initializer : node.getInitializer()) {
        if (initializer.getKind() == Kind.VARIABLE) {
            VariableTree var = (VariableTree) initializer;
            VariableDeclarationExpression newVar = convertVariableExpression(var, path);
            if (lastVar == null) {
                newNode.addInitializer(newVar);
                lastVar = newVar;
            } else {
                lastVar.addFragment(TreeUtil.remove(newVar.getFragment(0)));
            }
        } else {
            assert initializer.getKind() == Kind.EXPRESSION_STATEMENT;
            TreePath initializerPath = getTreePath(path, initializer);
            TreeNode expr = convert(((ExpressionStatementTree) initializer).getExpression(), initializerPath);
            newNode.addInitializer((Expression) expr);
        }
    }
    for (ExpressionStatementTree updater : node.getUpdate()) {
        newNode.addUpdater((Expression) convert(updater.getExpression(), getTreePath(path, updater)));
    }
    return newNode;
}
Also used : ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) LabeledStatementTree(com.sun.source.tree.LabeledStatementTree) StatementTree(com.sun.source.tree.StatementTree) TreePath(com.sun.source.util.TreePath) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) JCFunctionalExpression(com.sun.tools.javac.tree.JCTree.JCFunctionalExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) TreeNode(com.google.devtools.j2objc.ast.TreeNode) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) VariableTree(com.sun.source.tree.VariableTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) ForStatement(com.google.devtools.j2objc.ast.ForStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement)

Example 10 with VariableDeclarationExpression

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

the class Rewriter method visit.

@Override
public boolean visit(TryStatement node) {
    // This visit rewrites try-with-resources constructs into regular try statements according to
    // JLS 14.20.3. The rewriting is done in a visit instead of endVisit because the mutations may
    // result in more try-with-resources constructs that need to be rewritten recursively.
    List<TreeNode> resources = node.getResources();
    if (resources.isEmpty()) {
        return true;
    }
    if (!node.getCatchClauses().isEmpty() || node.getFinally() != null) {
        // Extended try-with-resources. (JLS 14.20.3.2)
        // The new innerTry statement will be a "Basic try-with-resources" and will be rewritten by
        // the code below when it is visited.
        TryStatement innerTry = new TryStatement().setBody(TreeUtil.remove(node.getBody()));
        ;
        TreeUtil.moveList(resources, innerTry.getResources());
        node.setBody(new Block().addStatement(innerTry));
        return true;
    }
    // Basic try-with-resources. (JLS 14.20.3.1)
    DeclaredType throwableType = (DeclaredType) typeUtil.getJavaThrowable().asType();
    VariableElement primaryException = GeneratedVariableElement.newLocalVar("__primaryException" + resources.size(), throwableType, null);
    TreeNode resource = resources.remove(0);
    VariableElement resourceVar = null;
    VariableDeclarationFragment resourceFrag = null;
    if (resource.getKind() == Kind.VARIABLE_DECLARATION_EXPRESSION) {
        List<VariableDeclarationFragment> resourceFrags = ((VariableDeclarationExpression) resource).getFragments();
        assert resourceFrags.size() == 1;
        resourceFrag = resourceFrags.get(0);
        resourceVar = resourceFrag.getVariableElement();
    } else {
        resourceVar = TreeUtil.getVariableElement((Expression) resource);
    }
    assert resourceVar != null;
    DeclaredType closeableType = typeUtil.findSupertype(resourceVar.asType(), "java.lang.AutoCloseable");
    ExecutablePair closeMethod = typeUtil.findMethod(closeableType, "close");
    ExecutablePair addSuppressedMethod = typeUtil.findMethod(throwableType, "addSuppressed", "java.lang.Throwable");
    Block block = new Block();
    if (resourceFrag != null) {
        block.addStatement(new VariableDeclarationStatement(resourceVar, TreeUtil.remove(resourceFrag.getInitializer())));
    }
    block.addStatement(new VariableDeclarationStatement(primaryException, new NullLiteral(typeUtil.getNull())));
    // If the current try node is the only statement in its parent block then replace the parent
    // block instead of the try node to avoid extra nesting of braces.
    TreeNode parent = node.getParent();
    if (parent instanceof Block && ((Block) parent).getStatements().size() == 1) {
        parent.replaceWith(block);
    } else {
        node.replaceWith(block);
    }
    block.addStatement(TreeUtil.remove(node));
    VariableElement caughtException = GeneratedVariableElement.newLocalVar("e", throwableType, null);
    Block catchBlock = new Block().addStatement(new ExpressionStatement(new Assignment(new SimpleName(primaryException), new SimpleName(caughtException)))).addStatement(new ThrowStatement(new SimpleName(caughtException)));
    node.addCatchClause(new CatchClause().setException(new SingleVariableDeclaration(caughtException)).setBody(catchBlock));
    Statement closeResource = new ExpressionStatement(new MethodInvocation(closeMethod, typeUtil.getVoid(), new SimpleName(resourceVar)));
    VariableElement suppressedException = GeneratedVariableElement.newLocalVar("e", throwableType, null);
    node.setFinally(new Block().addStatement(new IfStatement().setExpression(new InfixExpression(typeUtil.getBoolean(), InfixExpression.Operator.NOT_EQUALS, new SimpleName(resourceVar), new NullLiteral(typeUtil.getNull()))).setThenStatement(new Block().addStatement(new IfStatement().setExpression(new InfixExpression(typeUtil.getBoolean(), InfixExpression.Operator.NOT_EQUALS, new SimpleName(primaryException), new NullLiteral(typeUtil.getNull()))).setThenStatement(new Block().addStatement(new TryStatement().setBody(new Block().addStatement(closeResource)).addCatchClause(new CatchClause().setException(new SingleVariableDeclaration(suppressedException)).setBody(new Block().addStatement(new ExpressionStatement(new MethodInvocation(addSuppressedMethod, typeUtil.getVoid(), new SimpleName(primaryException)).addArgument(new SimpleName(suppressedException)))))))).setElseStatement(new Block().addStatement(closeResource.copy()))))));
    // Visit the new block instead of the current node because some of content of the node (eg. the
    // resource initializer) has been moved outside of the try node.
    block.accept(this);
    return false;
}
Also used : ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) 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) SimpleName(com.google.devtools.j2objc.ast.SimpleName) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement) CatchClause(com.google.devtools.j2objc.ast.CatchClause) Assignment(com.google.devtools.j2objc.ast.Assignment) IfStatement(com.google.devtools.j2objc.ast.IfStatement) TryStatement(com.google.devtools.j2objc.ast.TryStatement) Expression(com.google.devtools.j2objc.ast.Expression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) TreeNode(com.google.devtools.j2objc.ast.TreeNode) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) NullLiteral(com.google.devtools.j2objc.ast.NullLiteral) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)11 ForStatement (com.google.devtools.j2objc.ast.ForStatement)7 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)6 Expression (com.google.devtools.j2objc.ast.Expression)5 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)5 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)5 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)5 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)5 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)4 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)4 Statement (com.google.devtools.j2objc.ast.Statement)4 Block (com.google.devtools.j2objc.ast.Block)3 CastExpression (com.google.devtools.j2objc.ast.CastExpression)3 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)3 IfStatement (com.google.devtools.j2objc.ast.IfStatement)3 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)3 ThrowStatement (com.google.devtools.j2objc.ast.ThrowStatement)3 TreeNode (com.google.devtools.j2objc.ast.TreeNode)3 VariableElement (javax.lang.model.element.VariableElement)3 ArrayType (com.google.devtools.j2objc.ast.ArrayType)2