Search in sources :

Example 26 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project AutoRefactor by JnRouvignac.

the class ObsoleteLambdaCleanUp method visit.

@Override
public boolean visit(final LambdaExpression node) {
    if (node.hasParentheses() && node.parameters().size() == 1 && node.parameters().get(0) instanceof VariableDeclarationFragment) {
        // TODO it should also be possible to deal with a SingleVariableDeclaration
        // when the type matches the expected inferred type
        // To do this, we should visit the whole block and check the target type
        removeParamParentheses(node);
        return false;
    }
    if (node.getBody() instanceof Block) {
        List<Statement> statements = ASTNodes.asList((Block) node.getBody());
        if (statements.size() == 1 && statements.get(0) instanceof ReturnStatement) {
            removeReturnAndBrackets(node, statements);
            return false;
        }
    } else if (node.getBody() instanceof ClassInstanceCreation) {
        ClassInstanceCreation ci = (ClassInstanceCreation) node.getBody();
        List<Expression> arguments = ci.arguments();
        if (ci.resolveTypeBinding() != null && ci.getAnonymousClassDeclaration() == null && node.parameters().size() == arguments.size() && areSameIdentifiers(node, arguments)) {
            replaceByCreationReference(node, ci);
            return false;
        }
    } else if (node.getBody() instanceof SuperMethodInvocation) {
        SuperMethodInvocation smi = (SuperMethodInvocation) node.getBody();
        List<Expression> arguments = smi.arguments();
        if (node.parameters().size() == arguments.size() && areSameIdentifiers(node, arguments)) {
            replaceBySuperMethodReference(node, smi);
            return false;
        }
    } else if (node.getBody() instanceof MethodInvocation) {
        MethodInvocation methodInvocation = (MethodInvocation) node.getBody();
        Expression calledExpression = methodInvocation.getExpression();
        ITypeBinding calledType = ASTNodes.getCalledType(methodInvocation);
        List<Expression> arguments = methodInvocation.arguments();
        if (node.parameters().size() == arguments.size()) {
            if (!areSameIdentifiers(node, arguments)) {
                return true;
            }
            if (isStaticMethod(methodInvocation)) {
                if (!arguments.isEmpty()) {
                    String[] remainingParams = new String[arguments.size() - 1];
                    for (int i = 0; i < arguments.size() - 1; i++) {
                        ITypeBinding argumentBinding = arguments.get(i + 1).resolveTypeBinding();
                        if (argumentBinding == null) {
                            return true;
                        }
                        remainingParams[i] = argumentBinding.getQualifiedName();
                    }
                    for (IMethodBinding methodBinding : calledType.getDeclaredMethods()) {
                        if ((methodBinding.getModifiers() & Modifier.STATIC) == 0 && ASTNodes.usesGivenSignature(methodBinding, calledType.getQualifiedName(), methodInvocation.getName().getIdentifier(), remainingParams)) {
                            return true;
                        }
                    }
                }
                replaceByTypeReference(node, methodInvocation);
                return false;
            }
            if (calledExpression == null || calledExpression instanceof StringLiteral || calledExpression instanceof NumberLiteral || calledExpression instanceof ThisExpression) {
                replaceByMethodReference(node, methodInvocation);
                return false;
            }
            if (calledExpression instanceof FieldAccess) {
                FieldAccess fieldAccess = (FieldAccess) calledExpression;
                if (fieldAccess.resolveFieldBinding().isEffectivelyFinal()) {
                    replaceByMethodReference(node, methodInvocation);
                    return false;
                }
            } else if (calledExpression instanceof SuperFieldAccess) {
                SuperFieldAccess fieldAccess = (SuperFieldAccess) calledExpression;
                if (fieldAccess.resolveFieldBinding().isEffectivelyFinal()) {
                    replaceByMethodReference(node, methodInvocation);
                    return false;
                }
            }
        } else if (calledExpression instanceof SimpleName && node.parameters().size() == arguments.size() + 1) {
            SimpleName calledObject = (SimpleName) calledExpression;
            if (isSameIdentifier(node, 0, calledObject)) {
                for (int i = 0; i < arguments.size(); i++) {
                    SimpleName expression = ASTNodes.as(arguments.get(i), SimpleName.class);
                    if (expression == null || !isSameIdentifier(node, i + 1, expression)) {
                        return true;
                    }
                }
                ITypeBinding klass = calledExpression.resolveTypeBinding();
                if (klass == null) {
                    return true;
                }
                String[] remainingParams = new String[arguments.size() + 1];
                remainingParams[0] = klass.getQualifiedName();
                for (int i = 0; i < arguments.size(); i++) {
                    ITypeBinding argumentBinding = arguments.get(i).resolveTypeBinding();
                    if (argumentBinding == null) {
                        return true;
                    }
                    remainingParams[i + 1] = argumentBinding.getQualifiedName();
                }
                for (IMethodBinding methodBinding : klass.getDeclaredMethods()) {
                    if ((methodBinding.getModifiers() & Modifier.STATIC) != 0 && ASTNodes.usesGivenSignature(methodBinding, klass.getQualifiedName(), methodInvocation.getName().getIdentifier(), remainingParams)) {
                        return true;
                    }
                }
                replaceByTypeReference(node, methodInvocation);
                return false;
            }
        }
    }
    return true;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) List(java.util.List) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess) SuperFieldAccess(org.eclipse.jdt.core.dom.SuperFieldAccess) NumberLiteral(org.eclipse.jdt.core.dom.NumberLiteral)

