Search in sources :

Example 1 with ForLoopContent

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;
}
Also used : ForLoopContent(org.autorefactor.jdt.internal.corext.dom.ForLoops.ForLoopContent) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement)

Example 2 with ForLoopContent

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;
}
Also used : ForLoopContent(org.autorefactor.jdt.internal.corext.dom.ForLoops.ForLoopContent) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess) HashSet(java.util.HashSet) Set(java.util.Set) ThisExpression(org.eclipse.jdt.core.dom.ThisExpression) Expression(org.eclipse.jdt.core.dom.Expression) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) Name(org.eclipse.jdt.core.dom.Name)

Example 3 with ForLoopContent

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;
}
Also used : Assignment(org.eclipse.jdt.core.dom.Assignment) ForLoopContent(org.autorefactor.jdt.internal.corext.dom.ForLoops.ForLoopContent) ArrayAccess(org.eclipse.jdt.core.dom.ArrayAccess)

Aggregations

ForLoopContent (org.autorefactor.jdt.internal.corext.dom.ForLoops.ForLoopContent)3 ArrayAccess (org.eclipse.jdt.core.dom.ArrayAccess)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Assignment (org.eclipse.jdt.core.dom.Assignment)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 Name (org.eclipse.jdt.core.dom.Name)1 ThisExpression (org.eclipse.jdt.core.dom.ThisExpression)1