Search in sources :

Example 6 with Pair

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;
}
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 7 with Pair

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();
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Pair(org.autorefactor.util.Pair)

Example 8 with Pair

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;
}
Also used : SourceLocation(org.autorefactor.jdt.internal.corext.dom.SourceLocation) LineComment(org.eclipse.jdt.core.dom.LineComment) Comment(org.eclipse.jdt.core.dom.Comment) BlockComment(org.eclipse.jdt.core.dom.BlockComment) LineComment(org.eclipse.jdt.core.dom.LineComment) Pair(org.autorefactor.util.Pair)

Example 9 with Pair

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;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup) LinkedList(java.util.LinkedList) Pair(org.autorefactor.util.Pair)

Example 10 with Pair

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;
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) LineComment(org.eclipse.jdt.core.dom.LineComment) Comment(org.eclipse.jdt.core.dom.Comment) BlockComment(org.eclipse.jdt.core.dom.BlockComment) LineComment(org.eclipse.jdt.core.dom.LineComment) Pair(org.autorefactor.util.Pair)

Aggregations

Pair (org.autorefactor.util.Pair)10 LinkedList (java.util.LinkedList)6 Expression (org.eclipse.jdt.core.dom.Expression)6 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Statement (org.eclipse.jdt.core.dom.Statement)4 IfStatement (org.eclipse.jdt.core.dom.IfStatement)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)2 BlockComment (org.eclipse.jdt.core.dom.BlockComment)2 CastExpression (org.eclipse.jdt.core.dom.CastExpression)2 Comment (org.eclipse.jdt.core.dom.Comment)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 LineComment (org.eclipse.jdt.core.dom.LineComment)2 TextEditGroup (org.eclipse.text.edits.TextEditGroup)2 HashSet (java.util.HashSet)1 SourceLocation (org.autorefactor.jdt.internal.corext.dom.SourceLocation)1 VarConflictVisitor (org.autorefactor.jdt.internal.corext.dom.VarConflictVisitor)1 SourceLocation (org.autorefactor.refactoring.SourceLocation)1