Search in sources :

Example 16 with IfStatement

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

the class CommonCodeInIfElseStatementRefactoring method collectAllCases.

/**
     * Collects all cases (if/else, if/else if/else, etc.) and returns whether all are covered.
     *
     * @param allCases the output collection for all the cases
     * @param node the {@link IfStatement} to examine
     * @return true if all cases (if/else, if/else if/else, etc.) are covered,
     *         false otherwise
     */
private boolean collectAllCases(List<List<Statement>> allCases, IfStatement node) {
    final List<Statement> thenStmts = asList(node.getThenStatement());
    final List<Statement> elseStmts = asList(node.getElseStatement());
    if (thenStmts.isEmpty() || elseStmts.isEmpty()) {
        // let other refactorings take care of removing empty blocks.
        return false;
    }
    allCases.add(thenStmts);
    if (elseStmts.size() == 1) {
        final IfStatement is = as(elseStmts.get(0), IfStatement.class);
        if (is != null) {
            return collectAllCases(allCases, is);
        }
    }
    allCases.add(elseStmts);
    return true;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement)

Example 17 with IfStatement

use of org.eclipse.jdt.core.dom.IfStatement 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 18 with IfStatement

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

the class MergeConditionalBlocksRefactoring method visit.

@Override
public boolean visit(IfStatement node) {
    final Expression firstCondition = node.getExpression();
    final List<Statement> ifCode = asList(node.getThenStatement());
    final List<Statement> elseCode = asList(node.getElseStatement());
    if (elseCode != null && elseCode.size() == 1 && elseCode.get(0) instanceof IfStatement) {
        final IfStatement subNode = (IfStatement) elseCode.get(0);
        final Expression secondCondition = subNode.getExpression();
        return maybeMergeBlocks(firstCondition, ifCode, subNode, secondCondition, subNode.getThenStatement(), subNode.getElseStatement(), true) && maybeMergeBlocks(firstCondition, ifCode, subNode, secondCondition, subNode.getElseStatement(), subNode.getThenStatement(), false);
    }
    return VISIT_SUBTREE;
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement)

Example 19 with IfStatement

use of org.eclipse.jdt.core.dom.IfStatement 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 20 with IfStatement

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

the class InputFlowAnalyzer method endVisit.

@Override
public void endVisit(IfStatement node) {
    if (skipNode(node))
        return;
    Statement thenPart = node.getThenStatement();
    Statement elsePart = node.getElseStatement();
    if ((thenPart != null && fSelection.coveredBy(thenPart)) || (elsePart != null && fSelection.coveredBy(elsePart))) {
        GenericSequentialFlowInfo info = createSequential();
        setFlowInfo(node, info);
        endVisitConditional(info, node.getExpression(), new ASTNode[] { thenPart, elsePart });
    } else {
        super.endVisit(node);
    }
}
Also used : ForStatement(org.eclipse.jdt.core.dom.ForStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) 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) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement)

Aggregations

IfStatement (org.eclipse.jdt.core.dom.IfStatement)30 Statement (org.eclipse.jdt.core.dom.Statement)28 Block (org.eclipse.jdt.core.dom.Block)19 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)19 ForStatement (org.eclipse.jdt.core.dom.ForStatement)19 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)19 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)19 DoStatement (org.eclipse.jdt.core.dom.DoStatement)18 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)16 AST (org.eclipse.jdt.core.dom.AST)15 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)15 Expression (org.eclipse.jdt.core.dom.Expression)14 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)14 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)14 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)14 ASTNode (org.eclipse.jdt.core.dom.ASTNode)13 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)13 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)12 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)12 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)12