Example usage for org.eclipse.jdt.core.dom Statement getParent

List of usage examples for org.eclipse.jdt.core.dom Statement getParent

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom Statement getParent.

Prototype

public final ASTNode getParent() 

Source Link

Document

Returns this node's parent node, or null if this is the root node.

Usage

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.editors.JavaMessageGrouper.java

License:Open Source License

/**
 * Updates labels and colours for the grouping.
 * @param currentGrouping/*from   w ww  .j  a  va 2 s. co m*/
 */
private void updateGrouping(MappedMessageGrouping grouping) {
    ASTNode node = (ASTNode) grouping.getKey();
    String text = "";
    int i;
    Color bg = null;
    Color fg = null;
    switch (node.getNodeType()) {
    case ASTNode.IF_STATEMENT:
        IfStatement ifStatement = (IfStatement) node;
        text = "if (" + ifStatement.getExpression().toString() + ")";
        //it could be an else-if, make sure
        if (ifStatement.getParent().getNodeType() == ASTNode.IF_STATEMENT) {
            if (ifStatement.equals(((IfStatement) ifStatement.getParent()).getElseStatement())) {
                text = "else " + text;
            }
        }
        fg = ISketchColorConstants.CONDITION_FG;
        bg = ISketchColorConstants.CONDITION_BG;
        break;
    case ASTNode.WHILE_STATEMENT:
        WhileStatement whileStatement = (WhileStatement) node;
        text = "while (" + whileStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.DO_STATEMENT:
        DoStatement doStatement = (DoStatement) node;
        text = "do..while (" + doStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.FOR_STATEMENT:
        ForStatement forStatement = (ForStatement) node;
        List<?> initializers = forStatement.initializers();
        List<?> updaters = forStatement.updaters();
        text = "for (";
        for (i = 0; i < initializers.size(); i++) {
            text += initializers.get(i).toString();
            if (i < initializers.size() - 1) {
                text += ",";
            }
        }
        text += ";";
        if (forStatement.getExpression() != null) {
            text += forStatement.getExpression();
        }
        text += ";";
        for (i = 0; i < updaters.size(); i++) {
            text += updaters.get(i).toString();
            if (i < updaters.size() - 1) {
                text += ",";
            }
        }
        text += ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.ENHANCED_FOR_STATEMENT:
        EnhancedForStatement enhancedForStatement = (EnhancedForStatement) node;
        text = "for (" + enhancedForStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.TRY_STATEMENT:
        text = "try";
        fg = ISketchColorConstants.ERROR_FG;
        bg = ISketchColorConstants.ERROR_BG;
        break;
    case ASTNode.CATCH_CLAUSE:
        CatchClause catchClause = (CatchClause) node;
        text = "catch (" + catchClause.getException().toString() + ")";
        fg = ISketchColorConstants.ERROR_FG;
        bg = ISketchColorConstants.ERROR_BG;
        break;
    default:
        //get the else blocks
        if (node instanceof Statement) {
            Statement statement = (Statement) node;
            if (statement.getParent() instanceof IfStatement) {
                if (((IfStatement) statement.getParent()).getElseStatement() == statement) {
                    text = "else";
                    fg = ISketchColorConstants.CONDITION_FG;
                    bg = ISketchColorConstants.CONDITION_BG;
                }
            }
        }
        break;
    }

    grouping.setName(text);
    grouping.setForeground(fg);
    grouping.setBackground(bg);
}

From source file:ca.uvic.chisel.diver.sequencediagrams.sc.java.editors.JavaMessageGrouper.java

License:Open Source License

/**
 * @param messageNode/*from  ww  w.j a v  a  2 s.c  o  m*/
 * @return
 */
private static ASTNode findBlockParent(ASTNode messageNode) {
    if (messageNode.getNodeType() == ASTNode.METHOD_DECLARATION) {
        return messageNode;
    }
    //search through the tree, up through the parents to find the nearest block
    ASTNode parent = messageNode.getParent();
    while (parent != null) {
        switch (parent.getNodeType()) {
        case ASTNode.IF_STATEMENT:
        case ASTNode.WHILE_STATEMENT:
        case ASTNode.FOR_STATEMENT:
        case ASTNode.DO_STATEMENT:
        case ASTNode.ENHANCED_FOR_STATEMENT:
        case ASTNode.TRY_STATEMENT:
        case ASTNode.CATCH_CLAUSE:
        case ASTNode.METHOD_DECLARATION:
            return parent;
        default:
            //get the else blocks
            if (parent instanceof Statement) {
                Statement statement = (Statement) parent;
                if (statement.getParent() instanceof IfStatement) {
                    if (((IfStatement) statement.getParent()).getElseStatement() == statement) {
                        return statement;
                    }
                }
            }
            break;
        }
        parent = parent.getParent();
    }
    return null;
}

