Search in sources :

Example 1 with LabeledStatement

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

the class SourceProvider method isDangligIf.

public boolean isDangligIf() {
    List<Statement> statements = fDeclaration.getBody().statements();
    if (statements.size() != 1)
        return false;
    ASTNode p = statements.get(0);
    while (true) {
        if (p instanceof IfStatement) {
            return ((IfStatement) p).getElseStatement() == null;
        } else {
            ChildPropertyDescriptor childD;
            if (p instanceof WhileStatement) {
                childD = WhileStatement.BODY_PROPERTY;
            } else if (p instanceof ForStatement) {
                childD = ForStatement.BODY_PROPERTY;
            } else if (p instanceof EnhancedForStatement) {
                childD = EnhancedForStatement.BODY_PROPERTY;
            } else if (p instanceof DoStatement) {
                childD = DoStatement.BODY_PROPERTY;
            } else if (p instanceof LabeledStatement) {
                childD = LabeledStatement.BODY_PROPERTY;
            } else {
                return false;
            }
            Statement body = (Statement) p.getStructuralProperty(childD);
            if (body instanceof Block) {
                return false;
            } else {
                p = body;
            }
        }
    }
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) ChildPropertyDescriptor(org.eclipse.jdt.core.dom.ChildPropertyDescriptor) 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) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) IfStatement(org.eclipse.jdt.core.dom.IfStatement) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) Block(org.eclipse.jdt.core.dom.Block) 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 2 with LabeledStatement

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

the class CallInliner method initializeInsertionPoint.

private void initializeInsertionPoint(int nos) {
    fInsertionIndex = -1;
    fNeedsStatement = false;
    // if we have a constructor invocation the invocation itself is already a statement
    ASTNode parentStatement = fInvocation instanceof Statement ? fInvocation : ASTNodes.getParent(fInvocation, Statement.class);
    if (parentStatement == null)
        return;
    ASTNode container = parentStatement.getParent();
    int type = container.getNodeType();
    if (type == ASTNode.BLOCK) {
        Block block = (Block) container;
        fListRewrite = fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
        fInsertionIndex = fListRewrite.getRewrittenList().indexOf(parentStatement);
    } else if (type == ASTNode.SWITCH_STATEMENT) {
        SwitchStatement switchStatement = (SwitchStatement) container;
        fListRewrite = fRewrite.getListRewrite(switchStatement, SwitchStatement.STATEMENTS_PROPERTY);
        fInsertionIndex = fListRewrite.getRewrittenList().indexOf(parentStatement);
    } else if (isControlStatement(container) || type == ASTNode.LABELED_STATEMENT) {
        fNeedsStatement = true;
        if (nos > 1 || needsBlockAroundDanglingIf()) {
            Block block = fInvocation.getAST().newBlock();
            fInsertionIndex = 0;
            Statement currentStatement = null;
            switch(type) {
                case ASTNode.LABELED_STATEMENT:
                    currentStatement = ((LabeledStatement) container).getBody();
                    break;
                case ASTNode.FOR_STATEMENT:
                    currentStatement = ((ForStatement) container).getBody();
                    break;
                case ASTNode.ENHANCED_FOR_STATEMENT:
                    currentStatement = ((EnhancedForStatement) container).getBody();
                    break;
                case ASTNode.WHILE_STATEMENT:
                    currentStatement = ((WhileStatement) container).getBody();
                    break;
                case ASTNode.DO_STATEMENT:
                    currentStatement = ((DoStatement) container).getBody();
                    break;
                case ASTNode.IF_STATEMENT:
                    IfStatement node = (IfStatement) container;
                    Statement thenPart = node.getThenStatement();
                    if (fTargetNode == thenPart || ASTNodes.isParent(fTargetNode, thenPart)) {
                        currentStatement = thenPart;
                    } else {
                        currentStatement = node.getElseStatement();
                    }
                    break;
            }
            Assert.isNotNull(currentStatement);
            fRewrite.replace(currentStatement, block, null);
            fListRewrite = fRewrite.getListRewrite(block, Block.STATEMENTS_PROPERTY);
            // The method to be inlined is not the body of the control statement.
            if (currentStatement != fTargetNode) {
                fListRewrite.insertLast(fRewrite.createCopyTarget(currentStatement), null);
            } else {
                // We can't replace a copy with something else. So we
                // have to insert all statements to be inlined.
                fTargetNode = null;
            }
        }
    }
// We only insert one new statement or we delete the existing call.
// So there is no need to have an insertion index.
}
Also used : IfStatement(org.eclipse.jdt.core.dom.IfStatement) SwitchStatement(org.eclipse.jdt.core.dom.SwitchStatement) DoStatement(org.eclipse.jdt.core.dom.DoStatement) Statement(org.eclipse.jdt.core.dom.Statement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) 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) 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) Block(org.eclipse.jdt.core.dom.Block)

