Search in sources :

Example 11 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
 * @return the blocks liveness state after current node
 */
public LivenessState buildCFG(final BreakStatement node, final LivenessState state) {
    CFGBasicBlock basicBlock = getCFGBasicBlock(node, state);
    Statement targetStatement;
    if (node.getLabel() != null) {
        targetStatement = findLabeledParentStatement(node);
    } else {
        targetStatement = findBreakableParentStatement(node);
    }
    addEdgeToBuild(targetStatement, 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)

Example 12 with BreakStatement

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

the class NavigateToDefinitionHandler method computeBreakContinue.

private Location computeBreakContinue(ITypeRoot typeRoot, int line, int column) throws CoreException {
    int offset = JsonRpcHelpers.toOffset(typeRoot.getBuffer(), line, column);
    if (offset >= 0) {
        CompilationUnit unit = SharedASTProviderCore.getAST(typeRoot, SharedASTProviderCore.WAIT_YES, null);
        if (unit == null) {
            return null;
        }
        ASTNode selectedNode = NodeFinder.perform(unit, offset, 0);
        ASTNode node = null;
        SimpleName label = null;
        if (selectedNode instanceof BreakStatement) {
            node = selectedNode;
            label = ((BreakStatement) node).getLabel();
        } else if (selectedNode instanceof ContinueStatement) {
            node = selectedNode;
            label = ((ContinueStatement) node).getLabel();
        } else if (selectedNode instanceof SimpleName && selectedNode.getParent() instanceof BreakStatement) {
            node = selectedNode.getParent();
            label = ((BreakStatement) node).getLabel();
        } else if (selectedNode instanceof SimpleName && selectedNode.getParent() instanceof ContinueStatement) {
            node = selectedNode.getParent();
            label = ((ContinueStatement) node).getLabel();
        }
        if (node != null) {
            ASTNode parent = node.getParent();
            ASTNode target = null;
            while (parent != null) {
                if (parent instanceof MethodDeclaration || parent instanceof Initializer) {
                    break;
                }
                if (label == null) {
                    if (parent instanceof ForStatement || parent instanceof EnhancedForStatement || parent instanceof WhileStatement || parent instanceof DoStatement) {
                        target = parent;
                        break;
                    }
                    if (node instanceof BreakStatement) {
                        if (parent instanceof SwitchStatement || parent instanceof SwitchExpression) {
                            target = parent;
                            break;
                        }
                    }
                    if (node instanceof LabeledStatement) {
                        target = parent;
                        break;
                    }
                } else if (LabeledStatement.class.isInstance(parent)) {
                    LabeledStatement ls = (LabeledStatement) parent;
                    if (ls.getLabel().getIdentifier().equals(label.getIdentifier())) {
                        target = ls;
                        break;
                    }
                }
                parent = parent.getParent();
            }
            if (target != null) {
                int start = target.getStartPosition();
                int end = new TokenScanner(unit.getTypeRoot()).getNextEndOffset(node.getStartPosition(), true) - start;
                if (start >= 0 && end >= 0) {
                    return JDTUtils.toLocation((ICompilationUnit) typeRoot, start, end);
                }
            }
        }
    }
    return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) TokenScanner(org.eclipse.jdt.internal.corext.dom.TokenScanner) DoStatement(org.eclipse.jdt.core.dom.DoStatement) SwitchExpression(org.eclipse.jdt.core.dom.SwitchExpression) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SimpleName(org.eclipse.jdt.core.dom.SimpleName) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) BreakStatement(org.eclipse.jdt.core.dom.BreakStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) Initializer(org.eclipse.jdt.core.dom.Initializer) ASTNode(org.eclipse.jdt.core.dom.ASTNode) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement)

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