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