Search in sources :

Example 56 with Statement

use of org.eclipse.jdt.core.dom.Statement in project flux by eclipse.

the class QuickAssistProcessor method removeCatchBlock.

private static void removeCatchBlock(ASTRewrite rewrite, CatchClause catchClause) {
    TryStatement tryStatement = (TryStatement) catchClause.getParent();
    if (tryStatement.catchClauses().size() > 1 || tryStatement.getFinally() != null || !tryStatement.resources().isEmpty()) {
        rewrite.remove(catchClause, null);
    } else {
        Block block = tryStatement.getBody();
        List<Statement> statements = block.statements();
        int nStatements = statements.size();
        if (nStatements == 1) {
            ASTNode first = statements.get(0);
            rewrite.replace(tryStatement, rewrite.createCopyTarget(first), null);
        } else if (nStatements > 1) {
            ListRewrite listRewrite = rewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
            ASTNode first = statements.get(0);
            ASTNode last = statements.get(statements.size() - 1);
            ASTNode newStatement = listRewrite.createCopyTarget(first, last);
            if (ASTNodes.isControlStatementBody(tryStatement.getLocationInParent())) {
                Block newBlock = rewrite.getAST().newBlock();
                newBlock.statements().add(newStatement);
                newStatement = newBlock;
            }
            rewrite.replace(tryStatement, newStatement, null);
        } else {
            rewrite.remove(tryStatement, null);
        }
    }
}
Also used : TryStatement(org.eclipse.jdt.core.dom.TryStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite)

Example 57 with Statement

use of org.eclipse.jdt.core.dom.Statement in project flux by eclipse.

the class AdvancedQuickAssistProcessor method copyStatementExceptBreak.

private static Statement copyStatementExceptBreak(AST ast, ASTRewrite rewrite, Statement source) {
    if (source instanceof Block) {
        Block block = (Block) source;
        Block newBlock = ast.newBlock();
        for (Iterator<Statement> iter = block.statements().iterator(); iter.hasNext(); ) {
            Statement statement = iter.next();
            if (statement instanceof BreakStatement) {
                continue;
            }
            newBlock.statements().add(copyStatementExceptBreak(ast, rewrite, statement));
        }
        return newBlock;
    }
    return (Statement) rewrite.createMoveTarget(source);
}
Also used : BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 58 with Statement

use of org.eclipse.jdt.core.dom.Statement in project flux by eclipse.

the class AdvancedQuickAssistProcessor method getJoinIfListInIfElseIfProposals.

private static boolean getJoinIfListInIfElseIfProposals(IInvocationContext context, ASTNode covering, ArrayList<ASTNode> coveredNodes, Collection<ICommandAccess> resultingCollections) {
    if (coveredNodes.isEmpty()) {
        return false;
    }
    // check that we have more than one covered statement
    if (coveredNodes.size() < 2) {
        return false;
    }
    // check that all selected nodes are 'if' statements with only 'then' statement
    for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
        ASTNode node = iter.next();
        if (!(node instanceof IfStatement)) {
            return false;
        }
        IfStatement ifStatement = (IfStatement) node;
        if (ifStatement.getElseStatement() != null) {
            return false;
        }
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //
    final AST ast = covering.getAST();
    final ASTRewrite rewrite = ASTRewrite.create(ast);
    //
    IfStatement firstIfStatement = (IfStatement) coveredNodes.get(0);
    IfStatement firstNewIfStatement = null;
    //
    IfStatement prevIfStatement = null;
    for (Iterator<ASTNode> iter = coveredNodes.iterator(); iter.hasNext(); ) {
        IfStatement ifStatement = (IfStatement) iter.next();
        // prepare new 'if' statement
        IfStatement newIfStatement = ast.newIfStatement();
        newIfStatement.setExpression((Expression) rewrite.createMoveTarget(ifStatement.getExpression()));
        // prepare 'then' statement and convert into block if needed
        Statement thenStatement = (Statement) rewrite.createMoveTarget(ifStatement.getThenStatement());
        if (ifStatement.getThenStatement() instanceof IfStatement) {
            IfStatement ifBodyStatement = (IfStatement) ifStatement.getThenStatement();
            if (ifBodyStatement.getElseStatement() == null) {
                Block thenBlock = ast.newBlock();
                thenBlock.statements().add(thenStatement);
                thenStatement = thenBlock;
            }
        }
        newIfStatement.setThenStatement(thenStatement);
        //
        if (prevIfStatement != null) {
            prevIfStatement.setElseStatement(newIfStatement);
            rewrite.remove(ifStatement, null);
        } else {
            firstNewIfStatement = newIfStatement;
        }
        prevIfStatement = newIfStatement;
    }
    rewrite.replace(firstIfStatement, firstNewIfStatement, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_joinIfSequence;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.JOIN_IF_SEQUENCE);
    resultingCollections.add(proposal);
    return true;
}
Also used : ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) AST(org.eclipse.jdt.core.dom.AST) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) Block(org.eclipse.jdt.core.dom.Block)