Example 27 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project AutoRefactor by JnRouvignac.

the class LogParametersRatherThanLogMessageCleanUp method maybeReplaceConcatenation.

private boolean maybeReplaceConcatenation(final MethodInvocation visited, final String methodName, final InfixExpression message) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    StringBuilder messageBuilder = new StringBuilder();
    List<Expression> allOperands = ASTNodes.allOperands(message);
    List<Expression> params = new ArrayList<>(allOperands.size());
    boolean hasLiteral = false;
    boolean hasObjects = false;
    for (Expression string : allOperands) {
        if (string instanceof StringLiteral) {
            hasLiteral = true;
            String literal = (String) string.resolveConstantExpressionValue();
            // Due to a bug in JDT Core, the literal may be null
            if (literal == null || literal.contains("{") || literal.contains("}")) {
                // $NON-NLS-1$ //$NON-NLS-2$
                return true;
            }
            messageBuilder.append(literal);
        } else {
            hasObjects = true;
            // $NON-NLS-1$
            messageBuilder.append("{}");
            if (ASTNodes.hasType(string, Throwable.class.getCanonicalName())) {
                MethodInvocation valueOfMethod = ast.newMethodInvocation();
                valueOfMethod.setExpression(ASTNodeFactory.newName(ast, String.class.getSimpleName()));
                // $NON-NLS-1$
                valueOfMethod.setName(ast.newSimpleName("valueOf"));
                valueOfMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(string)));
                MethodInvocation newMethodInvocation = valueOfMethod;
                params.add(newMethodInvocation);
            } else {
                params.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(string)));
            }
        }
    }
    if (hasLiteral && hasObjects) {
        replaceConcatenation(visited, methodName, messageBuilder, params);
        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) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ArrayList(java.util.ArrayList) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 28 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project AutoRefactor by JnRouvignac.

the class ObsoleteStringRatherThanNewStringCleanUp method visit.

@Override
public boolean visit(final ClassInstanceCreation node) {
    if (ASTNodes.hasType(node, String.class.getCanonicalName()) && node.arguments().size() == 1) {
        Expression arg0 = (Expression) node.arguments().get(0);
        if (ASTNodes.hasType(arg0, String.class.getCanonicalName()) && (arg0 instanceof StringLiteral || arg0 instanceof InfixExpression)) {
            ASTRewrite rewrite = cuRewrite.getASTRewrite();
            ASTNodeFactory ast = cuRewrite.getASTBuilder();
            TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteStringRatherThanNewStringCleanUp_description);
            ASTNodes.replaceButKeepComment(rewrite, node, ASTNodeFactory.parenthesizeIfNeeded(ast, ASTNodes.createMoveTarget(rewrite, arg0)), group);
            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) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 29 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)

Example 30 with StringLiteral

use of org.eclipse.jdt.core.dom.StringLiteral in project AutoRefactor by JnRouvignac.

the class ASTNodeFactory method newStringLiteral.

/**
 * Builds a new {@link StringLiteral} instance.
 *
 * @param s the string literal value
 * @return a new string literal
 */
public StringLiteral newStringLiteral(final String s) {
    StringLiteral sl = ast.newStringLiteral();
    sl.setLiteralValue(s);
    return sl;
}
Also used : StringLiteral(org.eclipse.jdt.core.dom.StringLiteral)

Aggregations

StringLiteral (org.eclipse.jdt.core.dom.StringLiteral)54 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)20 Expression (org.eclipse.jdt.core.dom.Expression)16 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)16 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 CastExpression (org.eclipse.jdt.core.dom.CastExpression)11 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 SimpleName (org.eclipse.jdt.core.dom.SimpleName)7 CharacterLiteral (org.eclipse.jdt.core.dom.CharacterLiteral)6 NumberLiteral (org.eclipse.jdt.core.dom.NumberLiteral)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 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)5 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)5 Name (org.eclipse.jdt.core.dom.Name)5