Search in sources :

Example 11 with WhileStatement

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

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

the class AdvancedQuickAssistProcessor method isLastStatementInEnclosingMethodOrLambda.

private static boolean isLastStatementInEnclosingMethodOrLambda(Statement statement) {
    ASTNode currentStructure = statement;
    ASTNode currentParent = statement.getParent();
    while (!(currentParent instanceof MethodDeclaration || currentParent instanceof LambdaExpression)) {
        // should not be in a loop
        if (currentParent instanceof ForStatement || currentParent instanceof EnhancedForStatement || currentParent instanceof WhileStatement || currentParent instanceof DoStatement) {
            return false;
        }
        if (currentParent instanceof Block) {
            Block parentBlock = (Block) currentParent;
            if (parentBlock.statements().indexOf(currentStructure) != parentBlock.statements().size() - 1) {
                // not last statement in the block
                return false;
            }
        }
        currentStructure = currentParent;
        currentParent = currentParent.getParent();
    }
    return true;
}
Also used : DoStatement(org.eclipse.jdt.core.dom.DoStatement) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) 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) LambdaExpression(org.eclipse.jdt.core.dom.LambdaExpression)

Aggregations

EnhancedForStatement (org.eclipse.jdt.core.dom.EnhancedForStatement)12 ForStatement (org.eclipse.jdt.core.dom.ForStatement)12 WhileStatement (org.eclipse.jdt.core.dom.WhileStatement)12 DoStatement (org.eclipse.jdt.core.dom.DoStatement)11 Statement (org.eclipse.jdt.core.dom.Statement)11 ASTNode (org.eclipse.jdt.core.dom.ASTNode)10 Block (org.eclipse.jdt.core.dom.Block)10 IfStatement (org.eclipse.jdt.core.dom.IfStatement)9 ReturnStatement (org.eclipse.jdt.core.dom.ReturnStatement)9 VariableDeclarationStatement (org.eclipse.jdt.core.dom.VariableDeclarationStatement)7 ContinueStatement (org.eclipse.jdt.core.dom.ContinueStatement)5 ExpressionStatement (org.eclipse.jdt.core.dom.ExpressionStatement)5 LabeledStatement (org.eclipse.jdt.core.dom.LabeledStatement)5 SwitchStatement (org.eclipse.jdt.core.dom.SwitchStatement)5 AST (org.eclipse.jdt.core.dom.AST)4 BreakStatement (org.eclipse.jdt.core.dom.BreakStatement)4 LambdaExpression (org.eclipse.jdt.core.dom.LambdaExpression)4 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)4 AssertStatement (org.eclipse.jdt.core.dom.AssertStatement)3 CastExpression (org.eclipse.jdt.core.dom.CastExpression)3