Example 59 with Statement

use of org.eclipse.jdt.core.dom.Statement in project flux by eclipse.

the class ScopeAnalyzer method getDeclarationsAfter.

public IBinding[] getDeclarationsAfter(int offset, int flags) {
    try {
        org.eclipse.jdt.core.dom.NodeFinder finder = new org.eclipse.jdt.core.dom.NodeFinder(fRoot, offset, 0);
        ASTNode node = finder.getCoveringNode();
        if (node == null) {
            return null;
        }
        ASTNode declaration = ASTResolving.findParentStatement(node);
        while (declaration instanceof Statement && declaration.getNodeType() != ASTNode.BLOCK) {
            declaration = declaration.getParent();
        }
        if (declaration instanceof Block) {
            DefaultBindingRequestor requestor = new DefaultBindingRequestor();
            DeclarationsAfterVisitor visitor = new DeclarationsAfterVisitor(node.getStartPosition(), flags, requestor);
            declaration.accept(visitor);
            List<IBinding> result = requestor.getResult();
            return result.toArray(new IBinding[result.size()]);
        }
        return NO_BINDING;
    } finally {
        clearLists();
    }
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) TypeDeclarationStatement(org.eclipse.jdt.core.dom.TypeDeclarationStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) IBinding(org.eclipse.jdt.core.dom.IBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block)

Example 60 with Statement

use of org.eclipse.jdt.core.dom.Statement in project che by eclipse.

the class ExtractMethodAnalyzer method getParentLoopBody.

private Statement getParentLoopBody(ASTNode node) {
    Statement stmt = null;
    ASTNode start = node;
    while (start != null && !(start instanceof ForStatement) && !(start instanceof DoStatement) && !(start instanceof WhileStatement) && !(start instanceof EnhancedForStatement) && !(start instanceof SwitchStatement)) {
        start = start.getParent();
    }
    if (start instanceof ForStatement) {
        stmt = ((ForStatement) start).getBody();
    } else if (start instanceof DoStatement) {
        stmt = ((DoStatement) start).getBody();
    } else if (start instanceof WhileStatement) {
        stmt = ((WhileStatement) start).getBody();
    } else if (start instanceof EnhancedForStatement) {
        stmt = ((EnhancedForStatement) start).getBody();
    }
    if (start != null && start.getParent() instanceof LabeledStatement) {
        LabeledStatement labeledStatement = (LabeledStatement) start.getParent();
        fEnclosingLoopLabel = labeledStatement.getLabel();
    }
    return stmt;
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement)

Aggregations

Statement (org.eclipse.jdt.core.dom.Statement)95 ForStatement (org.eclipse.jdt.core.dom.ForStatement)59 IfStatement (org.eclipse.jdt.core.dom.IfStatement)58 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)56 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)51 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)49 DoStatement (org.eclipse.jdt.core.dom.DoStatement)46 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)46 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)45 Block (org.eclipse.jdt.core.dom.Block)42 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)39 Expression (org.eclipse.jdt.core.dom.Expression)38 ASTNode (org.eclipse.jdt.core.dom.ASTNode)34 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)30 AST (org.eclipse.jdt.core.dom.AST)26 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)26 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)25 TryStatement (org.eclipse.jdt.core.dom.TryStatement)23 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)22 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)22