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;
}
Aggregations