Search in sources :

Example 71 with MethodInvocation

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

the class IsEmptyRatherThanSizeRefactoring method visit.

@Override
public boolean visit(InfixExpression node) {
    final MethodInvocation leftMi = as(node.getLeftOperand(), MethodInvocation.class);
    final Long rightLiteral = asNumber(node.getRightOperand());
    final MethodInvocation rightMi = as(node.getRightOperand(), MethodInvocation.class);
    final Long leftLiteral = asNumber(node.getLeftOperand());
    if ((isMethod(leftMi, "java.util.Collection", "size") || isMethod(leftMi, "java.util.Map", "size")) && rightLiteral != null) {
        return replaceCollectionSize(node, leftMi, sign(node.getOperator(), true), rightLiteral);
    } else if ((isMethod(rightMi, "java.util.Collection", "size") || isMethod(rightMi, "java.util.Map", "size")) && leftLiteral != null) {
        return replaceCollectionSize(node, rightMi, sign(node.getOperator(), false), leftLiteral);
    }
    return VISIT_SUBTREE;
}
Also used : MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 72 with MethodInvocation

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

the class TryWithResourceRefactoring method visit.

@Override
public boolean visit(TryStatement node) {
    final List<Statement> tryStmts = asList(node.getBody());
    if (tryStmts.size() >= 1 && tryStmts.get(0).getNodeType() == TRY_STATEMENT) {
        final TryStatement innerTryStmt = as(tryStmts.get(0), TryStatement.class);
        if (innerTryStmt != null && !innerTryStmt.resources().isEmpty() && innerTryStmt.catchClauses().isEmpty()) {
            return collapseTryStatements(node, innerTryStmt);
        }
    }
    final VariableDeclarationStatement previousDeclStmt = as(getPreviousStatement(node), VariableDeclarationStatement.class);
    if (previousDeclStmt == null) {
        return VISIT_SUBTREE;
    }
    final VariableDeclarationFragment previousDeclFragment = getUniqueFragment(previousDeclStmt);
    final List<Statement> finallyStmts = asList(node.getFinally());
    if (previousDeclFragment != null && finallyStmts.size() >= 1) {
        final List<ASTNode> nodesToRemove = new ArrayList<ASTNode>();
        nodesToRemove.add(previousDeclStmt);
        final Statement finallyStmt = finallyStmts.get(0);
        nodesToRemove.add(finallyStmts.size() == 1 ? node.getFinally() : finallyStmt);
        final ExpressionStatement finallyEs = as(finallyStmt, ExpressionStatement.class);
        final IfStatement finallyIs = as(finallyStmt, IfStatement.class);
        if (finallyEs != null) {
            final MethodInvocation mi = as(finallyEs.getExpression(), MethodInvocation.class);
            if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, mi.getExpression())) {
                final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
                return refactorToTryWithResources(node, newResource, nodesToRemove);
            }
        } else if (finallyIs != null && asList(finallyIs.getThenStatement()).size() == 1 && asList(finallyIs.getElseStatement()).isEmpty()) {
            final Expression nullCheckedExpr = getNullCheckedExpression(finallyIs.getExpression());
            final Statement thenStmt = asList(finallyIs.getThenStatement()).get(0);
            final MethodInvocation mi = asExpression(thenStmt, MethodInvocation.class);
            if (methodClosesCloseables(mi) && areSameVariables(previousDeclFragment, nullCheckedExpr, mi.getExpression())) {
                final VariableDeclarationExpression newResource = newResource(tryStmts, previousDeclStmt, previousDeclFragment, nodesToRemove);
                return refactorToTryWithResources(node, newResource, nodesToRemove);
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) ArrayList(java.util.ArrayList) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IfStatement(org.eclipse.jdt.core.dom.IfStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) Expression(org.eclipse.jdt.core.dom.Expression) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement)

Example 73 with MethodInvocation

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

the class AbstractClassSubstituteRefactoring method replaceClass.

