use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class StringRefactoring method visit.
@Override
public boolean visit(MethodInvocation node) {
final Expression expr = node.getExpression();
final ASTNode parent = node.getParent();
final ASTBuilder b = this.ctx.getASTBuilder();
final Refactorings r = ctx.getRefactorings();
final boolean isStringValueOf = isStringValueOf(node);
if (isMethod(node, "java.lang.Object", "toString")) {
if (hasType(expr, "java.lang.String")) {
// if node is already a String, no need to call toString()
r.replace(node, b.move(expr));
return DO_NOT_VISIT_SUBTREE;
} else if (parent.getNodeType() == INFIX_EXPRESSION) {
// if node is in a String context, no need to call toString()
final InfixExpression ie = (InfixExpression) node.getParent();
final Expression leftOp = ie.getLeftOperand();
final Expression rightOp = ie.getRightOperand();
final boolean leftOpIsString = hasType(leftOp, "java.lang.String");
final boolean rightOpIsString = hasType(rightOp, "java.lang.String");
final MethodInvocation lmi = as(leftOp, MethodInvocation.class);
final MethodInvocation rmi = as(rightOp, MethodInvocation.class);
if (!node.equals(lmi) && !node.equals(rmi) && (leftOpIsString || rightOpIsString)) {
// node is in the extended operands
r.replace(node, replaceToString(node.getExpression()));
return DO_NOT_VISIT_SUBTREE;
} else if (leftOpIsString && isMethod(rmi, "java.lang.Object", "toString")) {
r.replace(rmi, replaceToString(rmi.getExpression()));
return DO_NOT_VISIT_SUBTREE;
} else if (rightOpIsString && node.equals(lmi)) {
r.replace(lmi, replaceToString(lmi.getExpression()));
return DO_NOT_VISIT_SUBTREE;
}
}
} else if (isStringValueOf && hasType(arg0(node), "java.lang.String")) {
r.replace(node, b.move(arg0(node)));
return DO_NOT_VISIT_SUBTREE;
} else if ((isToStringForPrimitive(node) || isStringValueOf) && parent.getNodeType() == INFIX_EXPRESSION) {
// if node is in a String context, no need to call toString()
final InfixExpression ie = (InfixExpression) node.getParent();
final Expression lo = ie.getLeftOperand();
final Expression ro = ie.getRightOperand();
if (node.equals(lo)) {
if (hasType(ro, "java.lang.String")) {
replaceStringValueOfByArg0(lo, node);
return DO_NOT_VISIT_SUBTREE;
}
} else if (node.equals(ro)) {
if (hasType(lo, "java.lang.String") && // to avoid compilation errors post refactoring
!r.hasBeenRefactored(lo)) {
replaceStringValueOfByArg0(ro, node);
return DO_NOT_VISIT_SUBTREE;
}
} else {
// left or right operation is necessarily a string, so just replace
replaceStringValueOfByArg0(node, node);
return DO_NOT_VISIT_SUBTREE;
}
} else if (isMethod(node, "java.lang.String", "equals", "java.lang.Object")) {
final MethodInvocation leftInvocation = as(node.getExpression(), MethodInvocation.class);
final MethodInvocation rightInvocation = as(arg0(node), MethodInvocation.class);
if (leftInvocation != null && rightInvocation != null && ((isMethod(leftInvocation, "java.lang.String", "toLowerCase") && isMethod(rightInvocation, "java.lang.String", "toLowerCase")) || (isMethod(leftInvocation, "java.lang.String", "toUpperCase") && isMethod(rightInvocation, "java.lang.String", "toUpperCase")))) {
final Expression leftExpr = leftInvocation.getExpression();
final Expression rightExpr = rightInvocation.getExpression();
r.replace(node, b.invoke(b.copy(leftExpr), "equalsIgnoreCase", b.copy(rightExpr)));
return DO_NOT_VISIT_SUBTREE;
}
} else if (isMethod(node, "java.lang.String", "equalsIgnoreCase", "java.lang.String")) {
final AtomicBoolean isRefactoringNeeded = new AtomicBoolean(false);
final Expression leftExpr = getReducedStringExpression(node.getExpression(), isRefactoringNeeded);
final Expression rightExpr = getReducedStringExpression(arg0(node), isRefactoringNeeded);
if (isRefactoringNeeded.get()) {
r.replace(node, b.invoke(b.copy(leftExpr), "equalsIgnoreCase", b.copy(rightExpr)));
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class StringRefactoring method replaceStringValueOfByArg0.
private void replaceStringValueOfByArg0(final Expression toReplace, final MethodInvocation mi) {
final ASTBuilder b = this.ctx.getASTBuilder();
final ITypeBinding expectedType = mi.resolveMethodBinding().getParameterTypes()[0];
final ITypeBinding actualType = arg0(mi).resolveTypeBinding();
if (!expectedType.equals(actualType) && !getBoxedTypeBinding(expectedType, mi.getAST()).equals(actualType)) {
ctx.getRefactorings().replace(toReplace, b.cast(b.type(expectedType.getQualifiedName()), b.move(arg0(mi))));
} else {
ctx.getRefactorings().replace(toReplace, b.parenthesizeIfNeeded(b.move(arg0(mi))));
}
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class TryWithResourceRefactoring method collapseTryStatements.
private boolean collapseTryStatements(TryStatement outerTryStmt, TryStatement innerTryStmt) {
final Refactorings r = ctx.getRefactorings();
final ASTBuilder b = ctx.getASTBuilder();
r.insertLast(outerTryStmt, TryStatement.RESOURCES_PROPERTY, b.copyRange(resources(innerTryStmt)));
r.replace(innerTryStmt, b.move(innerTryStmt.getBody()));
return DO_NOT_VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class TernaryOperatorRatherThanDuplicateConditionsRefactoring method maybeReplaceDuplicateExpr.
private boolean maybeReplaceDuplicateExpr(final InfixExpression node, final Expression firstExpr, final Expression secondExpr, final Expression thirdExpr, final boolean isFirstExprPositive) {
if (!match(new ASTMatcher(), secondExpr, thirdExpr)) {
final ASTBuilder b = ctx.getASTBuilder();
final Expression thenExpr;
final Expression elseExpr;
if (isFirstExprPositive) {
thenExpr = secondExpr;
elseExpr = thirdExpr;
} else {
thenExpr = thirdExpr;
elseExpr = secondExpr;
}
ctx.getRefactorings().replace(node, b.conditionalExpr(b.copy(firstExpr), b.copy(thenExpr), b.copy(elseExpr)));
return DO_NOT_VISIT_SUBTREE;
}
return VISIT_SUBTREE;
}
use of org.autorefactor.refactoring.ASTBuilder in project AutoRefactor by JnRouvignac.
the class StringRatherThanNewStringRefactoring method visit.
@Override
public boolean visit(ClassInstanceCreation node) {
if (hasType(node, "java.lang.String") && arguments(node).size() == 1) {
final Expression arg0 = arguments(node).get(0);
if (hasType(arg0, "java.lang.String")) {
final ASTBuilder b = ctx.getASTBuilder();
ctx.getRefactorings().replace(node, b.parenthesizeIfNeeded(b.copy(arg0)));
return DO_NOT_VISIT_SUBTREE;
}
}
return VISIT_SUBTREE;
}
Aggregations