Search in sources :

Example 6 with WhileStatement

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

the class AdvancedQuickAssistProcessor method getInverseIfIntoContinueInLoopsProposals.

private static boolean getInverseIfIntoContinueInLoopsProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    if (!(covering instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) covering;
    if (ifStatement.getElseStatement() != null) {
        return false;
    }
    // prepare outer control structure and block that contains 'if' statement
    ASTNode ifParent = ifStatement.getParent();
    Block ifParentBlock = null;
    ASTNode ifParentStructure = ifParent;
    if (ifParentStructure instanceof Block) {
        ifParentBlock = (Block) ifParent;
        ifParentStructure = ifParentStructure.getParent();
    }
    // check that control structure is loop and 'if' statement if last statement
    if (!(ifParentStructure instanceof ForStatement) && !(ifParentStructure instanceof WhileStatement)) {
        return false;
    }
    if (ifParentBlock != null && ifParentBlock.statements().indexOf(ifStatement) != ifParentBlock.statements().size() - 1) {
        return false;
    }
    //  we could produce quick assist
    if (resultingCollections == null) {
        return true;
    }
    //
    AST ast = covering.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // create inverted 'if' statement
    Expression inversedExpression = getInversedExpression(rewrite, ifStatement.getExpression());
    IfStatement newIf = ast.newIfStatement();
    newIf.setExpression(inversedExpression);
    newIf.setThenStatement(ast.newContinueStatement());
    //
    if (ifParentBlock == null) {
        // if there is no block, create it
        ifParentBlock = ast.newBlock();
        ifParentBlock.statements().add(newIf);
        for (Iterator<Statement> iter = getUnwrappedStatements(ifStatement.getThenStatement()).iterator(); iter.hasNext(); ) {
            Statement statement = iter.next();
            ifParentBlock.statements().add(rewrite.createMoveTarget(statement));
        }
        // replace 'if' statement as body with new block
        if (ifParentStructure instanceof ForStatement) {
            rewrite.set(ifParentStructure, ForStatement.BODY_PROPERTY, ifParentBlock, null);
        } else if (ifParentStructure instanceof WhileStatement) {
            rewrite.set(ifParentStructure, WhileStatement.BODY_PROPERTY, ifParentBlock, null);
        }
    } else {
        // if there was block, replace
        ListRewrite listRewriter = rewrite.getListRewrite(ifParentBlock, (ChildListPropertyDescriptor) ifStatement.getLocationInParent());
        listRewriter.replace(ifStatement, newIf, null);
        // add statements from 'then' to the end of block
        for (Iterator<Statement> iter = getUnwrappedStatements(ifStatement.getThenStatement()).iterator(); iter.hasNext(); ) {
            Statement statement = iter.next();
            listRewriter.insertLast(rewrite.createMoveTarget(statement), null);
        }
    }
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseIfToContinue_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERT_IF_TO_CONTINUE);
    resultingCollections.add(proposal);
    return true;
}
Also used : 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) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ListRewrite(org.eclipse.jdt.core.dom.rewrite.ListRewrite) ASTRewriteCorrectionProposal(org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement)

Example 7 with WhileStatement

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

the class AdvancedQuickAssistProcessor method getInverseIfContinueIntoIfThenInLoopsProposals.

