Search in sources :

Example 6 with LabeledStatement

use of org.eclipse.jdt.core.dom.LabeledStatement 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 LabeledStatement

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

the class ExtractMethodAnalyzer method getParentLoopBody.

private Statement getParentLoopBody(ASTNode node) {
    Statement stmt = null;
    ASTNode start = node;
    while (start != null && !(start instanceof ForStatement) && !(start instanceof DoStatement) && !(start instanceof WhileStatement) && !(start instanceof EnhancedForStatement) && !(start instanceof SwitchStatement)) {
        start = start.getParent();
    }
    if (start instanceof ForStatement) {
        stmt = ((ForStatement) start).getBody();
    } else if (start instanceof DoStatement) {
        stmt = ((DoStatement) start).getBody();
    } else if (start instanceof WhileStatement) {
        stmt = ((WhileStatement) start).getBody();
    } else if (start instanceof EnhancedForStatement) {
        stmt = ((EnhancedForStatement) start).getBody();
    }
    if (start != null && start.getParent() instanceof LabeledStatement) {
        LabeledStatement labeledStatement = (LabeledStatement) start.getParent();
        fEnclosingLoopLabel = labeledStatement.getLabel();
    }
    return stmt;
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) 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) ASTNode(org.eclipse.jdt.core.dom.ASTNode) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement)

Example 8 with LabeledStatement

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

the class ExtractMethodAnalyzer method getParentLoopBody.

private Statement getParentLoopBody(ASTNode node) {
    Statement stmt = null;
    ASTNode start = node;
    while (start != null && !(start instanceof ForStatement) && !(start instanceof DoStatement) && !(start instanceof WhileStatement) && !(start instanceof EnhancedForStatement) && !(start instanceof SwitchStatement)) {
        start = start.getParent();
    }
    if (start instanceof ForStatement) {
        stmt = ((ForStatement) start).getBody();
    } else if (start instanceof DoStatement) {
        stmt = ((DoStatement) start).getBody();
    } else if (start instanceof WhileStatement) {
        stmt = ((WhileStatement) start).getBody();
    } else if (start instanceof EnhancedForStatement) {
        stmt = ((EnhancedForStatement) start).getBody();
    }
    if (start != null && start.getParent() instanceof LabeledStatement) {
        LabeledStatement labeledStatement = (LabeledStatement) start.getParent();
        fEnclosingLoopLabel = labeledStatement.getLabel();
    }
    return stmt;
}
Also used : SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) 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) ASTNode(org.eclipse.jdt.core.dom.ASTNode) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement)

Example 9 with LabeledStatement

use of org.eclipse.jdt.core.dom.LabeledStatement 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

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