Search in sources :

Example 51 with StringLiteral

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;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTNode(org.eclipse.jdt.core.dom.ASTNode) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Aggregations

StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)51 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)20 Expression (org.eclipse.jdt.core.dom.Expression)16 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)14 CastExpression (org.eclipse.jdt.core.dom.CastExpression)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)10 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)8 AST (org.eclipse.jdt.core.dom.AST)7 MemberValuePair (org.eclipse.jdt.core.dom.MemberValuePair)7 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)6 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)6 SimpleName (org.eclipse.jdt.core.dom.SimpleName)6 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)6 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)6 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)6 ArrayList (java.util.ArrayList)5 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 Name (org.eclipse.jdt.core.dom.Name)5 NormalAnnotation (org.eclipse.jdt.core.dom.NormalAnnotation)5