private static boolean getInverseIfContinueIntoIfThenInLoopsProposals(IInvocationContext context, ASTNode covering, Collection<ICommandAccess> resultingCollections) {
    if (!(covering instanceof IfStatement)) {
        return false;
    }
    IfStatement ifStatement = (IfStatement) covering;
    if (ifStatement.getElseStatement() != null) {
        return false;
    }
    // check that 'then' is 'continue'
    if (!(ifStatement.getThenStatement() instanceof ContinueStatement)) {
        return false;
    }
    // check that 'if' statement is statement in block that is body of loop
    Block loopBlock = null;
    if (ifStatement.getParent() instanceof Block && ifStatement.getParent().getParent() instanceof ForStatement) {
        loopBlock = (Block) ifStatement.getParent();
    } else if (ifStatement.getParent() instanceof Block && ifStatement.getParent().getParent() instanceof WhileStatement) {
        loopBlock = (Block) ifStatement.getParent();
    } else {
        return false;
    }
    if (resultingCollections == null) {
        return true;
    }
    //
    AST ast = covering.getAST();
    ASTRewrite rewrite = ASTRewrite.create(ast);
    // create inverted 'if' statement
    Expression inversedExpression = getInversedExpression(rewrite, ifStatement.getExpression());
    IfStatement newIf = ast.newIfStatement();
    newIf.setExpression(inversedExpression);
    // prepare 'then' for new 'if'
    Block thenBlock = ast.newBlock();
    int ifIndex = loopBlock.statements().indexOf(ifStatement);
    for (int i = ifIndex + 1; i < loopBlock.statements().size(); i++) {
        Statement statement = (Statement) loopBlock.statements().get(i);
        thenBlock.statements().add(rewrite.createMoveTarget(statement));
    }
    newIf.setThenStatement(thenBlock);
    // replace 'if' statement in loop
    rewrite.replace(ifStatement, newIf, null);
    // add correction proposal
    String label = CorrectionMessages.AdvancedQuickAssistProcessor_inverseIfContinue_description;
    //		Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
    ASTRewriteCorrectionProposal proposal = new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.INVERSE_IF_CONTINUE);
    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) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) 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) ASTRewrite(org.eclipse.jdt.core.dom.rewrite.ASTRewrite) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement)

Example 8 with WhileStatement

use of org.eclipse.jdt.core.dom.WhileStatement in project AutoRefactor by JnRouvignac.

the class CleanCodeRatherThanSemicolonRefactoring method visit.