From source file:ca.uvic.chisel.javasketch.ui.internal.presentation.ASTMessageGrouper.java

License:Open Source License

/**
 * Updates labels and colours for the grouping.
 * @param currentGrouping/*from   w w  w .  j a va2s .c om*/
 */
private void updateGrouping(ASTMessageGrouping grouping, ASTNode node) {
    String text = "";
    int i;
    Color bg = null;
    Color fg = null;
    switch (node.getNodeType()) {
    case ASTNode.IF_STATEMENT:
        IfStatement ifStatement = (IfStatement) node;
        text = "if (" + ifStatement.getExpression().toString() + ")";
        //it could be an else-if, make sure
        if (ifStatement.getParent().getNodeType() == ASTNode.IF_STATEMENT) {
            if (ifStatement.equals(((IfStatement) ifStatement.getParent()).getElseStatement())) {
                text = "else " + text;
            }
        }
        fg = ISketchColorConstants.CONDITION_FG;
        bg = ISketchColorConstants.CONDITION_BG;
        break;
    case ASTNode.WHILE_STATEMENT:
        WhileStatement whileStatement = (WhileStatement) node;
        text = "while (" + whileStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.DO_STATEMENT:
        DoStatement doStatement = (DoStatement) node;
        text = "do..while (" + doStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.FOR_STATEMENT:
        ForStatement forStatement = (ForStatement) node;
        List<?> initializers = forStatement.initializers();
        List<?> updaters = forStatement.updaters();
        text = "for (";
        for (i = 0; i < initializers.size(); i++) {
            text += initializers.get(i).toString();
            if (i < initializers.size() - 1) {
                text += ",";
            }
        }
        text += ";";
        if (forStatement.getExpression() != null) {
            text += forStatement.getExpression();
        }
        text += ";";
        for (i = 0; i < updaters.size(); i++) {
            text += updaters.get(i).toString();
            if (i < updaters.size() - 1) {
                text += ",";
            }
        }
        text += ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.ENHANCED_FOR_STATEMENT:
        EnhancedForStatement enhancedForStatement = (EnhancedForStatement) node;
        text = "for (" + enhancedForStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.TRY_STATEMENT:
        text = "try";
        fg = ISketchColorConstants.ERROR_FG;
        bg = ISketchColorConstants.ERROR_BG;
        break;
    case ASTNode.CATCH_CLAUSE:
        CatchClause catchClause = (CatchClause) node;
        text = "catch (" + catchClause.getException().toString() + ")";
        fg = ISketchColorConstants.ERROR_FG;
        bg = ISketchColorConstants.ERROR_BG;
        break;
    default:
        //get the else blocks
        if (node instanceof Statement) {
            Statement statement = (Statement) node;
            if (statement.getParent() instanceof IfStatement) {
                if (((IfStatement) statement.getParent()).getElseStatement() == statement) {
                    text = "else";
                    fg = ISketchColorConstants.CONDITION_FG;
                    bg = ISketchColorConstants.CONDITION_BG;
                }
            }
        }
        break;
    }
    if (grouping.node.isLoop()) {
        ASTMessageGroupingTree[] siblings = grouping.node.getSiblings();
        text = text + "[" + grouping.node.getIteration() + " of " + (siblings.length + 1) + "]";
    }

    grouping.setName(text);
    grouping.setForeground(fg);
    grouping.setBackground(bg);
}

From source file:ca.uvic.chisel.javasketch.ui.internal.presentation.CopyOfASTMessageGrouper.java

License:Open Source License

/**
 * Updates labels and colours for the grouping.
 * @param currentGrouping/* w ww  .j a v  a  2s.  co m*/
 */
