Example usage for org.eclipse.jdt.core.dom TryStatement resources

List of usage examples for org.eclipse.jdt.core.dom TryStatement resources

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom TryStatement resources.

Prototype

ASTNode.NodeList resources

To view the source code for org.eclipse.jdt.core.dom TryStatement resources.

Click Source Link

Document

The resource expressions (element type: Expression ).

Usage

From source file:at.bestsolution.fxide.jdt.corext.dom.ASTFlattener.java

License:Open Source License

@Override
public boolean visit(TryStatement node) {
    this.fBuffer.append("try ");//$NON-NLS-1$
    if (node.getAST().apiLevel() >= JLS4) {
        if (!node.resources().isEmpty()) {
            this.fBuffer.append("(");//$NON-NLS-1$
            for (Iterator<VariableDeclarationExpression> it = node.resources().iterator(); it.hasNext();) {
                VariableDeclarationExpression var = it.next();
                var.accept(this);
                if (it.hasNext()) {
                    this.fBuffer.append(",");//$NON-NLS-1$
                }/*w w  w .j a  v  a2 s  . co  m*/
            }
            this.fBuffer.append(") ");//$NON-NLS-1$
        }
    }
    node.getBody().accept(this);
    this.fBuffer.append(" ");//$NON-NLS-1$
    for (Iterator<CatchClause> it = node.catchClauses().iterator(); it.hasNext();) {
        CatchClause cc = it.next();
        cc.accept(this);
    }
    if (node.getFinally() != null) {
        this.fBuffer.append("finally ");//$NON-NLS-1$
        node.getFinally().accept(this);
    }
    return false;
}

From source file:boa.datagen.util.Java7Visitor.java

License:Apache License

@Override
public boolean visit(TryStatement node) {
    boa.types.Ast.Statement.Builder b = boa.types.Ast.Statement.newBuilder();
    //      b.setPosition(pos.build());
    List<boa.types.Ast.Statement> list = statements.peek();
    b.setKind(boa.types.Ast.Statement.StatementKind.TRY);
    statements.push(new ArrayList<boa.types.Ast.Statement>());
    node.getBody().accept(this);
    for (Object c : node.catchClauses())
        ((CatchClause) c).accept(this);
    if (node.getFinally() != null)
        node.getFinally().accept(this);
    for (boa.types.Ast.Statement s : statements.pop())
        b.addStatements(s);/*from w ww  .j av a  2  s  . c o m*/
    if (node.resources() != null)
        for (Object v : node.resources()) {
            ((VariableDeclarationExpression) v).accept(this);
            b.addInitializations(expressions.pop());
        }
    list.add(b.build());
    return false;
}

From source file:com.bsiag.eclipse.jdt.java.formatter.linewrap.WrapPreparator.java

License:Open Source License

@Override
public boolean visit(TryStatement node) {
    handleArguments(node.resources(), this.options.alignment_for_resources_in_try);
    return true;//from ww w  .j  ava  2  s  .  c om
}

From source file:com.bsiag.eclipse.jdt.java.formatter.SpacePreparator.java

License:Open Source License

@Override
public boolean visit(TryStatement node) {
    List<VariableDeclarationExpression> resources = node.resources();
    if (!resources.isEmpty()) {
        handleToken(node, TokenNameLPAREN, this.options.insert_space_before_opening_paren_in_try,
                this.options.insert_space_after_opening_paren_in_try);
        handleTokenBefore(node.getBody(), TokenNameRPAREN,
                this.options.insert_space_before_closing_paren_in_try, false);
        for (int i = 1; i < resources.size(); i++) {
            handleTokenBefore(resources.get(i), TokenNameSEMICOLON,
                    this.options.insert_space_before_semicolon_in_try_resources,
                    this.options.insert_space_after_semicolon_in_try_resources);
        }/*from  w w  w  .  j a v  a 2  s  .  co m*/
        // there can be a semicolon after the last resource
        int index = this.tm.firstIndexAfter(resources.get(resources.size() - 1), -1);
        while (index < this.tm.size()) {
            Token token = this.tm.get(index++);
            if (token.tokenType == TokenNameSEMICOLON) {
                handleToken(token, this.options.insert_space_before_semicolon_in_try_resources, false);
            } else if (token.tokenType == TokenNameRPAREN) {
                break;
            }
        }
    }
    return true;
}

