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