Search in sources :

Example 6 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 7 with BreakStatement

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

the class IfRatherThanWhileAndFallsThroughRefactoring method visit.

@Override
public boolean visit(WhileStatement node) {
    if (isEndingWithExit(node.getBody())) {
        final BreakVisitor breakVisitor = new BreakVisitor(node);
        breakVisitor.visitNode(node);
        if (breakVisitor.canBeRefactored()) {
            final ASTBuilder b = ctx.getASTBuilder();
            for (final BreakStatement breakStmt : breakVisitor.getBreaks()) {
                ctx.getRefactorings().remove(breakStmt);
            }
            ctx.getRefactorings().replace(node, b.if0(b.copy(node.getExpression()), b.copy(node.getBody())));
            return DO_NOT_VISIT_SUBTREE;
        }
    }
    return VISIT_SUBTREE;
}
Also used : BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ASTBuilder(org.autorefactor.refactoring.ASTBuilder)

Example 8 with BreakStatement

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

the class CFGBuilder method buildCFG.

/**
 * Builds a CFG for the provided node.
 *
 * @param node the node for which to build a CFG.
 * @param state the blocks liveness state before current node
 * @param throwers the thrower blocks information
 * @return the blocks liveness state after current node
 */
public LivenessState buildCFG(BreakStatement node, LivenessState state, ThrowerBlocks throwers) {
    final CFGBasicBlock basicBlock = getCFGBasicBlock(node, state);
    final Statement targetStmt;
    if (node.getLabel() != null) {
        targetStmt = findLabeledParentStmt(node);
    } else {
        targetStmt = findBreakableParentStmt(node);
    }
    addEdgeToBuild(targetStmt, new CFGEdgeBuilder(basicBlock), true);
    return state.copyLiveBasicBlock();
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement) SynchronizedStatement(org.eclipse.jdt.core.dom.SynchronizedStatement) ThrowStatement(org.eclipse.jdt.core.dom.ThrowStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) TypeDeclarationStatement(org.eclipse.jdt.core.dom.TypeDeclarationStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) EmptyStatement(org.eclipse.jdt.core.dom.EmptyStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) TryStatement(org.eclipse.jdt.core.dom.TryStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) CFGEdgeBuilder(org.autorefactor.cfg.CFGEdgeBuilder)

Example 9 with BreakStatement

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

the class AdvancedQuickAssistProcessor method copyStatementExceptBreak.

private static Statement copyStatementExceptBreak(AST ast, ASTRewrite rewrite, Statement source) {
    if (source instanceof Block) {
        Block block = (Block) source;
        Block newBlock = ast.newBlock();
        for (Iterator<Statement> iter = block.statements().iterator(); iter.hasNext(); ) {
            Statement statement = iter.next();
            if (statement instanceof BreakStatement) {
                continue;
            }
            newBlock.statements().add(copyStatementExceptBreak(ast, rewrite, statement));
        }
        return newBlock;
    }
    return (Statement) rewrite.createMoveTarget(source);
}
Also used : BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) 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) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) AssertStatement(org.eclipse.jdt.core.dom.AssertStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) VariableDeclarationStatement(org.eclipse.jdt.core.dom.VariableDeclarationStatement) Block(org.eclipse.jdt.core.dom.Block)

Example 10 with BreakStatement

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

the class ObsoleteIfRatherThanWhileAndFallsThroughCleanUp method replaceByIf.

private void replaceByIf(final WhileStatement visited, final BreakVisitor breakVisitor) {
    ASTRewrite rewrite = cuRewrite.getASTRewrite();
    ASTNodeFactory ast = cuRewrite.getASTBuilder();
    TextEditGroup group = new TextEditGroup(MultiFixMessages.ObsoleteIfRatherThanWhileAndFallsThroughCleanUp_description);
    for (BreakStatement breakStatement : breakVisitor.getBreaks()) {
        if (ASTNodes.canHaveSiblings(breakStatement) || breakStatement.getLocationInParent() == IfStatement.ELSE_STATEMENT_PROPERTY) {
            rewrite.remove(breakStatement, group);
        } else {
            ASTNodes.replaceButKeepComment(rewrite, breakStatement, ast.newBlock(), group);
        }
    }
    IfStatement newIfStatement = ast.newIfStatement();
    newIfStatement.setExpression(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(visited.getExpression())));
    newIfStatement.setThenStatement(ASTNodes.createMoveTarget(rewrite, visited.getBody()));
    ASTNodes.replaceButKeepComment(rewrite, visited, newIfStatement, group);
}
Also used : BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) ASTNodeFactory(org.autorefactor.jdt.internal.corext.dom.ASTNodeFactory) ASTRewrite(org.autorefactor.jdt.core.dom.ASTRewrite) TextEditGroup(org.eclipse.text.edits.TextEditGroup)

Aggregations

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