From source file:com.google.devtools.j2objc.util.ASTUtil.java

License:Apache License

@SuppressWarnings("unchecked")
public static List<VariableDeclarationExpression> getResources(TryStatement tryStatement) {
    return tryStatement.resources();
}

From source file:com.google.googlejavaformat.java.JavaInputAstVisitor.java

License:Apache License

/** Visitor method for {@link TryStatement}s. */
@Override/*from  ww w . j av  a  2 s  .  c o  m*/
public boolean visit(TryStatement node) {
    sync(node);
    builder.open(ZERO);
    token("try");
    builder.space();
    if (!node.resources().isEmpty()) {
        token("(");
        builder.open(plusFour);
        boolean first = true;
        for (VariableDeclarationExpression resource : (List<VariableDeclarationExpression>) node.resources()) {
            if (!first) {
                token(";");
                builder.forcedBreak();
            }
            visit(resource);
            first = false;
        }
        // TODO(cushon): emit a space after the optional trailing semi-colon
        builder.guessToken(";");
        token(")");
        builder.close();
        builder.space();
    }
    // An empty try-with-resources body can collapse to "{}" if there are no trailing catch or
    // finally blocks.
    boolean trailingClauses = !node.catchClauses().isEmpty() || node.getFinally() != null;
    visitBlock(node.getBody(), CollapseEmptyOrNot.valueOf(!trailingClauses), AllowLeadingBlankLine.YES,
            AllowTrailingBlankLine.valueOf(trailingClauses));
    for (int i = 0; i < node.catchClauses().size(); i++) {
        CatchClause catchClause = (CatchClause) node.catchClauses().get(i);
        trailingClauses = i < node.catchClauses().size() - 1 || node.getFinally() != null;
        visitCatchClause(catchClause, AllowTrailingBlankLine.valueOf(trailingClauses));
    }
    if (node.getFinally() != null) {
        builder.space();
        token("finally");
        builder.space();
        visitBlock(node.getFinally(), CollapseEmptyOrNot.NO, AllowLeadingBlankLine.YES,
                AllowTrailingBlankLine.NO);
    }
    builder.close();
    return false;
}

From source file:org.autorefactor.refactoring.ASTHelper.java

License:Open Source License

/**
 * Generecized version of the equivalent JDT method.
 *
 * @param node the node on which to call the equivalent JDT method
 * @return a List of variable declaration expressions
 * @see TryStatement#resources()/*from  ww w  . j a  v  a2s  .  c  o m*/
 */
@SuppressWarnings("unchecked")
public static List<VariableDeclarationExpression> resources(TryStatement node) {
    return node.resources();
}

From source file:org.autorefactor.refactoring.rules.DeadCodeEliminationRefactoring.java

License:Open Source License

@Override
public boolean visit(TryStatement node) {
    final List<Statement> tryStmts = asList(node.getBody());
    if (tryStmts.isEmpty()) {
        final List<Statement> finallyStmts = asList(node.getFinally());
        if (!finallyStmts.isEmpty()) {
            final ASTBuilder b = this.ctx.getASTBuilder();
            this.ctx.getRefactorings().replace(node, b.copy(node.getFinally()));
            return DO_NOT_VISIT_SUBTREE;
        } else if (node.resources().isEmpty()) {
            this.ctx.getRefactorings().remove(node);
            return DO_NOT_VISIT_SUBTREE;
        }//from w ww .  j  a  va  2s.c om
    }
    // }else {
    // for (CatchClause catchClause : (List<CatchClause>) node.catchClauses()) {
    // final List<Statement> finallyStmts = asList(catchClause.getBody());
    // if (finallyStmts.isEmpty()) {
    // // TODO cannot remove without checking what subsequent catch clauses are
    // catching
    // this.ctx.getRefactorings().remove(catchClause);
    // }
    // }
    //
    // final List<Statement> finallyStmts = asList(node.getFinally());
    // if (finallyStmts.isEmpty()) {
    // this.ctx.getRefactorings().remove(node.getFinally());
    // }
    // // TODO If all finally and catch clauses have been removed,
    // // then we can remove the whole try statement and replace it with a simple block
    // return DO_NOT_VISIT_SUBTREE; // TODO JNR is this correct?
    // }
    return VISIT_SUBTREE;
}

