Search in sources :

Example 6 with ASTMatcher

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

the class UpdateSetRatherThanTestingFirstRefactoring method maybeReplaceSetContains.

private boolean maybeReplaceSetContains(final IfStatement ifStmtToReplace, final Expression ifExpr, final Statement stmt, final Statement oppositeStmt, final boolean negate, final String methodName) {
    final List<Statement> stmts = asList(stmt);
    final MethodInvocation miContains = as(ifExpr, MethodInvocation.class);
    if (!stmts.isEmpty() && isMethod(miContains, "java.util.Set", "contains", "java.lang.Object")) {
        final Statement firstStmt = getFirst(stmts);
        final MethodInvocation miAddOrRemove = asExpression(firstStmt, MethodInvocation.class);
        final ASTMatcher astMatcher = new ASTMatcher();
        if (isMethod(miAddOrRemove, "java.util.Set", methodName, "java.lang.Object") && match(astMatcher, miContains.getExpression(), miAddOrRemove.getExpression()) && match(astMatcher, arg0(miContains), arg0(miAddOrRemove))) {
            final ASTBuilder b = this.ctx.getASTBuilder();
            final Refactorings r = this.ctx.getRefactorings();
            if (stmts.size() == 1 && asList(oppositeStmt).isEmpty()) {
                // Only one statement: replace if statement with col.add() (or col.remove())
                r.replace(ifStmtToReplace, b.move(firstStmt));
                return DO_NOT_VISIT_SUBTREE;
            } else {
                // There are other statements, replace the if condition with col.add() (or col.remove())
                r.replace(ifStmtToReplace.getExpression(), negate ? b.negate(miAddOrRemove, ASTBuilder.Copy.MOVE) : b.move(miAddOrRemove));
                r.remove(firstStmt);
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Refactorings(org.autorefactor.refactoring.Refactorings) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 7 with ASTMatcher

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

the class ORConditionRatherThanRedundantClausesRefactoring method maybeRefactorCondition.

private boolean maybeRefactorCondition(final InfixExpression node, final Operator operator, final Expression leftOperand, final Expression rightOperand, final boolean forward) {
    final InfixExpression complexCondition = as(leftOperand, InfixExpression.class);
    if (complexCondition != null && !complexCondition.hasExtendedOperands() && (hasOperator(complexCondition, CONDITIONAL_AND) || hasOperator(complexCondition, AND))) {
        final AtomicBoolean isFirstExprPositive = new AtomicBoolean();
        final AtomicBoolean isSecondExprPositive = new AtomicBoolean();
        final AtomicBoolean isThirdExprPositive = new AtomicBoolean();
        final Expression firstExpr = getBasisExpression(complexCondition.getLeftOperand(), isFirstExprPositive);
        final Expression secondExpr = getBasisExpression(complexCondition.getRightOperand(), isSecondExprPositive);
        final Expression thirdExpr = getBasisExpression(rightOperand, isThirdExprPositive);
        if (isPrimitive(firstExpr) && isPrimitive(secondExpr) && isPrimitive(thirdExpr)) {
            if (match(new ASTMatcher(), secondExpr, thirdExpr)) {
                replaceDuplicateExpr(node, operator, firstExpr, thirdExpr, isFirstExprPositive.get(), isThirdExprPositive.get(), forward);
                return DO_NOT_VISIT_SUBTREE;
            } else if (match(new ASTMatcher(), firstExpr, thirdExpr)) {
                replaceDuplicateExpr(node, operator, secondExpr, thirdExpr, isSecondExprPositive.get(), isThirdExprPositive.get(), forward);
                return DO_NOT_VISIT_SUBTREE;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 8 with ASTMatcher

use of org.eclipse.jdt.core.dom.ASTMatcher in project che by eclipse.

the class UnresolvedElementsSubProcessor method getArgumentName.

private static String getArgumentName(List<Expression> arguments, int index) {
    String def = String.valueOf(index + 1);
    ASTNode expr = arguments.get(index);
    if (expr.getLength() > 18) {
        return def;
    }
    ASTMatcher matcher = new ASTMatcher();
    for (int i = 0; i < arguments.size(); i++) {
        if (i != index && matcher.safeSubtreeMatch(expr, arguments.get(i))) {
            return def;
        }
    }
    return '\'' + BasicElementLabels.getJavaElementName(ASTNodes.asString(expr)) + '\'';
}
Also used : ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 9 with ASTMatcher

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

the class CommonCodeInIfElseStatementRefactoring method visit.

// TODO handle switch statements
// TODO also handle ternary operator, ConditionalExpression
@Override
public boolean visit(IfStatement node) {
    if (node.getElseStatement() == null) {
        return VISIT_SUBTREE;
    }
    if (!(node.getParent() instanceof Block)) {
        // when not inside curly braces
        return VISIT_SUBTREE;
    }
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    final List<List<Statement>> allCasesStmts = new ArrayList<List<Statement>>();
    final List<List<ASTNode>> removedCaseStmts = new LinkedList<List<ASTNode>>();
    // Collect all the if / else if / else if / ... / else cases
    if (collectAllCases(allCasesStmts, node)) {
        // initialize removedCaseStmts list
        for (int i = 0; i < allCasesStmts.size(); i++) {
            removedCaseStmts.add(new LinkedList<ASTNode>());
        }
        // If all cases exist
        final ASTMatcher matcher = new ASTMatcherSameVariablesAndMethods();
        final int minSize = minSize(allCasesStmts);
        final List<Statement> caseStmts = allCasesStmts.get(0);
        boolean result = VISIT_SUBTREE;
        // Identify matching statements starting from the beginning of each case
        for (int stmtIndex = 0; stmtIndex < minSize; stmtIndex++) {
            if (!match(matcher, allCasesStmts, true, stmtIndex, 0, allCasesStmts.size())) {
                break;
            }
            r.insertBefore(b.copy(caseStmts.get(stmtIndex)), node);
            removeStmts(allCasesStmts, true, stmtIndex, removedCaseStmts);
            result = DO_NOT_VISIT_SUBTREE;
        }
        // Identify matching statements starting from the end of each case
        for (int stmtIndex = 1; 0 <= minSize - stmtIndex; stmtIndex++) {
            if (!match(matcher, allCasesStmts, false, stmtIndex, 0, allCasesStmts.size()) || anyContains(removedCaseStmts, allCasesStmts, stmtIndex)) {
                break;
            }
            r.insertAfter(b.copy(caseStmts.get(caseStmts.size() - stmtIndex)), node);
            removeStmts(allCasesStmts, false, stmtIndex, removedCaseStmts);
            result = DO_NOT_VISIT_SUBTREE;
        }
        // Remove the nodes common to all cases
        final boolean[] areCasesEmpty = new boolean[allCasesStmts.size()];
        for (int i = 0; i < allCasesStmts.size(); i++) {
            areCasesEmpty[i] = false;
        }
        removeStmtsFromCases(allCasesStmts, removedCaseStmts, areCasesEmpty);
        if (allEmpty(areCasesEmpty)) {
            r.removeButKeepComment(node);
            return DO_NOT_VISIT_SUBTREE;
        }
        // Remove empty cases
        if (areCasesEmpty[0]) {
            if (areCasesEmpty.length == 2 && !areCasesEmpty[1]) {
                // Then clause is empty and there is only one else clause
                // => revert if statement
                r.replace(node, b.if0(b.not(b.parenthesizeIfNeeded(b.move(node.getExpression()))), b.move(node.getElseStatement())));
            } else {
                r.replace(node.getThenStatement(), b.block());
            }
            result = DO_NOT_VISIT_SUBTREE;
        }
        for (int i = 1; i < areCasesEmpty.length; i++) {
            if (areCasesEmpty[i]) {
                final Statement firstStmt = allCasesStmts.get(i).get(0);
                r.remove(findNodeToRemove(firstStmt, firstStmt.getParent()));
                result = DO_NOT_VISIT_SUBTREE;
            }
        }
        return result;
    }
    return VISIT_SUBTREE;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) Refactorings(org.autorefactor.refactoring.Refactorings) ArrayList(java.util.ArrayList) ASTBuilder(org.autorefactor.refactoring.ASTBuilder) LinkedList(java.util.LinkedList) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ASTMatcherSameVariablesAndMethods(org.autorefactor.refactoring.ASTMatcherSameVariablesAndMethods) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Example 10 with ASTMatcher

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

the class DoWhileRatherThanDuplicateCodeRefactoring method visit.

@Override
public boolean visit(WhileStatement node) {
    final List<Statement> whileStmts = asList(node.getBody());
    if (whileStmts.isEmpty()) {
        return VISIT_SUBTREE;
    }
    final List<Statement> previousStmts = new ArrayList<Statement>(whileStmts.size());
    final ASTMatcher matcher = new ASTMatcher();
    Statement previousStmt = getPreviousSibling(node);
    int i = whileStmts.size() - 1;
    while (i >= 0) {
        if (previousStmt == null || !match(matcher, previousStmt, whileStmts.get(i))) {
            return VISIT_SUBTREE;
        }
        i--;
        previousStmts.add(previousStmt);
        previousStmt = getPreviousSibling(previousStmt);
    }
    replaceWithDoWhile(node, previousStmts);
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Statement(org.eclipse.jdt.core.dom.Statement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ArrayList(java.util.ArrayList) ASTMatcher(org.eclipse.jdt.core.dom.ASTMatcher)

Aggregations

ASTMatcher (org.eclipse.jdt.core.dom.ASTMatcher)10 Expression (org.eclipse.jdt.core.dom.Expression)5 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)5 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)5 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 IfStatement (org.eclipse.jdt.core.dom.IfStatement)3 Statement (org.eclipse.jdt.core.dom.Statement)3 ArrayList (java.util.ArrayList)2 Refactorings (org.autorefactor.refactoring.Refactorings)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ASTMatcherSameVariablesAndMethods (org.autorefactor.refactoring.ASTMatcherSameVariablesAndMethods)1 Block (org.eclipse.jdt.core.dom.Block)1 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)1 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)1