Search in sources :

Example 96 with MethodInvocation

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

the class ObsoleteValueOfRatherThanInstantiationCleanUp method replaceFloatWithFloatValue.

private void replaceFloatWithFloatValue(final ClassInstanceCreation visited, final Expression arg0) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteValueOfRatherThanInstantiationCleanUp_description);
    MethodInvocation floatValueMethod = ast.newMethodInvocation();
    floatValueMethod.setExpression(ASTNodes.createMoveTarget(rewrite, arg0));
    // $NON-NLS-1$
    floatValueMethod.setName(ast.newSimpleName("floatValue"));
    ASTNodes.replaceButKeepComment(rewrite, visited, floatValueMethod, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Example 97 with MethodInvocation

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

the class OppositeComparisonRatherThanNegativeExpressionCleanUp method visit.

@Override
public boolean visit(final PrefixExpression visited) {
    if (ASTNodes.hasOperator(visited, PrefixExpression.Operator.MINUS)) {
        MethodInvocation methodInvocation = ASTNodes.as(visited.getOperand(), MethodInvocation.class);
        if (methodInvocation != null && methodInvocation.getExpression() != null && methodInvocation.arguments().size() == 1) {
            Expression argument = (Expression) methodInvocation.arguments().get(0);
            String[] classes = { Double.class.getCanonicalName(), Float.class.getCanonicalName(), Short.class.getCanonicalName(), Integer.class.getCanonicalName(), Long.class.getCanonicalName(), Character.class.getCanonicalName(), Byte.class.getCanonicalName(), Boolean.class.getCanonicalName() };
            for (String klass : classes) {
                if (ASTNodes.usesGivenSignature(methodInvocation, klass, "compareTo", klass)) {
                    // $NON-NLS-1$
                    if (ASTNodes.hasType(argument, klass)) {
                        reverseObjects(visited, methodInvocation);
                        return false;
                    }
                    return true;
                }
            }
        }
    }
    return true;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 98 with MethodInvocation

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

the class ObsoleteJava7HashRatherThanEclipseJava6HashCleanUp method isDoubleToLongBitsMethod.

private SimpleName isDoubleToLongBitsMethod(final CollectedData data, final Expression initializer) {
    SimpleName fieldToFind = null;
    MethodInvocation doubleToLongBits = ASTNodes.as(initializer, MethodInvocation.class);
    if (doubleToLongBits != null && ASTNodes.usesGivenSignature(doubleToLongBits, Double.class.getCanonicalName(), "doubleToLongBits", double.class.getSimpleName())) {
        // $NON-NLS-1$
        SimpleName fieldName = ASTNodes.as((Expression) doubleToLongBits.arguments().get(0), SimpleName.class);
        if (fieldName != null && !ASTNodes.isSameVariable(fieldName, data.getPrimeId()) && !ASTNodes.isSameVariable(fieldName, data.getResultId())) {
            fieldToFind = fieldName;
        }
    }
    return fieldToFind;
}
Also used : SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 99 with MethodInvocation

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

the class ObsoleteSerializeRatherThanBoxingAndSerializeCleanUp method refactor.

private void refactor(final MethodInvocation visited, final Expression primitiveValue, final Class<?> wrapperClass) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteSerializeRatherThanBoxingAndSerializeCleanUp_description);
    MethodInvocation toStringMethod = ast.newMethodInvocation();
    toStringMethod.setExpression(ast.newSimpleName(wrapperClass.getSimpleName()));
    // $NON-NLS-1$
    toStringMethod.setName(ast.newSimpleName("toString"));
    toStringMethod.arguments().add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(primitiveValue)));
    ASTNodes.replaceButKeepComment(rewrite, visited, toStringMethod, group);
}
Also used : ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

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

Aggregations

MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)283 Expression (org.eclipse.jdt.core.dom.Expression)124 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)69 ASTNode (org.eclipse.jdt.core.dom.ASTNode)66 SimpleName (org.eclipse.jdt.core.dom.SimpleName)63 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)54 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)52 ArrayList (java.util.ArrayList)48 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)48 ASTNodeFactory (org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory)47 CastExpression (org.eclipse.jdt.core.dom.CastExpression)43 Block (org.eclipse.jdt.core.dom.Block)39 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)39 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)38 AST (org.eclipse.jdt.core.dom.AST)37 ClassInstanceCreation (org.eclipse.jdt.core.dom.ClassInstanceCreation)37 VariableDeclarationFragment (org.eclipse.jdt.core.dom.VariableDeclarationFragment)37 TextEditGroup (org.eclipse.text.edits.TextEditGroup)37 PrefixExpression (org.eclipse.jdt.core.dom.PrefixExpression)36 Name (org.eclipse.jdt.core.dom.Name)33