Search in sources :

Example 1 with BreakStatement

use of org.eclipse.jdt.core.dom.BreakStatement in project che by eclipse.

the class ExtractMethodAnalyzer method canHandleBranches.

private String canHandleBranches() {
    if (fReturnValue != null)
        return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
    ASTNode[] selectedNodes = getSelectedNodes();
    final ASTNode lastSelectedNode = selectedNodes[selectedNodes.length - 1];
    Statement body = getParentLoopBody(lastSelectedNode.getParent());
    if (!(body instanceof Block))
        return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
    if (body != lastSelectedNode) {
        Block block = (Block) body;
        List<Statement> statements = block.statements();
        ASTNode lastStatementInLoop = statements.get(statements.size() - 1);
        if (lastSelectedNode != lastStatementInLoop)
            return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
    }
    final String[] continueMatchesLoopProblem = { null };
    for (int i = 0; i < selectedNodes.length; i++) {
        final ASTNode astNode = selectedNodes[i];
        astNode.accept(new ASTVisitor() {

            ArrayList<String> fLocalLoopLabels = new ArrayList<String>();

            @Override
            public boolean visit(BreakStatement node) {
                SimpleName label = node.getLabel();
                if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
                    continueMatchesLoopProblem[0] = Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_branch_break_mismatch, //$NON-NLS-1$
                    new Object[] { ("break " + label.getIdentifier()) });
                }
                return false;
            }

            @Override
            public boolean visit(LabeledStatement node) {
                SimpleName label = node.getLabel();
                if (label != null)
                    fLocalLoopLabels.add(label.getIdentifier());
                return true;
            }

            @Override
            public void endVisit(ContinueStatement node) {
                SimpleName label = node.getLabel();
                if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
                    if (fEnclosingLoopLabel == null || !label.getIdentifier().equals(fEnclosingLoopLabel.getIdentifier())) {
                        continueMatchesLoopProblem[0] = Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_branch_continue_mismatch, //$NON-NLS-1$
                        new Object[] { "continue " + label.getIdentifier() });
                    }
                }
            }
        });
    }
    return continueMatchesLoopProblem[0];
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement)

Example 2 with BreakStatement

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

the class IfRatherThanWhileAndFallsThroughRefactoring method isEndingWithExit.

/**
 * Return true if the statement falls through.
 *
 * @param stmt the statement
 * @return true if the statement falls through.
 */
private boolean isEndingWithExit(final Statement stmt) {
    final List<Statement> stmts = asList(stmt);
    if (stmts.isEmpty()) {
        return false;
    }
    final Statement lastStmt = stmts.get(stmts.size() - 1);
    switch(lastStmt.getNodeType()) {
        case RETURN_STATEMENT:
        case THROW_STATEMENT:
            return true;
        case BREAK_STATEMENT:
            final BreakStatement breakStmt = (BreakStatement) lastStmt;
            return breakStmt.getLabel() == null;
        case IF_STATEMENT:
            final IfStatement ifStmt = (IfStatement) lastStmt;
            final Statement thenStmt = ifStmt.getThenStatement();
            final Statement elseStmt = ifStmt.getElseStatement();
            return isEndingWithExit(thenStmt) && isEndingWithExit(elseStmt);
        default:
            return false;
    }
}
Also used : BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement)

Example 3 with BreakStatement

use of org.eclipse.jdt.core.dom.BreakStatement in project eclipse.jdt.ls by eclipse.

the class ExtractMethodAnalyzer method canHandleBranches.

private String canHandleBranches() {
    if (fReturnValue != null) {
        return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
    }
    ASTNode[] selectedNodes = getSelectedNodes();
    final ASTNode lastSelectedNode = selectedNodes[selectedNodes.length - 1];
    Statement body = getParentLoopBody(lastSelectedNode.getParent());
    if (!(body instanceof Block)) {
        return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
    }
    if (body != lastSelectedNode) {
        Block block = (Block) body;
        List<Statement> statements = block.statements();
        ASTNode lastStatementInLoop = statements.get(statements.size() - 1);
        if (lastSelectedNode != lastStatementInLoop) {
            return RefactoringCoreMessages.ExtractMethodAnalyzer_branch_mismatch;
        }
    }
    final String[] continueMatchesLoopProblem = { null };
    for (int i = 0; i < selectedNodes.length; i++) {
        final ASTNode astNode = selectedNodes[i];
        astNode.accept(new ASTVisitor() {

            ArrayList<String> fLocalLoopLabels = new ArrayList<>();

            @Override
            public boolean visit(BreakStatement node) {
                SimpleName label = node.getLabel();
                if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
                    // $NON-NLS-1$
                    continueMatchesLoopProblem[0] = Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_branch_break_mismatch, new Object[] { ("break " + label.getIdentifier()) });
                }
                return false;
            }

            @Override
            public boolean visit(LabeledStatement node) {
                SimpleName label = node.getLabel();
                if (label != null) {
                    fLocalLoopLabels.add(label.getIdentifier());
                }
                return true;
            }

            @Override
            public void endVisit(ContinueStatement node) {
                SimpleName label = node.getLabel();
                if (label != null && !fLocalLoopLabels.contains(label.getIdentifier())) {
                    if (fEnclosingLoopLabel == null || !label.getIdentifier().equals(fEnclosingLoopLabel.getIdentifier())) {
                        // $NON-NLS-1$
                        continueMatchesLoopProblem[0] = Messages.format(RefactoringCoreMessages.ExtractMethodAnalyzer_branch_continue_mismatch, new Object[] { "continue " + label.getIdentifier() });
                    }
                }
            }
        });
    }
    return continueMatchesLoopProblem[0];
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement)

Example 4 with BreakStatement

