use of org.autorefactor.util.Pair 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.util.Pair in project AutoRefactor by JnRouvignac.
the class ObsoleteCommonCodeInIfElseStatementCleanUp method getMatchingCases.
private List<Integer> getMatchingCases(final List<List<Statement>> allCasesStatements, final ASTSemanticMatcher matcher) {
List<Pair<Statement, List<Integer>>> matchingCases = new ArrayList<>();
for (int i = 0; i < allCasesStatements.size(); i++) {
boolean isMatching = false;
Statement currentStatement = allCasesStatements.get(i).get(allCasesStatements.get(i).size() - 1);
for (Pair<Statement, List<Integer>> pair : matchingCases) {
if (ASTNodes.match(matcher, pair.getFirst(), currentStatement)) {
pair.getSecond().add(i);
isMatching = true;
break;
}
}
if (!isMatching) {
Pair<Statement, List<Integer>> newPair = Pair.<Statement, List<Integer>>of(currentStatement, new ArrayList<>());
newPair.getSecond().add(i);
matchingCases.add(newPair);
}
}
if (matchingCases.isEmpty()) {
return null;
}
Collections.sort(matchingCases, (o1, o2) -> Integer.compare(o2.getSecond().size(), o1.getSecond().size()));
Pair<Statement, List<Integer>> notFallingThroughCase = null;
for (Pair<Statement, List<Integer>> matchingCase : matchingCases) {
if (!ASTNodes.fallsThrough(matchingCase.getFirst())) {
if (notFallingThroughCase != null) {
return null;
}
notFallingThroughCase = matchingCase;
}
}
if (notFallingThroughCase != null) {
return notFallingThroughCase.getSecond();
}
return matchingCases.get(0).getSecond();
}
use of org.autorefactor.util.Pair in project AutoRefactor by JnRouvignac.
the class CommentsCleanUp method betterCommentExist.
private boolean betterCommentExist(final Comment comment, final ASTNode nodeWhereToAddJavadoc) {
if (hasJavadoc(nodeWhereToAddJavadoc)) {
return true;
}
SourceLocation nodeLoc = new SourceLocation(nodeWhereToAddJavadoc);
SourceLocation bestLoc = new SourceLocation(comment);
Comment bestComment = comment;
for (Iterator<Pair<SourceLocation, Comment>> iter = this.comments.iterator(); iter.hasNext(); ) {
Pair<SourceLocation, Comment> pair = iter.next();
SourceLocation newLoc = pair.getFirst();
Comment newComment = pair.getSecond();
if (newLoc.compareTo(bestLoc) < 0) {
// Since comments are visited in ascending order,
// we can forget this comment.
iter.remove();
continue;
}
if (nodeLoc.compareTo(newLoc) < 0) {
break;
}
if (bestLoc.compareTo(newLoc) < 0 && (!(newComment instanceof LineComment) || !(bestComment instanceof LineComment))) {
// New comment is a BlockComment or a Javadoc
bestLoc = newLoc;
bestComment = newComment;
continue;
}
}
return bestComment != null && bestComment != comment;
}
use of org.autorefactor.util.Pair in project AutoRefactor by JnRouvignac.
the class StringBuilderCleanUp method visit.
@Override
public boolean visit(final MethodInvocation visited) {
if (visited.getExpression() != null && // $NON-NLS-1$
"append".equals(visited.getName().getIdentifier()) && visited.arguments().size() == 1 && // Most expensive check comes last
ASTNodes.hasType(visited.getExpression(), StringBuilder.class.getCanonicalName(), StringBuffer.class.getCanonicalName())) {
return maybeRefactorAppending(visited);
}
if (// $NON-NLS-1$
ASTNodes.usesGivenSignature(visited, StringBuilder.class.getCanonicalName(), "toString") || ASTNodes.usesGivenSignature(visited, StringBuffer.class.getCanonicalName(), "toString")) {
// $NON-NLS-1$
List<Pair<ITypeBinding, Expression>> allAppendedStrings = new LinkedList<>();
Expression lastExpression = readAppendMethod(visited.getExpression(), allAppendedStrings, new AtomicBoolean(false), new AtomicBoolean(false));
if (lastExpression instanceof ClassInstanceCreation) {
// Replace with String concatenation
TextEditGroup group = new TextEditGroup(MultiFixMessages.StringBuilderCleanUp_description);
ASTRewrite rewrite = cuRewrite.getASTRewrite();
rewrite.replace(visited, createStringConcats(allAppendedStrings), group);
return false;
}
}
return true;
}
use of org.autorefactor.util.Pair in project AutoRefactor by JnRouvignac.
the class CommentsRefactoring method betterCommentExist.
private boolean betterCommentExist(Comment comment, ASTNode nodeWhereToAddJavadoc) {
if (hasJavadoc(nodeWhereToAddJavadoc)) {
return true;
}
final SourceLocation nodeLoc = new SourceLocation(nodeWhereToAddJavadoc);
SourceLocation bestLoc = new SourceLocation(comment);
Comment bestComment = comment;
for (Iterator<Pair<SourceLocation, Comment>> iter = this.comments.iterator(); iter.hasNext(); ) {
final Pair<SourceLocation, Comment> pair = iter.next();
final SourceLocation newLoc = pair.getFirst();
final Comment newComment = pair.getSecond();
if (newLoc.compareTo(bestLoc) < 0) {
// since comments are visited in ascending order,
// we can forget this comment.
iter.remove();
continue;
}
if (nodeLoc.compareTo(newLoc) < 0) {
break;
}
if (bestLoc.compareTo(newLoc) < 0) {
if (!(newComment instanceof LineComment)) {
// new comment is a BlockComment or a Javadoc
bestLoc = newLoc;
bestComment = newComment;
continue;
} else if (!(bestComment instanceof LineComment)) {
// new comment is a line comment and best comment is not
bestLoc = newLoc;
bestComment = newComment;
continue;
}
}
}
return bestComment != null && bestComment != comment;
}
Aggregations