Search in sources :

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

Example 82 with MethodInvocation

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

the class ForLoopHelper method buildForLoopContent.

private static ForLoopContent buildForLoopContent(final Expression loopVar, final Expression containerVar) {
    if (!(loopVar instanceof Name)) {
        return null;
    }
    if (containerVar instanceof MethodInvocation) {
        final MethodInvocation mi = (MethodInvocation) containerVar;
        final Name containerVarName = as(mi.getExpression(), Name.class);
        if (containerVarName != null && isMethod(mi, "java.util.Collection", "size")) {
            return ForLoopContent.indexedCollection(containerVarName, (Name) loopVar);
        }
    } else if (containerVar instanceof QualifiedName) {
        final QualifiedName containerVarName = (QualifiedName) containerVar;
        if (isArrayLength(containerVarName)) {
            Name containerVariable = ((QualifiedName) containerVar).getQualifier();
            return ForLoopContent.indexedArray(containerVariable, (Name) loopVar);
        }
    }
    return null;
}
Also used : QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name)

Example 83 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 84 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 85 with MethodInvocation

use of org.eclipse.jdt.core.dom.MethodInvocation in project xtext-xtend by eclipse.

the class JavaASTFlattener method computeArrayName.

public String computeArrayName(final ArrayAccess node) {
    String _switchResult = null;
    Expression _array = node.getArray();
    final Expression array = _array;
    boolean _matched = false;
    if (array instanceof SimpleName) {
        _matched = true;
        _switchResult = ((SimpleName) array).getIdentifier();
    }
    if (!_matched) {
        if (array instanceof MethodInvocation) {
            _matched = true;
            _switchResult = ((MethodInvocation) array).getName().getIdentifier();
        }
    }
    if (!_matched) {
        if (array instanceof ArrayAccess) {
            _matched = true;
            String _computeArrayName = this.computeArrayName(((ArrayAccess) array));
            _switchResult = ("_" + _computeArrayName);
        }
    }
    if (!_matched) {
        _switchResult = "tmpNode";
    }
    return _switchResult;
}
Also used : ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) InstanceofExpression(org.eclipse.jdt.core.dom.InstanceofExpression) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) CastExpression(org.eclipse.jdt.core.dom.CastExpression) VariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression) PostfixExpression(org.eclipse.jdt.core.dom.PostfixExpression) PrefixExpression(org.eclipse.jdt.core.dom.PrefixExpression) ConditionalExpression(org.eclipse.jdt.core.dom.ConditionalExpression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) ParenthesizedExpression(org.eclipse.jdt.core.dom.ParenthesizedExpression) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation)

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