use of org.eclipse.jdt.core.dom.BreakStatement in project flux by eclipse.

the class AdvancedQuickAssistProcessor method hasStopAsLastExecutableStatement.

private static boolean hasStopAsLastExecutableStatement(Statement lastStatement) {
    if (lastStatement instanceof ReturnStatement || lastStatement instanceof BreakStatement) {
        return true;
    }
    if (lastStatement instanceof Block) {
        Block block = (Block) lastStatement;
        lastStatement = (Statement) block.statements().get(block.statements().size() - 1);
        return hasStopAsLastExecutableStatement(lastStatement);
    }
    return false;
}
Also used : BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 5 with BreakStatement

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

the class ObsoleteIfRatherThanTwoSwitchCasesCleanUp method visit.

@SuppressWarnings("deprecation")
@Override
public boolean visit(final SwitchStatement visited) {
    List<?> statements = visited.statements();
    if (statements.isEmpty()) {
        return true;
    }
    Set<SimpleName> previousVarIds = new HashSet<>();
    Set<SimpleName> caseVarIds = new HashSet<>();
    List<Pair<List<Expression>, List<Statement>>> switchStructure = new ArrayList<>();
    List<Expression> caseExprs = new ArrayList<>();
    List<Statement> caseStatements = new ArrayList<>();
    boolean isPreviousStmtACase = true;
    int caseIndexWithDefault = -1;
    for (Object object : statements) {
        Statement statement = (Statement) object;
        if (statement instanceof SwitchCase) {
            if (!isPreviousStmtACase) {
                if (switchStructure.size() > 2) {
                    return true;
                }
                previousVarIds.addAll(caseVarIds);
                caseVarIds.clear();
                switchStructure.add(Pair.<List<Expression>, List<Statement>>of(caseExprs, caseStatements));
                caseExprs = new ArrayList<>();
                caseStatements = new ArrayList<>();
            }
            if (((SwitchCase) statement).isDefault()) {
                caseIndexWithDefault = switchStructure.size();
            } else {
                caseExprs.add(((SwitchCase) statement).getExpression());
            }
            isPreviousStmtACase = true;
        } else {
            VarConflictVisitor varOccurrenceVisitor = new VarConflictVisitor(previousVarIds, false);
            varOccurrenceVisitor.traverseNodeInterruptibly(statement);
            if (varOccurrenceVisitor.isVarConflicting()) {
                return true;
            }
            caseVarIds.addAll(ASTNodes.getLocalVariableIdentifiers(statement, false));
            caseStatements.add(statement);
            isPreviousStmtACase = false;
        }
    }
    switchStructure.add(Pair.<List<Expression>, List<Statement>>of(caseExprs, caseStatements));
    if (caseIndexWithDefault != -1) {
        Pair<List<Expression>, List<Statement>> caseWithDefault = switchStructure.remove(caseIndexWithDefault);
        switchStructure.add(caseWithDefault);
    }
    if (switchStructure.size() > 2 || !switchStructure.get(switchStructure.size() - 1).getFirst().isEmpty() && !ASTNodes.isPassive(visited.getExpression())) {
        return true;
    }
    List<BreakStatement> overBreaks = new ArrayList<>();
    for (int i = 0; i < switchStructure.size(); i++) {
        Pair<List<Expression>, List<Statement>> caseStructure = switchStructure.get(i);
        if (!caseStructure.getSecond().isEmpty()) {
            Statement lastStatement = caseStructure.getSecond().get(caseStructure.getSecond().size() - 1);
            if (i < switchStructure.size() - 1 && !ASTNodes.fallsThrough(lastStatement)) {
                return true;
            }
            BreakStatement breakStatement = ASTNodes.as(lastStatement, BreakStatement.class);
            if (breakStatement != null && breakStatement.getLabel() == null) {
                caseStructure.getSecond().remove(caseStructure.getSecond().size() - 1);
            }
        } else if (i < switchStructure.size() - 1) {
            return true;
        }
    }
    for (Pair<List<Expression>, List<Statement>> caseStructure : switchStructure) {
        for (Statement oneStatement : caseStructure.getSecond()) {
            BreakVisitor breakVisitor = new BreakVisitor();
            breakVisitor.traverseNodeInterruptibly(oneStatement);
            if (!breakVisitor.canBeRefactored()) {
                return true;
            }
            overBreaks.addAll(breakVisitor.getBreaks());
        }
    }
    replaceBySwitch(visited, switchStructure, caseIndexWithDefault, overBreaks);
    return false;
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) Statement(org.eclipse.jdt.core.dom.Statement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ArrayList(java.util.ArrayList) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) VarConflictVisitor(org.autorefactor.jdt.internal.corext.dom.VarConflictVisitor) SwitchCase(org.eclipse.jdt.core.dom.SwitchCase) Expression(org.eclipse.jdt.core.dom.Expression) InfixExpression(org.eclipse.jdt.core.dom.InfixExpression) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) Pair(org.autorefactor.util.Pair)

Aggregations

BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)11 DoStatement (org.eclipse.jdt.core.dom.DoStatement)8 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)8 ForStatement (org.eclipse.jdt.core.dom.ForStatement)8 Statement (org.eclipse.jdt.core.dom.Statement)8 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)8 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)8 IfStatement (org.eclipse.jdt.core.dom.IfStatement)7 Block (org.eclipse.jdt.core.dom.Block)5 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)5 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)5 ArrayList (java.util.ArrayList)4 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)4 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)4 TryStatement (org.eclipse.jdt.core.dom.TryStatement)4 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)3 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)3 SimpleName (org.eclipse.jdt.core.dom.SimpleName)3 List (java.util.List)2 ASTRewrite (org.autorefactor.jdt.core.dom.ASTRewrite)2