Search in sources :

Example 86 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> methodCallsToRefactor) {
    final ASTBuilder b = ctx.getASTBuilder();
    final Type substituteType = substituteType(b, originalInstanceCreation.getType(), originalInstanceCreation);
    if (substituteType != null) {
        ctx.getRefactorings().replace(originalInstanceCreation.getType(), substituteType);
        originalInstanceCreation.setType(substituteType);
    }
    for (final MethodInvocation methodCall : methodCallsToRefactor) {
        final MethodInvocation copyOfMethodCall = b.copySubtree(methodCall);
        refactorMethod(b, methodCall, copyOfMethodCall);
        ctx.getRefactorings().replace(methodCall, copyOfMethodCall);
    }
    for (final VariableDeclaration variableDecl : variableDecls) {
        final VariableDeclarationStatement oldDeclareStmt = (VariableDeclarationStatement) variableDecl.getParent();
        final Type substituteVarType = substituteType(b, oldDeclareStmt.getType(), (ASTNode) oldDeclareStmt.fragments().get(0));
        if (substituteVarType != null) {
            ctx.getRefactorings().replace(oldDeclareStmt.getType(), substituteVarType);
        }
    }
}
Also used : Type(org.eclipse.jdt.core.dom.Type) ASTHelper.hasType(org.autorefactor.refactoring.ASTHelper.hasType) 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 87 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);
    node.accept(classCreationVisitor);
    for (final ClassInstanceCreation instanceCreation : classCreationVisitor.getObjectInstantiations()) {
        final List<VariableDeclaration> varDecls = new ArrayList<VariableDeclaration>();
        final List<MethodInvocation> methodCallsToRefactor = new ArrayList<MethodInvocation>();
        if (canInstantiationBeRefactored(instanceCreation) && canBeRefactored(node, instanceCreation, instanceCreation.resolveTypeBinding(), varDecls, methodCallsToRefactor) && canCodeBeRefactored()) {
            replaceClass(instanceCreation, varDecls, methodCallsToRefactor);
            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 88 with MethodInvocation

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

the class AllInOneMethodRatherThanLoopRefactoring method maybeReplaceWithCollectionMethod.

private boolean maybeReplaceWithCollectionMethod(ForStatement node, ForLoopContent loopContent, String methodName, MethodInvocation colMI) {
    final Expression addArg0 = arg0(colMI);
    final MethodInvocation getMI = as(addArg0, MethodInvocation.class);
    if (isSameVariable(loopContent, getMI)) {
        replaceWithCollectionMethod(node, methodName, colMI.getExpression(), getMI.getExpression());
        return DO_NOT_VISIT_SUBTREE;
    }
    return VISIT_SUBTREE;
}
Also used : Expression(org.eclipse.jdt.core.dom.Expression) ASTHelper.asExpression(org.autorefactor.refactoring.ASTHelper.asExpression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation)

Example 89 with MethodInvocation

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

the class AllInOneMethodRatherThanLoopRefactoring method visit.

@Override
public boolean visit(ForStatement node) {
    final ForLoopContent loopContent = iterateOverContainer(node);
    final List<Statement> stmts = asList(node.getBody());
    if (loopContent != null && loopContent.getLoopVariable() != null && stmts.size() == 1) {
        final SimpleName loopVariable = (SimpleName) loopContent.getLoopVariable();
        final IVariableBinding loopVariableName = (IVariableBinding) loopVariable.resolveBinding();
        // As we replace only one, there should be no more than one occurrence
        if (getVariableUseCount(loopVariableName, node.getBody()) == 1) {
            final MethodInvocation mi = asExpression(stmts.get(0), MethodInvocation.class);
            switch(loopContent.getContainerType()) {
                case COLLECTION:
                    if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object")) {
                        return maybeReplaceWithCollectionMethod(node, loopContent, "addAll", mi);
                    } else if (isMethod(mi, "java.util.Collection", "remove", "java.lang.Object")) {
                        return maybeReplaceWithCollectionMethod(node, loopContent, "removeAll", mi);
                    }
                    break;
                case ARRAY:
                    if (isMethod(mi, "java.util.Collection", "add", "java.lang.Object") && areTypeCompatible(mi.getExpression(), loopContent.getContainerVariable())) {
                        final Expression addArg0 = arg0(mi);
                        final ArrayAccess aa = as(addArg0, ArrayAccess.class);
                        if (isSameVariable(loopContent, aa)) {
                            replaceWithCollectionsAddAll(node, loopContent.getContainerVariable(), mi);
                            return DO_NOT_VISIT_SUBTREE;
                        }
                    }
                    break;
            }
        }
    }
    return VISIT_SUBTREE;
}
Also used : ForLoopContent(org.autorefactor.refactoring.ForLoopHelper.ForLoopContent) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) Expression(org.eclipse.jdt.core.dom.Expression) ASTHelper.asExpression(org.autorefactor.refactoring.ASTHelper.asExpression) Statement(org.eclipse.jdt.core.dom.Statement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding)

Example 90 with MethodInvocation

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

the class ASTBuilder method invoke.

/**
 * Builds a new {@link MethodInvocation} instance.
 *
 * @param <E> the arguments type
 * @param expression the method invocation expression
 * @param methodName the name of the invoked method
 * @param arguments the arguments for the method invocation
 * @return a new method invocation
 */
public <E extends Expression> MethodInvocation invoke(Expression expression, String methodName, List<E> arguments) {
    final MethodInvocation mi = ast.newMethodInvocation();
    mi.setExpression(expression);
    mi.setName(ast.newSimpleName(methodName));
    addAll(mi, arguments);
    return mi;
}
Also used : 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