private void replaceClass(final ClassInstanceCreation originalInstanceCreation, final List<VariableDeclaration> variableDecls, final List<MethodInvocation> methodCallsToRefactorAlone, final List<MethodInvocation> methodCallsToRefactorWithVariable) {
    final ASTBuilder b = ctx.getASTBuilder();
    if (variableDecls.isEmpty() && methodCallsToRefactorAlone.isEmpty()) {
        final ClassInstanceCreation newInstanceCreation = b.copySubtree(originalInstanceCreation);
        refactorInstantiation(b, originalInstanceCreation, newInstanceCreation);
        ctx.getRefactorings().replace(originalInstanceCreation, newInstanceCreation);
    } else {
        refactorInstantiation(b, originalInstanceCreation, originalInstanceCreation);
        for (final MethodInvocation methodCall : methodCallsToRefactorAlone) {
            final MethodInvocation copyOfMethodCall = b.copySubtree(methodCall);
            refactorMethod(b, methodCall, copyOfMethodCall);
            ctx.getRefactorings().replace(methodCall, copyOfMethodCall);
        }
        for (final MethodInvocation methodCall : methodCallsToRefactorWithVariable) {
            refactorMethod(b, methodCall, methodCall);
        }
        for (final VariableDeclaration variableDecl : variableDecls) {
            final VariableDeclarationStatement parent = (VariableDeclarationStatement) variableDecl.getParent();
            final VariableDeclarationStatement newDeclareStmt = b.copySubtree(parent);
            replaceVariableType(b, parent, newDeclareStmt);
            ctx.getRefactorings().replace(parent, newDeclareStmt);
        }
    }
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 74 with MethodInvocation

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

the class AbstractClassSubstituteRefactoring method visit.

@Override
public boolean visit(Block node) {
    final ObjectInstantiationVisitor classCreationVisitor = new ObjectInstantiationVisitor();
    node.accept(classCreationVisitor);
    for (final ClassInstanceCreation instanceCreation : classCreationVisitor.getObjectInstantiations()) {
        final List<VariableDeclaration> varDecls = new ArrayList<VariableDeclaration>();
        final List<MethodInvocation> methodCallsToRefactorAlone = new ArrayList<MethodInvocation>();
        final List<MethodInvocation> methodCallsToRefactorWithVariable = new ArrayList<MethodInvocation>();
        if (canInstantiationBeRefactored(instanceCreation) && canBeRefactored(node, instanceCreation, varDecls, methodCallsToRefactorAlone, methodCallsToRefactorWithVariable) && canCodeBeRefactored()) {
            replaceClass(instanceCreation, varDecls, methodCallsToRefactorAlone, methodCallsToRefactorWithVariable);
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : ClassInstanceCreation(org.eclipse.jdt.core.dom.ClassInstanceCreation) ArrayList(java.util.ArrayList) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 75 with MethodInvocation

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

the class BigDecimalRefactoring method getCompareToNode.

private InfixExpression getCompareToNode(final boolean isPositive, final MethodInvocation node) {
    final ASTBuilder b = this.ctx.getASTBuilder();
    final MethodInvocation mi = b.invoke(b.copy(node.getExpression()), "compareTo", b.copy(arg0(node)));
    return b.infixExpr(mi, isPositive ? EQUALS : NOT_EQUALS, b.int0(0));
}
Also used : MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Aggregations

MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)75 Expression (org.eclipse.jdt.core.dom.Expression)39 ASTNode (org.eclipse.jdt.core.dom.ASTNode)34 SimpleName (org.eclipse.jdt.core.dom.SimpleName)27 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)23 SuperMethodInvocation (org.eclipse.jdt.core.dom.SuperMethodInvocation)23 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)18 VariableDeclarationExpression (org.eclipse.jdt.core.dom.VariableDeclarationExpression)17 AST (org.eclipse.jdt.core.dom.AST)14 IBinding (org.eclipse.jdt.core.dom.IBinding)14 ParenthesizedExpression (org.eclipse.jdt.core.dom.ParenthesizedExpression)14 CastExpression (org.eclipse.jdt.core.dom.CastExpression)13 Name (org.eclipse.jdt.core.dom.Name)13 ArrayList (java.util.ArrayList)12 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)12 FieldAccess (org.eclipse.jdt.core.dom.FieldAccess)12 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)12 InfixExpression (org.eclipse.jdt.core.dom.InfixExpression)12 QualifiedName (org.eclipse.jdt.core.dom.QualifiedName)11 Statement (org.eclipse.jdt.core.dom.Statement)11