private void updateGrouping(ASTMessageGrouping grouping, ASTNode node) {
    String text = "";
    int i;
    Color bg = null;
    Color fg = null;
    switch (node.getNodeType()) {
    case ASTNode.IF_STATEMENT:
        IfStatement ifStatement = (IfStatement) node;
        text = "if (" + ifStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.CONDITION_FG;
        bg = ISketchColorConstants.CONDITION_BG;
        break;
    case ASTNode.WHILE_STATEMENT:
        WhileStatement whileStatement = (WhileStatement) node;
        text = "while (" + whileStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.DO_STATEMENT:
        DoStatement doStatement = (DoStatement) node;
        text = "do..while (" + doStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.FOR_STATEMENT:
        ForStatement forStatement = (ForStatement) node;
        List<?> initializers = forStatement.initializers();
        List<?> updaters = forStatement.updaters();
        text = "for (";
        for (i = 0; i < initializers.size(); i++) {
            text += initializers.get(i).toString();
            if (i < initializers.size() - 1) {
                text += ",";
            }
        }
        text += ";";
        if (forStatement.getExpression() != null) {
            text += forStatement.getExpression();
        }
        text += ";";
        for (i = 0; i < updaters.size(); i++) {
            text += updaters.get(i).toString();
            if (i < updaters.size() - 1) {
                text += ",";
            }
        }
        text += ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.ENHANCED_FOR_STATEMENT:
        EnhancedForStatement enhancedForStatement = (EnhancedForStatement) node;
        text = "for (" + enhancedForStatement.getExpression().toString() + ")";
        fg = ISketchColorConstants.LOOP_FG;
        bg = ISketchColorConstants.LOOP_BG;
        break;
    case ASTNode.TRY_STATEMENT:
        text = "try";
        fg = ISketchColorConstants.ERROR_FG;
        bg = ISketchColorConstants.ERROR_BG;
        break;
    case ASTNode.CATCH_CLAUSE:
        CatchClause catchClause = (CatchClause) node;
        text = "catch (" + catchClause.getException().toString() + ")";
        fg = ISketchColorConstants.ERROR_FG;
        bg = ISketchColorConstants.ERROR_BG;
        break;
    default:
        //get the else blocks
        if (node instanceof Statement) {
            Statement statement = (Statement) node;
            if (statement.getParent() instanceof IfStatement) {
                if (((IfStatement) statement.getParent()).getElseStatement() == statement) {
                    text = "else";
                    fg = ISketchColorConstants.CONDITION_FG;
                    bg = ISketchColorConstants.CONDITION_BG;
                }
            }
        }
        break;
    }
    if (grouping.node.isLoop()) {
        ASTMessageGroupingTree[] siblings = grouping.node.getSiblings();
        text = text + "[" + grouping.node.getIteration() + " of " + (siblings.length + 1) + "]";
    }
    grouping.setName(text);
    grouping.setForeground(fg);
    grouping.setBackground(bg);
}

From source file:com.google.devtools.j2objc.ast.TreeUtil.java

License:Apache License

/**
 * Returns the given statement as a list of statements that can be added to.
 * If node is a Block, then returns it's statement list. If node is the direct
 * child of a Block, returns the sublist containing node as the only element.
 * Otherwise, creates a new Block node in the place of node and returns its
 * list of statements./*  w  ww .  j  a v a 2s.c  o m*/
 */
public static List<Statement> asStatementList(Statement node) {
    if (node instanceof Block) {
        return ((Block) node).getStatements();
    }
    TreeNode parent = node.getParent();
    if (parent instanceof Block) {
        List<Statement> stmts = ((Block) parent).getStatements();
        for (int i = 0; i < stmts.size(); i++) {
            if (stmts.get(i) == node) {
                return stmts.subList(i, i + 1);
            }
        }
    }
    return new LonelyStatementList(node);
}

From source file:com.google.devtools.j2objc.util.ASTUtil.java

License:Apache License

/**
 * Returns the given statement as a list of statements that can be added to.
 * If node is a Block, then returns it's statement list. If node is the direct
 * child of a Block, returns the sublist containing node as the only element.
 * Otherwise, creates a new Block node in the place of node and returns its
 * list of statements.//from  ww  w.  j  a  v a  2s .c o m
 */
