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;
}
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);
}
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;
}
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));
}
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;
}
Aggregations