Search in sources :

Example 1 with Refactorings

use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.

the class AbstractUnitTestRefactoring method maybeRefactorComparison.

private boolean maybeRefactorComparison(final ASTNode nodeToReplace, final MethodInvocation originalMethod, final InfixExpression ie, final boolean isAssertEquals, final Expression failureMessage, final boolean isRewriteNeeded) {
    final Pair<Expression, Expression> actualAndExpected = getActualAndExpected(ie.getLeftOperand(), ie.getRightOperand());
    if (isComparingObjects(ie) && !isNullLiteral(ie.getLeftOperand()) && !isNullLiteral(ie.getRightOperand())) {
        final ASTBuilder b = this.ctx.getASTBuilder();
        final Refactorings r = this.ctx.getRefactorings();
        final MethodInvocation newAssert = invokeMethod(b, originalMethod, getAssertName(isAssertEquals, "Same"), b.copy(actualAndExpected.getFirst()), b.copy(actualAndExpected.getSecond()), failureMessage);
        r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, newAssert));
        return DO_NOT_VISIT_SUBTREE;
    } else {
        return maybeRefactorToAssertEquals(nodeToReplace, originalMethod, isAssertEquals, actualAndExpected.getFirst(), actualAndExpected.getSecond(), failureMessage, true);
    }
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) Refactorings(org.autorefactor.refactoring.Refactorings) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 2 with Refactorings

use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.

the class AbstractUnitTestRefactoring method maybeRefactorToAssertEquals.

/**
     * Maybe refactor the assert equals.
     *
     * @param nodeToReplace
     *            The node to replace
     * @param originalMethod
     *            The node
     * @param isAssertEquals
     *            The is assert equals
     * @param actualValue
     *            The actual value
     * @param expectedValue
     *            The expected value
     * @param failureMessage
     *            The failure message
     * @param isRewriteNeeded
     *            True if is the rewriting is needed.
     * @return The return
     */
protected boolean maybeRefactorToAssertEquals(final ASTNode nodeToReplace, final MethodInvocation originalMethod, final boolean isAssertEquals, final Expression actualValue, final Expression expectedValue, final Expression failureMessage, final boolean isRewriteNeeded) {
    final Refactorings r = this.ctx.getRefactorings();
    final ASTBuilder b = this.ctx.getASTBuilder();
    if (isNullLiteral(actualValue)) {
        r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, invokeAssertNull(originalMethod, isAssertEquals, expectedValue, failureMessage)));
        return DO_NOT_VISIT_SUBTREE;
    } else if (isNullLiteral(expectedValue)) {
        r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, invokeAssertNull(originalMethod, isAssertEquals, actualValue, failureMessage)));
        return DO_NOT_VISIT_SUBTREE;
    } else if ((isConstant(actualValue) || isVariableNamedExpected(actualValue)) && !isConstant(expectedValue) && !isVariableNamedExpected(expectedValue)) {
        final MethodInvocation newAssert = invokeMethod(b, originalMethod, getAssertName(isAssertEquals, "Equals"), b.copy(expectedValue), b.copy(actualValue), failureMessage);
        r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, newAssert));
        return DO_NOT_VISIT_SUBTREE;
    } else if (isRewriteNeeded) {
        final MethodInvocation newAssert = invokeMethod(b, originalMethod, getAssertName(isAssertEquals, "Equals"), b.copy(actualValue), b.copy(expectedValue), failureMessage);
        r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, newAssert));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 3 with Refactorings

use of org.autorefactor.refactoring.Refactorings 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 4 with Refactorings

use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.

the class AbstractUnitTestRefactoring method refactorToAssertTrueOrFalse.

private boolean refactorToAssertTrueOrFalse(final ASTNode nodeToReplace, final MethodInvocation originalMethod, final Expression failureMessage, final Expression condition, final boolean isAssertTrue) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Refactorings r = this.ctx.getRefactorings();
    final String methodName = isAssertTrue ? "assertTrue" : "assertFalse";
    r.replace(nodeToReplace, invokeMethodOrStatement(nodeToReplace, b, invokeMethod(b, originalMethod, methodName, b.copy(condition), null, failureMessage)));
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 5 with Refactorings

use of org.autorefactor.refactoring.Refactorings in project AutoRefactor by JnRouvignac.

the class VectorOldToNewAPIRefactoring method replaceWithAndSwapArguments.

private void replaceWithAndSwapArguments(final MethodInvocation node, final String newMethodName) {
    final List<Expression> args = arguments(node);
    assertSize(args, 2);
    final Expression arg1 = args.get(1);
    final ASTBuilder b = ctx.getASTBuilder();
    final Refactorings r = ctx.getRefactorings();
    r.set(node, NAME_PROPERTY, b.simpleName(newMethodName));
    r.moveToIndex(arg1, 0, b.move(arg1));
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) Refactorings(org.autorefactor.refactoring.Refactorings) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Aggregations

Refactorings (org.autorefactor.refactoring.Refactorings)17 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)14 Expression (org.eclipse.jdt.core.dom.Expression)6 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)4 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)4 List (java.util.List)3 ArrayList (java.util.ArrayList)2 ASTMatcher (org.eclipse.jdt.core.dom.ASTMatcher)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 IfStatement (org.eclipse.jdt.core.dom.IfStatement)2 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)2 Statement (org.eclipse.jdt.core.dom.Statement)2 LinkedList (java.util.LinkedList)1 ListIterator (java.util.ListIterator)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Matcher (java.util.regex.Matcher)1 ASTHelper.hasOperator (org.autorefactor.refactoring.ASTHelper.hasOperator)1 ASTMatcherSameVariablesAndMethods (org.autorefactor.refactoring.ASTMatcherSameVariablesAndMethods)1 ArrayInitializer (org.eclipse.jdt.core.dom.ArrayInitializer)1 Block (org.eclipse.jdt.core.dom.Block)1