Search in sources :

Example 91 with MethodInvocation

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

the class StringBuilderMethodRatherThanReassignationRefactoring method visit.

@Override
public boolean visit(Assignment node) {
    final Expression targetVar = node.getLeftHandSide();
    Expression var = node.getRightHandSide();
    if (ASSIGN.equals(node.getOperator()) && hasType(targetVar, "java.lang.StringBuffer", "java.lang.StringBuilder") && var instanceof MethodInvocation) {
        var = getVar(var);
        if (isSameVariable(targetVar, var)) {
            final ASTBuilder b = this.ctx.getASTBuilder();
            ctx.getRefactorings().replace(node, b.copy(node.getRightHandSide()));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 92 with MethodInvocation

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

the class StringBuilderRefactoring method replaceWithAppendSubstring.

private void replaceWithAppendSubstring(final MethodInvocation node, final MethodInvocation embeddedMI) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final Expression stringVar = b.copy(embeddedMI.getExpression());
    final List<Expression> args = arguments(embeddedMI);
    final Expression arg0 = b.copy(args.get(0));
    final Expression arg1 = b.copy(args.get(1));
    final Expression lastExpr = b.copy(node.getExpression());
    MethodInvocation newAppendSubstring = null;
    if (arg1 == null) {
        newAppendSubstring = b.invoke(lastExpr, "append", stringVar, arg0);
    } else {
        newAppendSubstring = b.invoke(lastExpr, "append", stringVar, arg0, arg1);
    }
    this.ctx.getRefactorings().replace(node, newAppendSubstring);
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 93 with MethodInvocation

use of org.eclipse.jdt.core.dom.MethodInvocation in project eclipse-pmd by acanda.

the class UnnecessaryCaseChangeQuickFix method apply.

/**
 * Removes the <code>.toString()</code> from <code>"foo".toString()</code> if the expression is only a part of an
 * statement. Removes the expression completely if it is the whole statement.
 */
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final MethodInvocation node) {
    final AST ast = node.getAST();
    final MethodInvocation invocation = ast.newMethodInvocation();
    if (node.getExpression().getNodeType() == ASTNode.METHOD_INVOCATION) {
        invocation.setExpression(removeCaseChange((MethodInvocation) node.getExpression()));
        invocation.setName(ast.newSimpleName("equalsIgnoreCase"));
        invocation.arguments().add(copy((Expression) node.arguments().get(0)));
        return replace(node, invocation);
    }
    return false;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 94 with MethodInvocation

use of org.eclipse.jdt.core.dom.MethodInvocation in project eclipse-pmd by acanda.

the class AddEmptyStringQuickFix method apply.

@Override
@SuppressWarnings("unchecked")
protected boolean apply(final InfixExpression node) {
    final Expression rightOperand = node.getRightOperand();
    // "" + x.toString() -> x.toString()
    if (isString(rightOperand)) {
        return replace(node, copy(rightOperand));
    }
    // "" + 'a' -> "a"
    if (isCharacterLiteral(rightOperand)) {
        final AST ast = node.getAST();
        final StringLiteral stringLiteral = ast.newStringLiteral();
        final String escapedCharacter = ((CharacterLiteral) rightOperand).getEscapedValue();
        stringLiteral.setEscapedValue(convertToEscapedString(escapedCharacter));
        return replace(node, stringLiteral);
    }
    // "" + x -> String.valueOf(x)
    final AST ast = node.getAST();
    final MethodInvocation toString = ast.newMethodInvocation();
    toString.setExpression(ast.newSimpleName("String"));
    toString.setName(ast.newSimpleName("valueOf"));
    toString.arguments().add(copy(rightOperand));
    return replace(node, toString);
}
Also used : AST(org.eclipse.jdt.core.dom.AST) StringLiteral(org.eclipse.jdt.core.dom.StringLiteral) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) CharacterLiteral(org.eclipse.jdt.core.dom.CharacterLiteral)

Example 95 with MethodInvocation

use of org.eclipse.jdt.core.dom.MethodInvocation in project eclipse-pmd by acanda.

the class IntegerInstantiationValueOfQuickFix method apply.

/**
 * Replaces the Integer instantiation with its argument, e.g. {@code new Integer(123 + x)} with
 * {@code Integer.valueOf(123 + x)}.
 */
@Override
@SuppressWarnings("unchecked")
protected boolean apply(final ClassInstanceCreation node) {
    final AST ast = node.getAST();
    final MethodInvocation invocation = ast.newMethodInvocation();
    invocation.setExpression(ast.newSimpleName("Integer"));
    invocation.setName(ast.newSimpleName("valueOf"));
    invocation.arguments().add(copy((Expression) node.arguments().get(0)));
    replace(node, invocation);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Aggregations

MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)95 Expression (org.eclipse.jdt.core.dom.Expression)53 ASTNode (org.eclipse.jdt.core.dom.ASTNode)34 SimpleName (org.eclipse.jdt.core.dom.SimpleName)31 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)27 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)25 AST (org.eclipse.jdt.core.dom.AST)23 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)19 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)19 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)18 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)16 CastExpression (org.eclipse.jdt.core.dom.CastExpression)15 IBinding (org.eclipse.jdt.core.dom.IBinding)14 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)13 Name (org.eclipse.jdt.core.dom.Name)13 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)13 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)13 Type (org.eclipse.jdt.core.dom.Type)13 ArrayList (java.util.ArrayList)12 ASTBuilder (org.autorefactor.refactoring.ASTBuilder)12