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;
}
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;
}
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;
}
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;
}
Aggregations