@Override
public boolean visit(EmptyStatement node) {
    ASTNode parent = node.getParent();
    if (parent instanceof Block) {
        this.ctx.getRefactorings().remove(node);
        return DO_NOT_VISIT_SUBTREE;
    }
    parent = getParentIgnoring(node, Block.class);
    if (parent instanceof IfStatement) {
        IfStatement is = (IfStatement) parent;
        List<Statement> thenStmts = asList(is.getThenStatement());
        List<Statement> elseStmts = asList(is.getElseStatement());
        boolean thenIsEmptyStmt = thenStmts.size() == 1 && is(thenStmts.get(0), EmptyStatement.class);
        boolean elseIsEmptyStmt = elseStmts.size() == 1 && is(elseStmts.get(0), EmptyStatement.class);
        if (thenIsEmptyStmt && elseIsEmptyStmt) {
            this.ctx.getRefactorings().remove(parent);
            return DO_NOT_VISIT_SUBTREE;
        } else if (thenIsEmptyStmt && is.getElseStatement() == null) {
            this.ctx.getRefactorings().remove(is);
            return DO_NOT_VISIT_SUBTREE;
        } else if (elseIsEmptyStmt) {
            this.ctx.getRefactorings().remove(is.getElseStatement());
            return DO_NOT_VISIT_SUBTREE;
        }
    } else if (parent instanceof TryStatement) {
        TryStatement ts = (TryStatement) parent;
        return maybeRemoveEmptyStmtBody(node, ts, ts.getBody());
    } else if (parent instanceof EnhancedForStatement) {
        EnhancedForStatement efs = (EnhancedForStatement) parent;
        return maybeRemoveEmptyStmtBody(node, efs, efs.getBody());
    } else if (parent instanceof ForStatement) {
        ForStatement fs = (ForStatement) parent;
        return maybeRemoveEmptyStmtBody(node, fs, fs.getBody());
    } else if (parent instanceof WhileStatement) {
        WhileStatement ws = (WhileStatement) parent;
        return maybeRemoveEmptyStmtBody(node, ws, ws.getBody());
    }
    return VISIT_SUBTREE;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Example 9 with WhileStatement

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

the class SourceProvider method isSingleControlStatementWithoutBlock.

private boolean isSingleControlStatementWithoutBlock() {
    List<Statement> statements = fDeclaration.getBody().statements();
    int size = statements.size();
    if (size != 1)
        return false;
    Statement statement = statements.get(size - 1);
    int nodeType = statement.getNodeType();
    if (nodeType == ASTNode.IF_STATEMENT) {
        IfStatement ifStatement = (IfStatement) statement;
        return !(ifStatement.getThenStatement() instanceof Block) && !(ifStatement.getElseStatement() instanceof Block);
    } else if (nodeType == ASTNode.FOR_STATEMENT) {
        return !(((ForStatement) statement).getBody() instanceof Block);
    } else if (nodeType == ASTNode.WHILE_STATEMENT) {
        return !(((WhileStatement) statement).getBody() instanceof Block);
    }
    return false;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement)

Example 10 with WhileStatement

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

the class ControlStatementsFix method createRemoveBlockFix.

public static ControlStatementsFix[] createRemoveBlockFix(CompilationUnit compilationUnit, ASTNode node) {
    if (!(node instanceof Statement)) {
        return null;
    }
    Statement statement = (Statement) node;
    if (statement instanceof Block) {
        Block block = (Block) statement;
        if (block.statements().size() != 1)
            return null;
        ASTNode parent = block.getParent();
        if (!(parent instanceof Statement))
            return null;
        statement = (Statement) parent;
    }
    if (statement instanceof IfStatement) {
        List<ControlStatementsFix> result = new ArrayList<ControlStatementsFix>();
        List<RemoveBlockOperation> removeAllList = new ArrayList<RemoveBlockOperation>();
        IfElseIterator iter = new IfElseIterator((IfStatement) statement);
        IfStatement item = null;
        while (iter.hasNext()) {
            item = iter.next();
            if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(item, IfStatement.THEN_STATEMENT_PROPERTY)) {
                RemoveBlockOperation op = new RemoveBlockOperation(item, IfStatement.THEN_STATEMENT_PROPERTY);
                removeAllList.add(op);
                if (item == statement)
                    result.add(new ControlStatementsFix(FixMessages.ControlStatementsFix_removeIfBlock_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }));
            }
        }
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(item, IfStatement.ELSE_STATEMENT_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(item, IfStatement.ELSE_STATEMENT_PROPERTY);
            removeAllList.add(op);
            if (item == statement)
                result.add(new ControlStatementsFix(FixMessages.ControlStatementsFix_removeElseBlock_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }));
        }
        if (removeAllList.size() > 1) {
            CompilationUnitRewriteOperation[] allConvert = removeAllList.toArray(new CompilationUnitRewriteOperation[removeAllList.size()]);
            result.add(new ControlStatementsFix(FixMessages.ControlStatementsFix_removeIfElseBlock_proposalDescription, compilationUnit, allConvert));
        }
        return result.toArray(new ControlStatementsFix[result.size()]);
    } else if (statement instanceof WhileStatement) {
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, WhileStatement.BODY_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(statement, WhileStatement.BODY_PROPERTY);
            return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
        }
    } else if (statement instanceof ForStatement) {
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, ForStatement.BODY_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(statement, ForStatement.BODY_PROPERTY);
            return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
        }
    } else if (statement instanceof EnhancedForStatement) {
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, EnhancedForStatement.BODY_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(statement, EnhancedForStatement.BODY_PROPERTY);
            return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
        }
    } else if (statement instanceof DoStatement) {
        if (RemoveBlockOperation.satisfiesQuickAssistPrecondition(statement, DoStatement.BODY_PROPERTY)) {
            RemoveBlockOperation op = new RemoveBlockOperation(statement, DoStatement.BODY_PROPERTY);
            return new ControlStatementsFix[] { new ControlStatementsFix(FixMessages.ControlStatementsFix_removeBrackets_proposalDescription, compilationUnit, new CompilationUnitRewriteOperation[] { op }) };
        }
    }
    return null;
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) ArrayList(java.util.ArrayList) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Aggregations

EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)12 ForStatement (org.eclipse.jdt.core.dom.ForStatement)12 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)12 DoStatement (org.eclipse.jdt.core.dom.DoStatement)11 Statement (org.eclipse.jdt.core.dom.Statement)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 Block (org.eclipse.jdt.core.dom.Block)10 IfStatement (org.eclipse.jdt.core.dom.IfStatement)9 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)9 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)7 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)5 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)5 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)5 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)5 AST (org.eclipse.jdt.core.dom.AST)4 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)4 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)4 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)4 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)3 CastExpression (org.eclipse.jdt.core.dom.CastExpression)3