Search in sources :

Example 41 with MethodInvocation

use of org.eclipse.jdt.core.dom.MethodInvocation 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 42 with MethodInvocation

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

the class AbstractUnitTestRefactoring method maybeRefactorStatement.

/**
 * Maybe refactor the statement.
 *
 * @param nodeToReplace
 *            The node
 * @param originalMethod
 *            The method invocation
 * @param isAssertTrue
 *            True if assertTrue is used, False if assertFalse is used.
 * @param condition
 *            The condition on which the assert is based.
 * @param failureMessage
 *            The failure message or null.
 * @param isRewriteNeeded
 *            True if is the rewriting is needed.
 * @return True if refactored
 */
protected boolean maybeRefactorStatement(final ASTNode nodeToReplace, final MethodInvocation originalMethod, final boolean isAssertTrue, final Expression condition, final Expression failureMessage, final boolean isRewriteNeeded) {
    Expression localCondition = condition;
    boolean localIsAssertTrue = isAssertTrue;
    boolean localIsRewriteNeeded = isRewriteNeeded;
    PrefixExpression localConditionPe = as(localCondition, PrefixExpression.class);
    while (hasOperator(localConditionPe, NOT)) {
        localIsRewriteNeeded = true;
        localIsAssertTrue = !localIsAssertTrue;
        localCondition = as(localConditionPe.getOperand(), Expression.class);
        localConditionPe = as(localCondition, PrefixExpression.class);
    }
    final InfixExpression conditionIe = as(localCondition, InfixExpression.class);
    final MethodInvocation conditionMi = as(localCondition, MethodInvocation.class);
    final Object constantValue = localCondition.resolveConstantExpressionValue();
    return maybeRefactorAssertTrueOrFalse(nodeToReplace, originalMethod, localIsAssertTrue, localCondition, conditionIe, conditionMi, constantValue, failureMessage, localIsRewriteNeeded);
}
Also used : InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 43 with MethodInvocation

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

the class AllInOneMethodRatherThanLoopRefactoring method visit.

@Override
public boolean visit(EnhancedForStatement node) {
    final Expression iterable = node.getExpression();
    final List<Statement> stmts = asList(node.getBody());
    if (stmts.size() != 1) {
        return VISIT_SUBTREE;
    }
    final MethodInvocation mi = asExpression(stmts.get(0), MethodInvocation.class);
    final IVariableBinding foreachVariable = node.getParameter().resolveBinding();
    // As we replace only one, there should be no more than one occurrence
    if (getVariableUseCount(foreachVariable, node.getBody()) == 1) {
        if (instanceOf(iterable, "java.util.Collection")) {
            if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object")) {
                return maybeReplaceWithCollectionMethod(node, iterable, "addAll", mi);
            } else if (isMethod(mi, "java.util.Collection", "remove", "java.lang.Object")) {
                return maybeReplaceWithCollectionMethod(node, iterable, "removeAll", mi);
            }
        } else if (isArray(iterable) && isMethod(mi, "java.util.Collection", "add", "java.lang.Object") && areTypeCompatible(mi.getExpression(), iterable) && isSameLocalVariable(foreachVariable, arg0(mi))) {
            replaceWithCollectionsAddAll(node, iterable, mi);
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ASTHelper.asExpression(org.autorefactor.refactoring.ASTHelper.asExpression) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 44 with MethodInvocation

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

the class BigNumberRefactoring method getCompareToNode.

private InfixExpression getCompareToNode(final boolean isPositive, final MethodInvocation node) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final MethodInvocation mi = b.invoke(b.copy(node.getExpression()), "compareTo", b.copy(arg0(node)));
    return b.infixExpr(mi, isPositive ? EQUALS : NOT_EQUALS, b.int0(0));
}
Also used : MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 45 with MethodInvocation

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

the class EnumSetRatherThanHashSetRefactoring method replace.

/**
 * Refactoring is not correct if argument for HashSet constructor is a Collection, but other
 * than EnumSet. <br>
 * In case of empty collection <code>EnumSet.copyOf</code> will throw an
 * <code>IllegalArgumentException</code>, <br>
 * and HashSet(Collection) will not. <br>
 * <br>
 * Other constructors can be replaced with <code>EnumSet.noneOf(Class)</code> method. <br>
 * <br>
 *
 * @see {@link java.util.EnumSet#copyOf(Collection)}
 * @see {@link java.util.EnumSet#copyOf(EnumSet)}
 * @see {@link java.util.EnumSet#noneOf(Class)} <br>
 * @param cic
 *            - class instance creation node to be replaced
 * @param type
 *            - type argument of the declaration
 */
@Override
boolean replace(ClassInstanceCreation cic, Type... types) {
    if (types == null || types.length < 1) {
        return VISIT_SUBTREE;
    }
    Type type = types[0];
    ASTBuilder b = ctx.getASTBuilder();
    List<Expression> arguments = arguments(cic);
    final MethodInvocation invocation;
    if (!arguments.isEmpty() && instanceOf(arguments.get(0), "java.util.Collection")) {
        Expression typeArg = arguments.get(0);
        if (!instanceOf(typeArg, "java.util.EnumSet")) {
            return VISIT_SUBTREE;
        }
        invocation = b.invoke(b.name("java", "util", "EnumSet"), "copyOf", b.copy(typeArg));
    } else {
        TypeLiteral newTypeLiteral = ctx.getAST().newTypeLiteral();
        newTypeLiteral.setType(b.copy(type));
        invocation = b.invoke(b.name("java", "util", "EnumSet"), "noneOf", newTypeLiteral);
    }
    ctx.getRefactorings().replace(cic, invocation);
    return DO_NOT_VISIT_SUBTREE;
}
Also used : Type(org.eclipse.jdt.core.dom.Type) TypeLiteral(org.eclipse.jdt.core.dom.TypeLiteral) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Aggregations

MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)265 Expression (org.eclipse.jdt.core.dom.Expression)112 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)59 ASTNode (org.eclipse.jdt.core.dom.ASTNode)53 SimpleName (org.eclipse.jdt.core.dom.SimpleName)53 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)51 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)48 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)47 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)43 ArrayList (java.util.ArrayList)40 CastExpression (org.eclipse.jdt.core.dom.CastExpression)40 TextEditGroup (org.eclipse.text.edits.TextEditGroup)37 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)34 Block (org.eclipse.jdt.core.dom.Block)33 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)33 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)31 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)31 AST (org.eclipse.jdt.core.dom.AST)30 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)29 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)28