Search in sources :

Example 1 with VarConflictVisitor

use of org.autorefactor.jdt.internal.corext.dom.VarConflictVisitor in project AutoRefactor by JnRouvignac.

the class AbstractUnitTestCleanUp method maybeRefactorIfObjectsAreNotUsed.

/**
 * Maybe refactor the statement.
 *
 * @param classesToUseWithImport The classes to use with import
 * @param importsToAdd           The imports to add
 * @param nodeToReplace          The node
 * @param originalMethod         The method invocation
 * @param isAssertTrue           True if assertTrue is used, False if assertFalse is
 *                               used.
 * @param condition              The condition on which the assert is based.
 * @param failureMessage         The failure message or null.
 * @return True if refactored
 */
protected boolean maybeRefactorIfObjectsAreNotUsed(final Set<String> classesToUseWithImport, final Set<String> importsToAdd, final ASTNode nodeToReplace, final MethodInvocation originalMethod, final boolean isAssertTrue, final Expression condition, final Expression failureMessage) {
    VariableVisitor visitor = new VariableVisitor();
    condition.accept(visitor);
    VarConflictVisitor varOccurrenceVisitor = new VarConflictVisitor(visitor.getVariables(), true);
    varOccurrenceVisitor.traverseNodeInterruptibly((Expression) originalMethod.arguments().get(0));
    if (!varOccurrenceVisitor.isVarConflicting()) {
        return maybeRefactorStatement(classesToUseWithImport, importsToAdd, nodeToReplace, originalMethod, isAssertTrue, condition, failureMessage, true);
    }
    return true;
}
Also used : VarConflictVisitor(org.autorefactor.jdt.internal.corext.dom.VarConflictVisitor)

Example 2 with VarConflictVisitor

use of org.autorefactor.jdt.internal.corext.dom.VarConflictVisitor in project AutoRefactor by JnRouvignac.

the class ObsoleteIfRatherThanTwoSwitchCasesCleanUp method visit.

@SuppressWarnings("deprecation")
@Override
public boolean visit(final SwitchStatement visited) {
    List<?> statements = visited.statements();
    if (statements.isEmpty()) {
        return true;
    }
    Set<SimpleName> previousVarIds = new HashSet<>();
    Set<SimpleName> caseVarIds = new HashSet<>();
    List<Pair<List<Expression>, List<Statement>>> switchStructure = new ArrayList<>();
    List<Expression> caseExprs = new ArrayList<>();
    List<Statement> caseStatements = new ArrayList<>();
    boolean isPreviousStmtACase = true;
    int caseIndexWithDefault = -1;
    for (Object object : statements) {
        Statement statement = (Statement) object;
        if (statement instanceof SwitchCase) {
            if (!isPreviousStmtACase) {
                if (switchStructure.size() > 2) {
                    return true;
                }
                previousVarIds.addAll(caseVarIds);
                caseVarIds.clear();
                switchStructure.add(Pair.<List<Expression>, List<Statement>>of(caseExprs, caseStatements));
                caseExprs = new ArrayList<>();
                caseStatements = new ArrayList<>();
            }
            if (((SwitchCase) statement).isDefault()) {
                caseIndexWithDefault = switchStructure.size();
            } else {
                caseExprs.add(((SwitchCase) statement).getExpression());
            }
            isPreviousStmtACase = true;
        } else {
            VarConflictVisitor varOccurrenceVisitor = new VarConflictVisitor(previousVarIds, false);
            varOccurrenceVisitor.traverseNodeInterruptibly(statement);
            if (varOccurrenceVisitor.isVarConflicting()) {
                return true;
            }
            caseVarIds.addAll(ASTNodes.getLocalVariableIdentifiers(statement, false));
            caseStatements.add(statement);
            isPreviousStmtACase = false;
        }
    }
    switchStructure.add(Pair.<List<Expression>, List<Statement>>of(caseExprs, caseStatements));
    if (caseIndexWithDefault != -1) {
        Pair<List<Expression>, List<Statement>> caseWithDefault = switchStructure.remove(caseIndexWithDefault);
        switchStructure.add(caseWithDefault);
    }
    if (switchStructure.size() > 2 || !switchStructure.get(switchStructure.size() - 1).getFirst().isEmpty() && !ASTNodes.isPassive(visited.getExpression())) {
        return true;
    }
    List<BreakStatement> overBreaks = new ArrayList<>();
    for (int i = 0; i < switchStructure.size(); i++) {
        Pair<List<Expression>, List<Statement>> caseStructure = switchStructure.get(i);
        if (!caseStructure.getSecond().isEmpty()) {
            Statement lastStatement = caseStructure.getSecond().get(caseStructure.getSecond().size() - 1);
            if (i < switchStructure.size() - 1 && !ASTNodes.fallsThrough(lastStatement)) {
                return true;
            }
            BreakStatement breakStatement = ASTNodes.as(lastStatement, BreakStatement.class);
            if (breakStatement != null && breakStatement.getLabel() == null) {
                caseStructure.getSecond().remove(caseStructure.getSecond().size() - 1);
            }
        } else if (i < switchStructure.size() - 1) {
            return true;
        }
    }
    for (Pair<List<Expression>, List<Statement>> caseStructure : switchStructure) {
        for (Statement oneStatement : caseStructure.getSecond()) {
            BreakVisitor breakVisitor = new BreakVisitor();
            breakVisitor.traverseNodeInterruptibly(oneStatement);
            if (!breakVisitor.canBeRefactored()) {
                return true;
            }
            overBreaks.addAll(breakVisitor.getBreaks());
        }
    }
    replaceBySwitch(visited, switchStructure, caseIndexWithDefault, overBreaks);
    return false;
}
Also used : 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) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) VarConflictVisitor(org.autorefactor.jdt.internal.corext.dom.VarConflictVisitor) SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Pair(org.autorefactor.util.Pair)