Example 3 with LabeledStatement

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

the class ExtractMethodRefactoring method replaceBranches.

private void replaceBranches(final CompilationUnitChange result) {
    ASTNode[] selectedNodes = fAnalyzer.getSelectedNodes();
    for (int i = 0; i < selectedNodes.length; i++) {
        ASTNode astNode = selectedNodes[i];
        astNode.accept(new ASTVisitor() {

            private LinkedList<String> fOpenLoopLabels = new LinkedList<String>();

            private void registerLoopLabel(Statement node) {
                String identifier;
                if (node.getParent() instanceof LabeledStatement) {
                    LabeledStatement labeledStatement = (LabeledStatement) node.getParent();
                    identifier = labeledStatement.getLabel().getIdentifier();
                } else {
                    identifier = null;
                }
                fOpenLoopLabels.add(identifier);
            }

            @Override
            public boolean visit(ForStatement node) {
                registerLoopLabel(node);
                return super.visit(node);
            }

            @Override
            public void endVisit(ForStatement node) {
                fOpenLoopLabels.removeLast();
            }

            @Override
            public boolean visit(WhileStatement node) {
                registerLoopLabel(node);
                return super.visit(node);
            }

            @Override
            public void endVisit(WhileStatement node) {
                fOpenLoopLabels.removeLast();
            }

            @Override
            public boolean visit(EnhancedForStatement node) {
                registerLoopLabel(node);
                return super.visit(node);
            }

            @Override
            public void endVisit(EnhancedForStatement node) {
                fOpenLoopLabels.removeLast();
            }

            @Override
            public boolean visit(DoStatement node) {
                registerLoopLabel(node);
                return super.visit(node);
            }

            @Override
            public void endVisit(DoStatement node) {
                fOpenLoopLabels.removeLast();
            }

            @Override
            public void endVisit(ContinueStatement node) {
                final SimpleName label = node.getLabel();
                if (fOpenLoopLabels.isEmpty() || (label != null && !fOpenLoopLabels.contains(label.getIdentifier()))) {
                    TextEditGroup description = new TextEditGroup(RefactoringCoreMessages.ExtractMethodRefactoring_replace_continue);
                    result.addTextEditGroup(description);
                    ReturnStatement rs = fAST.newReturnStatement();
                    IVariableBinding returnValue = fAnalyzer.getReturnValue();
                    if (returnValue != null) {
                        rs.setExpression(fAST.newSimpleName(getName(returnValue)));
                    }
                    fRewriter.replace(node, rs, description);
                }
            }
        });
    }
}
Also used : 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) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) ExpressionStatement(org.eclipse.jdt.core.dom.ExpressionStatement) 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) SimpleName(org.eclipse.jdt.core.dom.SimpleName) WhileStatement(org.eclipse.jdt.core.dom.WhileStatement) IVariableBinding(org.eclipse.jdt.core.dom.IVariableBinding) LinkedList(java.util.LinkedList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) LabeledStatement(org.eclipse.jdt.core.dom.LabeledStatement) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) EnhancedForStatement(org.eclipse.jdt.core.dom.EnhancedForStatement) ForStatement(org.eclipse.jdt.core.dom.ForStatement) TextEditGroup(org.eclipse.text.edits.TextEditGroup) ContinueStatement(org.eclipse.jdt.core.dom.ContinueStatement)

Example 4 with LabeledStatement

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

Aggregations

ASTNode (org.eclipse.jdt.core.dom.ASTNode)8 DoStatement (org.eclipse.jdt.core.dom.DoStatement)8 EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)8 ForStatement (org.eclipse.jdt.core.dom.ForStatement)8 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)8 Statement (org.eclipse.jdt.core.dom.Statement)8 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)8 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)7 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)6 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)5 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)4 Block (org.eclipse.jdt.core.dom.Block)4 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)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 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