From source file:org.autorefactor.refactoring.rules.TryWithResourceRefactoring.java

License:Open Source License

@Override
public boolean visit(TryStatement node) {
    final List<Statement> tryStmts = asList(node.getBody());
    if (!tryStmts.isEmpty() && tryStmts.get(0).getNodeType() == TRY_STATEMENT) {
        final TryStatement innerTryStmt = as(tryStmts.get(0), TryStatement.class);
        if (innerTryStmt != null && !innerTryStmt.resources().isEmpty()
                && innerTryStmt.catchClauses().isEmpty()) {
            return collapseTryStatements(node, innerTryStmt);
        }/*w  ww  .  j  a va  2s  .c  o  m*/
    }

    final VariableDeclarationStatement previousDeclStmt = as(getPreviousStatement(node),
            VariableDeclarationStatement.class);
    if (previousDeclStmt == null) {
        return VISIT_SUBTREE;
    }

    final VariableDeclarationFragment previousDeclFragment = getUniqueFragment(previousDeclStmt);
    final List<Statement> finallyStmts = asList(node.getFinally());
    if (previousDeclFragment != null && !finallyStmts.isEmpty()) {
        final List<ASTNode> nodesToRemove = new ArrayList<ASTNode>();
        nodesToRemove.add(previousDeclStmt);

        final Statement finallyStmt = finallyStmts.get(0);
        nodesToRemove.add(finallyStmts.size() == 1 ? node.getFinally() : finallyStmt);

        final ExpressionStatement finallyEs = as(finallyStmt, ExpressionStatement.class);
        final IfStatement finallyIs = as(finallyStmt, IfStatement.class);
        if (finallyEs != null) {
            final MethodInvocation mi = as(finallyEs.getExpression(), MethodInvocation.class);
            if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, mi.getExpression())) {
                final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt,
                        previousDeclFragment, nodesToRemove);
                return refactorToTryWithResources(node, newResource, nodesToRemove);
            }
        } else if (finallyIs != null && asList(finallyIs.getThenStatement()).size() == 1
                && asList(finallyIs.getElseStatement()).isEmpty()) {
            final Expression nullCheckedExpr = getNullCheckedExpression(finallyIs.getExpression());

            final Statement thenStmt = asList(finallyIs.getThenStatement()).get(0);
            final MethodInvocation mi = asExpression(thenStmt, MethodInvocation.class);
            if (methodClosesCloseables(mi)
                    && areSameVariables(previousDeclFragment, nullCheckedExpr, mi.getExpression())) {
                final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt,
                        previousDeclFragment, nodesToRemove);
                return refactorToTryWithResources(node, newResource, nodesToRemove);
            }
        }
    }
    return VISIT_SUBTREE;
}

From source file:org.codemucker.jmutate.ast.JAstFlattener.java

License:Open Source License

public boolean visit(TryStatement node) {
    printIndent();/*from   w ww.  j ava 2 s.  com*/
    this.buffer.append("try ");//$NON-NLS-1$
    if (node.getAST().apiLevel() >= JLS4) {
        List resources = node.resources();
        if (!resources.isEmpty()) {
            this.buffer.append('(');
            for (Iterator it = resources.iterator(); it.hasNext();) {
                VariableDeclarationExpression variable = (VariableDeclarationExpression) it.next();
                variable.accept(this);
                if (it.hasNext()) {
                    this.buffer.append(';');
                }
            }
            this.buffer.append(')');
        }
    }
    node.getBody().accept(this);
    this.buffer.append(" ");//$NON-NLS-1$
    for (Iterator it = node.catchClauses().iterator(); it.hasNext();) {
        CatchClause cc = (CatchClause) it.next();
        cc.accept(this);
    }
    if (node.getFinally() != null) {
        this.buffer.append(" finally ");//$NON-NLS-1$
        node.getFinally().accept(this);
    }
    return false;
}