Example 3 with VarConflictVisitor

use of org.autorefactor.jdt.internal.corext.dom.VarConflictVisitor in project AutoRefactor by JnRouvignac.

the class ObsoleteCommonCodeInIfElseStatementCleanUp method hasVariableConflict.

private boolean hasVariableConflict(final IfStatement node, final List<Statement>[] caseStmtsToRemove2) {
    Set<SimpleName> ifVariableNames = new HashSet<>();
    for (List<Statement> caseStmtsToRemove : caseStmtsToRemove2) {
        for (Statement caseStmtToRemove : caseStmtsToRemove) {
            ifVariableNames.addAll(ASTNodes.getLocalVariableIdentifiers(caseStmtToRemove, false));
        }
    }
    for (Statement statement : ASTNodes.getNextSiblings(node)) {
        VarConflictVisitor varOccurrenceVisitor = new VarConflictVisitor(ifVariableNames, true);
        varOccurrenceVisitor.traverseNodeInterruptibly(statement);
        if (varOccurrenceVisitor.isVarConflicting()) {
            return true;
        }
    }
    return false;
}
Also used : VarConflictVisitor(org.autorefactor.jdt.internal.corext.dom.VarConflictVisitor) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) HashSet(java.util.HashSet)

Example 4 with VarConflictVisitor

use of org.autorefactor.jdt.internal.corext.dom.VarConflictVisitor in project AutoRefactor by JnRouvignac.

the class ObsoleteBreakRatherThanPassiveIterationsCleanUp method areAssignmentsValid.

private boolean areAssignmentsValid(final Set<SimpleName> allowedVars, final List<Statement> assignments) {
    for (Statement statement : assignments) {
        VariableDeclarationStatement variableDeclaration = ASTNodes.as(statement, VariableDeclarationStatement.class);
        Assignment assignment = ASTNodes.asExpression(statement, Assignment.class);
        if (variableDeclaration != null) {
            for (Object obj : variableDeclaration.fragments()) {
                VariableDeclarationFragment fragment = (VariableDeclarationFragment) obj;
                if (!ASTNodes.isHardCoded(fragment.getInitializer())) {
                    return false;
                }
            }
        } else if (assignment != null && ASTNodes.hasOperator(assignment, Assignment.Operator.ASSIGN) && ASTNodes.isHardCoded(assignment.getRightHandSide()) && ASTNodes.isPassive(assignment.getLeftHandSide())) {
            VarConflictVisitor varOccurrenceVisitor = new VarConflictVisitor(allowedVars, true);
            varOccurrenceVisitor.traverseNodeInterruptibly(assignment.getLeftHandSide());
            if (varOccurrenceVisitor.isVarConflicting()) {
                return false;
            }
        } else {
            return false;
        }
    }
    return true;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) VarConflictVisitor(org.autorefactor.jdt.internal.corext.dom.VarConflictVisitor) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Aggregations

VarConflictVisitor (org.autorefactor.jdt.internal.corext.dom.VarConflictVisitor)4 IfStatement (org.eclipse.jdt.core.dom.IfStatement)3 Statement (org.eclipse.jdt.core.dom.Statement)3 HashSet (java.util.HashSet)2 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)2 ForStatement (org.eclipse.jdt.core.dom.ForStatement)2 SimpleName (org.eclipse.jdt.core.dom.SimpleName)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Pair (org.autorefactor.util.Pair)1 Assignment (org.eclipse.jdt.core.dom.Assignment)1 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)1 DoStatement (org.eclipse.jdt.core.dom.DoStatement)1 Expression (org.eclipse.jdt.core.dom.Expression)1 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)1 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)1 SwitchCase (org.eclipse.jdt.core.dom.SwitchCase)1 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)1 ThrowStatement (org.eclipse.jdt.core.dom.ThrowStatement)1 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)1