Search in sources :

Example 51 with MethodInvocation

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

the class MethodReturnsInternalArrayQuickFix method apply.

@Override
protected boolean apply(final ReturnStatement node) {
    final Expression expression = node.getExpression();
    final AST ast = expression.getAST();
    final MethodInvocation replacement = create(ast, MethodInvocation.class);
    replacement.setExpression(copy(expression));
    final SimpleName name = create(ast, SimpleName.class);
    name.setIdentifier("clone");
    replacement.setName(name);
    return replace(expression, replacement);
}
Also used : AST(org.eclipse.jdt.core.dom.AST) Expression(org.eclipse.jdt.core.dom.Expression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 52 with MethodInvocation

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

the class UseCollectionIsEmptyQuickFix method apply.

/**
 * Replaces {@code x.size() == 0} or {@code 0 == x.size()} with {@code x.isEmpty()}. Replaces {@code x.size() != 0}
 * or {@code 0 != x.size()} with {@code !x.isEmpty()}.
 */
@Override
protected boolean apply(final InfixExpression node) {
    final MethodInvocation size;
    if (node.getLeftOperand() instanceof MethodInvocation) {
        size = (MethodInvocation) node.getLeftOperand();
    } else if (node.getRightOperand() instanceof MethodInvocation) {
        size = (MethodInvocation) node.getRightOperand();
    } else {
        return false;
    }
    final AST ast = node.getAST();
    final MethodInvocation invocation = (MethodInvocation) ast.createInstance(MethodInvocation.class);
    invocation.setExpression(ASTUtil.copy(size.getExpression()));
    final SimpleName isEmpty = (SimpleName) ast.createInstance(SimpleName.class);
    isEmpty.setIdentifier("isEmpty");
    invocation.setName(isEmpty);
    final Expression replacement;
    if (isNotEmpty(node)) {
        final PrefixExpression not = (PrefixExpression) ast.createInstance(PrefixExpression.class);
        not.setOperator(org.eclipse.jdt.core.dom.PrefixExpression.Operator.NOT);
        not.setOperand(invocation);
        replacement = not;
    } else {
        replacement = invocation;
    }
    ASTUtil.replace(node, replacement);
    return true;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 53 with MethodInvocation

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

the class ByteInstantiationValueOfQuickFix method apply.

/**
 * Replaces the Byte instantiation with its argument, e.g. {@code new Byte(123 + x)} with
 * {@code Byte.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("Byte"));
    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)

Example 54 with MethodInvocation

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

the class LongInstantiationValueOfQuickFix method apply.

/**
 * Replaces the Long instantiation with its argument, e.g. {@code new Long(123 + x)} with
 * {@code Long.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("Long"));
    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)

Example 55 with MethodInvocation

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

the class ShortInstantiationValueOfQuickFix method apply.

/**
 * Replaces the Short instantiation with its argument, e.g. {@code new Short(123 + x)} with
 * {@code Short.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("Short"));
    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