use of org.eclipse.jdt.core.dom.StringLiteral in project AutoRefactor by JnRouvignac.
the class StringCleanUp method visit.
@Override
public boolean visit(final MethodInvocation visited) {
ASTNode parent = visited.getParent();
boolean isStringValueOf = isStringValueOf(visited);
if (ASTNodes.usesGivenSignature(visited, Object.class.getCanonicalName(), "toString")) {
// $NON-NLS-1$
Expression stringExpression = visited.getExpression();
if (ASTNodes.hasType(stringExpression, String.class.getCanonicalName())) {
// If node is already a String, no need to call toString()
removeToString(visited);
return false;
}
if (parent instanceof InfixExpression && ASTNodes.hasOperator((InfixExpression) parent, InfixExpression.Operator.PLUS)) {
// If node is in a String context, no need to call toString()
InfixExpression infixExpression = (InfixExpression) parent;
Expression leftOperand = infixExpression.getLeftOperand();
Expression rightOperand = infixExpression.getRightOperand();
boolean leftOperandIsString = ASTNodes.hasType(leftOperand, String.class.getCanonicalName());
boolean rightOperandIsString = ASTNodes.hasType(rightOperand, String.class.getCanonicalName());
MethodInvocation lmi = ASTNodes.as(leftOperand, MethodInvocation.class);
MethodInvocation rmi = ASTNodes.as(rightOperand, MethodInvocation.class);
if ((leftOperandIsString || rightOperandIsString) && visited.getLocationInParent() != InfixExpression.LEFT_OPERAND_PROPERTY && visited.getLocationInParent() != InfixExpression.RIGHT_OPERAND_PROPERTY) {
// Node is in the extended operands
removeToString(visited);
return false;
}
if (leftOperandIsString && ASTNodes.usesGivenSignature(rmi, Object.class.getCanonicalName(), "toString")) {
// $NON-NLS-1$
removeToString(rmi);
return false;
}
if (rightOperandIsString && visited.getLocationInParent() == InfixExpression.LEFT_OPERAND_PROPERTY) {
removeToString(lmi);
return false;
}
}
} else if (isStringValueOf && ASTNodes.hasType((Expression) visited.arguments().get(0), String.class.getCanonicalName()) && (visited.arguments().get(0) instanceof StringLiteral || visited.arguments().get(0) instanceof InfixExpression)) {
removeValueOf(visited);
return false;
}
return true;
}
Aggregations