Search in sources :

Example 41 with IfStatement

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

the class RemoveEmptyIfRefactoring method visit.

@Override
public boolean visit(IfStatement node) {
    final Refactorings r = this.ctx.getRefactorings();
    final Statement thenStmt = node.getThenStatement();
    final Statement elseStmt = node.getElseStatement();
    if (elseStmt != null && asList(elseStmt).isEmpty()) {
        r.remove(elseStmt);
        return DO_NOT_VISIT_SUBTREE;
    } else if (thenStmt != null && asList(thenStmt).isEmpty()) {
        final ASTBuilder b = this.ctx.getASTBuilder();
        final Expression condition = node.getExpression();
        if (elseStmt != null) {
            r.replace(node, b.if0(b.negate(condition), b.move(elseStmt)));
        } else if (isPassive(condition)) {
            removeBlock(node, r, b);
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ForStatement(org.eclipse.jdt.core.dom.ForStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) 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) Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 42 with IfStatement

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

the class SwitchRefactoring method visit.

@Override
public boolean visit(final IfStatement node) {
    if (hasUnlabeledBreak(node)) {
        return VISIT_SUBTREE;
    }
    Variable variable = extractVariableAndValues(node);
    if (variable == null) {
        return VISIT_SUBTREE;
    }
    final SimpleName switchExpr = variable.name;
    final List<SwitchCaseSection> cases = new ArrayList<SwitchCaseSection>();
    Statement remainingStmt = null;
    final Set<String> variableDeclarationIds = new HashSet<String>();
    IfStatement currentNode = node;
    while (haveSameIdentifier(switchExpr, variable.name) && haveSameType(switchExpr, variable.name)) {
        if (detectDeclarationConflicts(currentNode.getThenStatement(), variableDeclarationIds)) {
            // Cannot declare two variables with the same name in two cases
            return VISIT_SUBTREE;
        }
        cases.add(new SwitchCaseSection(variable.constantValues, asList(currentNode.getThenStatement())));
        remainingStmt = currentNode.getElseStatement();
        variable = extractVariableAndValues(remainingStmt);
        if (variable == null) {
            break;
        }
        currentNode = (IfStatement) remainingStmt;
    }
    final List<SwitchCaseSection> filteredCases = filterDuplicateCaseValues(cases);
    return maybeReplaceWithSwitchStmt(node, switchExpr, filteredCases, remainingStmt);
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) 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) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 43 with IfStatement

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

the class CFGBuilder method buildCFG.

/**
 * Builds a CFG for the passed in statement.
 *
 * @param node the statement for which to build a CFG
 * @param state the liveness state before the current statement.
 *          It contains: the live edges before the current statement,
 *          the live basic block to which the current statement might be added.
 *          If null, then the new basic block must be created for the current statement.
 * @param throwers the thrower blocks information
 * @return the new live state after the current statement
 */
public LivenessState buildCFG(IfStatement node, LivenessState state, ThrowerBlocks throwers) {
    final CFGBasicBlock exprBlock = getCFGBasicBlock(node, state.nextStmtWillCreateNewBlock(), true);
    try {
        addVariableAccess(exprBlock, node.getExpression(), READ, throwers);
        final LivenessState result = new LivenessState();
        CFGEdgeBuilder thenEdge = new CFGEdgeBuilder(node.getExpression(), true, exprBlock);
        result.addAll(buildCFG(node.getThenStatement(), LivenessState.of(thenEdge), throwers));
        final Statement elseStmt = node.getElseStatement();
        CFGEdgeBuilder elseEdge = new CFGEdgeBuilder(node.getExpression(), false, exprBlock);
        if (elseStmt != null) {
            result.addAll(buildCFG(elseStmt, LivenessState.of(elseEdge), throwers));
        } else {
            result.add(elseEdge);
        }
        return result.nextStmtWillCreateNewBlock();
    } finally {
        moveAllEdgesToBuild(node, state);
    }
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) SynchronizedStatement(org.eclipse.jdt.core.dom.SynchronizedStatement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) TypeDeclarationStatement(org.eclipse.jdt.core.dom.TypeDeclarationStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) CFGEdgeBuilder(org.autorefactor.cfg.CFGEdgeBuilder)

Aggregations

IfStatement (org.eclipse.jdt.core.dom.IfStatement)43 Statement (org.eclipse.jdt.core.dom.Statement)39 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)27 ForStatement (org.eclipse.jdt.core.dom.ForStatement)27 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)27 DoStatement (org.eclipse.jdt.core.dom.DoStatement)25 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)25 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)23 Block (org.eclipse.jdt.core.dom.Block)22 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)21 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)21 Expression (org.eclipse.jdt.core.dom.Expression)18 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)17 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)17 AST (org.eclipse.jdt.core.dom.AST)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)16 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)15 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)15 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)15 ConditionalExpression (org.eclipse.jdt.core.dom.ConditionalExpression)13