Search in sources :

Example 86 with Statement

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

the class ASTHelper method getNextStatement.

/**
 * Returns the next statement in the source file if it exists.
 *
 * @param startNode the start node
 * @return the next statement in the source file if it exists, null otherwise
 */
public static Statement getNextStatement(Statement startNode) {
    final Statement nextSibling = getNextSibling(startNode);
    if (nextSibling != null) {
        return nextSibling;
    }
    final ASTNode parent = startNode.getParent();
    if (parent instanceof Statement) {
        return getNextStatement((Statement) parent);
    }
    return null;
}
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) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 87 with Statement

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

the class NoSettingRatherThanUselessSettingRefactoring method visit.

@Override
public boolean visit(VariableDeclarationStatement node) {
    if (node.fragments() != null && node.fragments().size() == 1) {
        final VariableDeclarationFragment fragment = (VariableDeclarationFragment) node.fragments().get(0);
        if (fragment.getInitializer() != null && fragment.getInitializer().resolveConstantExpressionValue() != null) {
            final IVariableBinding variable = fragment.resolveBinding();
            Statement stmtToInspect = getNextSibling(node);
            boolean isOverridden = false;
            boolean isRead = false;
            while (stmtToInspect != null && !isOverridden && !isRead) {
                if (stmtToInspect instanceof ExpressionStatement) {
                    final Assignment assignment = asExpression(stmtToInspect, Assignment.class);
                    isOverridden = hasOperator(assignment, ASSIGN) && isSameVariable(fragment.getName(), assignment.getLeftHandSide());
                }
                isRead = !new VariableDefinitionsUsesVisitor(variable, stmtToInspect).find().getUses().isEmpty();
                stmtToInspect = getNextSibling(stmtToInspect);
            }
            if (isOverridden && !isRead) {
                ctx.getRefactorings().remove(fragment.getInitializer());
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) Statement(org.eclipse.jdt.core.dom.Statement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 88 with Statement

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

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

the class SwitchRefactoring method mergeCases.

private void mergeCases(Merge merge, SwitchCaseSection sectionToKeep, SwitchCaseSection sectionToRemove) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    final Statement caseKept;
    if (merge == Merge.BEFORE_SWITCH_CASES) {
        caseKept = sectionToKeep.existingCases.get(0);
    } else {
        // move == Move.AFTER_SWITCH_CASES
        caseKept = sectionToKeep.stmts.get(0);
    }
    for (final SwitchCase caseToMove : sectionToRemove.existingCases) {
        r.insertBefore(b.move(caseToMove), caseKept);
    }
    r.remove(sectionToRemove.stmts);
}
Also used : SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) 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) Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 90 with Statement

use of org.eclipse.jdt.core.dom.Statement 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)

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