Search in sources :

Example 1 with ASTBuilder

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

the class AbstractUnitTestRefactoring method invokeAssertNull.

private MethodInvocation invokeAssertNull(final MethodInvocation originalMethod, final boolean isPositive, final Expression actual, final Expression failureMessage) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final String methodName = getAssertName(isPositive, "Null");
    final Expression copyOfActual = b.copy(actual);
    return invokeMethod(b, originalMethod, methodName, copyOfActual, null, failureMessage);
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 2 with ASTBuilder

use of org.autorefactor.refactoring.ASTBuilder 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 3 with ASTBuilder

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

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

the class InvertEqualsRefactoring method invertEqualsInvocation.

private void invertEqualsInvocation(final MethodInvocation node, final boolean isEquals, final Expression expr, final Expression arg0) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final String methodName = isEquals ? "equals" : "equalsIgnoreCase";
    this.ctx.getRefactorings().replace(node, b.invoke(b.parenthesizeIfNeeded(b.copy(arg0)), methodName, b.copy(expr)));
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 5 with ASTBuilder

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

the class LazyLogicalRatherThanEagerRefactoring method visit.

@Override
public boolean visit(InfixExpression node) {
    if (!node.hasExtendedOperands() && (hasType(node.getLeftOperand(), "boolean") || hasType(node.getLeftOperand(), "java.lang.Boolean")) && (hasType(node.getRightOperand(), "boolean") || hasType(node.getRightOperand(), "java.lang.Boolean")) && isPassive(node.getRightOperand()) && (Operator.AND.equals(node.getOperator()) || Operator.OR.equals(node.getOperator()))) {
        final ASTBuilder b = ctx.getASTBuilder();
        ctx.getRefactorings().replace(node, b.infixExpr(b.copy(node.getLeftOperand()), getLazyOperator(node.getOperator()), b.copy(node.getRightOperand())));
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Aggregations

ASTBuilder (org.autorefactor.refactoring.ASTBuilder)44 Expression (org.eclipse.jdt.core.dom.Expression)16 Refactorings (org.autorefactor.refactoring.Refactorings)14 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)10 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)8 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)7 IfStatement (org.eclipse.jdt.core.dom.IfStatement)5 ASTMatcher (org.eclipse.jdt.core.dom.ASTMatcher)4 Statement (org.eclipse.jdt.core.dom.Statement)4 List (java.util.List)3 Block (org.eclipse.jdt.core.dom.Block)3 Type (org.eclipse.jdt.core.dom.Type)3 ArrayList (java.util.ArrayList)2 ASTHelper.hasType (org.autorefactor.refactoring.ASTHelper.hasType)2 ASTNode (org.eclipse.jdt.core.dom.ASTNode)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 Name (org.eclipse.jdt.core.dom.Name)2 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)2 LinkedList (java.util.LinkedList)1 ListIterator (java.util.ListIterator)1