Search in sources :

Example 16 with DoStatement

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

the class DeclarationOutsideLoopRatherThanInsideCleanUp method visit.

@Override
public boolean visit(final Block visited) {
    List<Statement> blockStatement = ASTNodes.asList(visited);
    boolean result = true;
    for (int i = 0; i < blockStatement.size(); i++) {
        Statement statement = blockStatement.get(i);
        ForStatement forStatement = ASTNodes.as(statement, ForStatement.class);
        EnhancedForStatement enhancedForStatement = ASTNodes.as(statement, EnhancedForStatement.class);
        WhileStatement whileStatement = ASTNodes.as(statement, WhileStatement.class);
        DoStatement doStatement = ASTNodes.as(statement, DoStatement.class);
        List<Statement> forStatements = null;
        if (forStatement != null) {
            forStatements = ASTNodes.asList(forStatement.getBody());
        } else if (enhancedForStatement != null) {
            forStatements = ASTNodes.asList(enhancedForStatement.getBody());
        } else if (whileStatement != null) {
            forStatements = ASTNodes.asList(whileStatement.getBody());
        } else if (doStatement != null) {
            forStatements = ASTNodes.asList(doStatement.getBody());
        }
        if (forStatements != null) {
            Set<SimpleName> conflictingNamesOutOfTheLoop = new HashSet<>();
            for (int j = 0; j < i; j++) {
                if (!(blockStatement.get(j) instanceof Block)) {
                    conflictingNamesOutOfTheLoop.addAll(ASTNodes.getLocalVariableIdentifiers(blockStatement.get(j), false));
                }
            }
            for (int j = i + 1; j < blockStatement.size(); j++) {
                conflictingNamesOutOfTheLoop.addAll(ASTNodes.getLocalVariableIdentifiers(blockStatement.get(j), true));
            }
            Set<SimpleName> conflictingNamesInLoop = new HashSet<>();
            for (Statement oneStatement : forStatements) {
                conflictingNamesInLoop.addAll(ASTNodes.getLocalVariableIdentifiers(oneStatement, true));
            }
            List<VariableDeclarationStatement> candidates = new ArrayList<>();
            for (Statement declarationStatement : forStatements) {
                VariableDeclarationStatement declaration = ASTNodes.as(declarationStatement, VariableDeclarationStatement.class);
                VariableDeclarationFragment fragment = ASTNodes.getUniqueFragment(declaration);
                if (fragment != null && !isEffectivelyFinalRequired(declaration, fragment) && !hasAnnotation(declaration.modifiers())) {
                    SimpleName name = fragment.getName();
                    if (isUniqueNameInLoop(name.getIdentifier(), conflictingNamesInLoop) && isUniqueNameOutOfTheLoop(conflictingNamesOutOfTheLoop, name.getIdentifier())) {
                        candidates.add(declaration);
                        conflictingNamesOutOfTheLoop.add(name);
                    }
                }
            }
            for (VariableDeclarationStatement candidate : candidates) {
                moveDeclaration(statement, candidate);
                result = false;
            }
        }
    }
    return result;
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) VariableDeclarationFragment(org.eclipse.jdt.core.dom.VariableDeclarationFragment) Block(org.eclipse.jdt.core.dom.Block) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) HashSet(java.util.HashSet)

Aggregations

DoStatement (org.eclipse.jdt.core.dom.DoStatement)16 ForStatement (org.eclipse.jdt.core.dom.ForStatement)12 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)12 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)11 Statement (org.eclipse.jdt.core.dom.Statement)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 Block (org.eclipse.jdt.core.dom.Block)8 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)7 IfStatement (org.eclipse.jdt.core.dom.IfStatement)6 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)6 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)6 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)4 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)3 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)3 SimpleName (org.eclipse.jdt.core.dom.SimpleName)3 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)3 TryStatement (org.eclipse.jdt.core.dom.TryStatement)3 ArrayList (java.util.ArrayList)2 LinkedList (java.util.LinkedList)2 AST (org.eclipse.jdt.core.dom.AST)2