public static List<Statement> asStatementList(Statement node) {
    if (node instanceof Block) {
        return getStatements((Block) node);
    }
    ASTNode parent = node.getParent();
    if (parent instanceof Block) {
        List<Statement> stmts = getStatements((Block) parent);
        for (int i = 0; i < stmts.size(); i++) {
            if (stmts.get(i) == node) {
                return stmts.subList(i, i + 1);
            }
        }
    }
    Block block = node.getAST().newBlock();
    setProperty(node, block);
    getStatements(block).add(node);
    return getStatements(block);
}

From source file:de.ovgu.cide.export.physical.ahead.JakFeatureRefactorer.java

License:Open Source License

static void replaceStatement(Statement target, Statement replacement) {
    ASTNode p = target.getParent();

    if (target instanceof Block && !(replacement instanceof Block)) {
        Block b = replacement.getAST().newBlock();
        b.statements().add(replacement);
        replacement = b;//w  ww .  ja v a 2 s.c o m
    }

    StructuralPropertyDescriptor prop = target.getLocationInParent();
    if (prop.isSimpleProperty() || prop.isChildProperty()) {
        p.setStructuralProperty(prop, replacement);
    } else if (prop.isChildListProperty()) {
        assert false;
    }

}

From source file:de.ovgu.cide.export.physical.ahead.JakHookMethodHelper.java

License:Open Source License

/**
 * //  w  w  w  .  j av a  2 s .c o  m
 * @param statements
 * @param method
 * @param derivative
 * @param subtreeRuleException
 *            can be specified if a child of one of the statements should be
 *            preserved inside the hook declaration's body
 */
public JakHookMethodHelper(List<Statement> statements, MethodDeclaration method, Statement subtreeRuleException,
        RefactoringColorManager colorManager) {
    this.hookIdx = JakHookMethodHelper.hookIdxCounter++;
    this.name = "hook" + hookIdx;
    this.ast = method.getAST();
    this.statements = statements;
    this.thrownExceptions = calcThrownExceptions(method, statements);
    this.isStatic = isStatic(method);

    localVariableAnalyzer = new LocalVariableAnalyzer(method, statements, colorManager);
    localVariableAnalyzer.execute();
    parameters = LocalVariableAnalyzer.sortResult(localVariableAnalyzer.getParameters());
    returnValues = LocalVariableAnalyzer.sortResult(localVariableAnalyzer.getReturns());

    this.hasSubtreeRuleException = subtreeRuleException != null;
    if (hasSubtreeRuleException) {
        subtreeRuleExceptionParent = subtreeRuleException.getParent();
        this.subtreeRuleException = subtreeRuleException;
        subtreeRuleExceptionIsBlock = subtreeRuleException instanceof Block;
        exceptionPlaceholder = ast.newBlock();
        replaceSubtreeRuleExceptionByPlaceholder();
        assert subtreeRuleException.getParent() == null;
    }
}

From source file:edu.illinois.jflow.core.transformations.code.ExtractClosureRefactoring.java

License:Open Source License

private void createChannels(final CompilationUnitChange result) {
    Statement forStatement = locateEnclosingLoopStatement();

    TextEditGroup insertChannelDesc = new TextEditGroup(
            JFlowRefactoringCoreMessages.ExtractClosureRefactoring_channel_textedit_description);
    result.addTextEditGroup(insertChannelDesc);

    List<Statement> channelStatements = createChannelStatements();

    ChildListPropertyDescriptor forStatementDescriptor = (ChildListPropertyDescriptor) forStatement
            .getLocationInParent();/*ww w  .j a v  a  2 s  .com*/
    ListRewrite forStatementListRewrite = fRewriter.getListRewrite(forStatement.getParent(),
            forStatementDescriptor);

    // We use the loop at the anchor point and insert everything before it in alphabetical channel name order
    for (Statement stmt : channelStatements) {
        forStatementListRewrite.insertBefore(stmt, forStatement, insertChannelDesc);
    }
}

From source file:org.autorefactor.refactoring.ASTHelper.java

License:Open Source License

/**
 * Returns the previous statement in the source file if it exists.
 *
 * @param startNode the start node//from   w  w w  .  j a  va  2  s .co m
 * @return the previous statement in the source file if it exists, null otherwise
 */
public static Statement getPreviousStatement(Statement startNode) {
    final Statement previousSibling = getPreviousSibling(startNode);
    if (previousSibling != null) {
        return previousSibling;
    }
    final ASTNode parent = startNode.getParent();
    if (parent instanceof Statement) {
        return getPreviousStatement((Statement) parent);
    }
    return null;
}