use of org.autorefactor.jdt.internal.corext.dom.ForLoops.ForLoopContent in project AutoRefactor by JnRouvignac.
the class NoLoopIterationRatherThanEmptyCheckCleanUp method getContainer.
private Expression getContainer(final List<Statement> statements) {
ForStatement forStatement = ASTNodes.as(statements.get(0), ForStatement.class);
EnhancedForStatement enhancedForStatement = ASTNodes.as(statements.get(0), EnhancedForStatement.class);
if (forStatement != null) {
ForLoopContent loopContent = ForLoops.iterateOverContainer(forStatement);
if (loopContent != null) {
return loopContent.getContainerVariable();
}
} else if (enhancedForStatement != null) {
return enhancedForStatement.getExpression();
}
return null;
}
use of org.autorefactor.jdt.internal.corext.dom.ForLoops.ForLoopContent in project AutoRefactor by JnRouvignac.
the class ObsoleteAddAllRatherThanLoopCleanUp method maybeRefactorForStatement.
private boolean maybeRefactorForStatement(final ForStatement node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
ForLoopContent loopContent = ForLoops.iterateOverContainer(node);
MethodInvocation methodInvocation = ASTNodes.asExpression(node.getBody(), MethodInvocation.class);
if (loopContent != null && loopContent.getLoopVariable() != null && methodInvocation != null) {
Name loopVariable = loopContent.getLoopVariable();
IVariableBinding loopVariableName = (IVariableBinding) loopVariable.resolveBinding();
// As we replace only one, there should be no more than one occurrence
if (methodInvocation != null && methodInvocation.arguments().size() == 1 && getVariableUseCount(loopVariableName, node.getBody()) == 1 && (loopContent.isLoopingForward() || methodInvocation.resolveMethodBinding() != null && ASTNodes.hasType(methodInvocation.resolveMethodBinding().getDeclaringClass(), Set.class.getCanonicalName()))) {
Expression addArg0 = (Expression) methodInvocation.arguments().get(0);
switch(loopContent.getContainerType()) {
case COLLECTION:
MethodInvocation getMI = ASTNodes.as(addArg0, MethodInvocation.class);
if (getMI != null && getMI.arguments().size() == 1 && isSameVariable(loopContent, getMI)) {
return maybeReplaceForCollection(node, methodInvocation, getMI.getExpression());
}
break;
case ARRAY:
ArrayAccess arrayAccess = ASTNodes.as(addArg0, ArrayAccess.class);
if (isSameVariable(loopContent, arrayAccess)) {
return maybeReplaceForArray(node, classesToUseWithImport, importsToAdd, loopContent.getContainerVariable(), methodInvocation);
}
break;
}
}
}
return true;
}
use of org.autorefactor.jdt.internal.corext.dom.ForLoops.ForLoopContent in project AutoRefactor by JnRouvignac.
the class ObsoleteFillRatherThanLoopCleanUp method maybeRefactorForStatement.
private boolean maybeRefactorForStatement(final ForStatement node, final Set<String> classesToUseWithImport, final Set<String> importsToAdd) {
ForLoopContent loopContent = ForLoops.iterateOverContainer(node);
Assignment assignment = ASTNodes.asExpression(node.getBody(), Assignment.class);
if (assignment != null && loopContent != null && loopContent.getLoopVariable() != null && loopContent.getContainerType() == ContainerType.ARRAY && ASTNodes.hasOperator(assignment, Assignment.Operator.ASSIGN) && ASTNodes.isHardCoded(assignment.getRightHandSide()) && ASTNodes.isPassive(assignment.getRightHandSide())) {
ArrayAccess arrayAccess = ASTNodes.as(assignment.getLeftHandSide(), ArrayAccess.class);
if (arrayAccess != null && isSameVariable(loopContent, arrayAccess)) {
replaceWithArraysFill(node, classesToUseWithImport, importsToAdd, assignment, arrayAccess);
return false;
}
}
return true;
}
Aggregations