Search in sources :

Example 1 with ForLoopContent

use of org.autorefactor.refactoring.ForLoopHelper.ForLoopContent 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 replaceWithCollectionMethod(node, loopContent, "addAll", mi);
                    } else if (isMethod(mi, "java.util.Collection", "contains", "java.lang.Object")) {
                        return replaceWithCollectionMethod(node, loopContent, "containsAll", mi);
                    } else if (isMethod(mi, "java.util.Collection", "remove", "java.lang.Object")) {
                        return replaceWithCollectionMethod(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)) {
                            return replaceWithCollectionsAddAll(node, loopContent.getContainerVariable(), mi);
                        }
                    }
                    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)

Aggregations

ASTHelper.asExpression (org.autorefactor.refactoring.ASTHelper.asExpression)1 ForLoopContent (org.autorefactor.refactoring.ForLoopHelper.ForLoopContent)1 ArrayAccess (org.eclipse.jdt.core.dom.ArrayAccess)1 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)1 Expression (org.eclipse.jdt.core.dom.Expression)1 ForStatement (org.eclipse.jdt.core.dom.ForStatement)1 IVariableBinding (org.eclipse.jdt.core.dom.IVariableBinding)1 MethodInvocation (org.eclipse.jdt.core.dom.MethodInvocation)1 SimpleName (org.eclipse.jdt.core.dom.SimpleName)1 Statement (org.eclipse.jdt.core.dom.Statement)1