Example usage for org.eclipse.jdt.core.dom AST AST

List of usage examples for org.eclipse.jdt.core.dom AST AST

Introduction

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

Prototype

public AST() 

Source Link

Document

Creates a new, empty abstract syntax tree using default options.

Usage

From source file:org.eclipse.umlgen.reverse.java.JavaReverseCUVisitor.java

License:Open Source License

/**
 * linkToLastStatement : look for last statement and call control flow creation simple statement.
 *
 * @param node//w  w  w.  j  a va2s.c o  m
 */
protected void linkToLastStatement(Statement node) {
    ActivityNode targetNode = null;
    ActivityNode sourceNode = null;
    // If last statement was a parent node, link its inner exit to us.
    if (lastStatement != null && parentStatements.contains(lastStatement)) {
        // if last statement has a designated exit map, use that
        if (innerExitMap.containsKey(lastStatement)) {
            sourceNode = innerExitMap.get(lastStatement);
        } else {
            // TODO Otherwise, we might be inside a region. Add a start point and link to it
            // FIXME maybe the parent can create the nodes instead
        }
    } else {
        LiteralString stringuard = UMLFactory.eINSTANCE.createLiteralString();
        stringuard.setValue("else");

        // Specific case : else statement
        // parentStatements does not contains lastStatement any more
        if (node.getParent().getNodeType() == ASTNode.IF_STATEMENT) {
            IfStatement parent = (IfStatement) node.getParent();
            if (choiceMap.containsKey(parent) && parent.getElseStatement() != null
                    && parent.getElseStatement().equals(node)) {
                sourceNode = choiceMap.get(parent);
                choiceMap.remove(parent);
                if (lastGuard == null) {
                    lastGuard = stringuard;
                }
            }
        }
        if (node.getParent().getNodeType() == ASTNode.BLOCK
                && node.getParent().getParent().getNodeType() == ASTNode.IF_STATEMENT) {
            IfStatement parent = (IfStatement) node.getParent().getParent();
            Block block;

            if (parent.getElseStatement() instanceof IfStatement) {
                AST ast = new AST();
                block = (Block) ast.createInstance(ASTNode.BLOCK);
                List<Statement> elseblock = new ArrayList<Statement>();
                elseblock.add(parent.getElseStatement());
            } else {
                block = (Block) parent.getElseStatement();
            }
            if (choiceMap.containsKey(parent) && block != null && block.statements().size() > 0
                    && block.statements().get(0).equals(node)) {
                sourceNode = choiceMap.get(parent);
                choiceMap.remove(parent);
                if (lastGuard == null) {
                    lastGuard = stringuard;
                }
            }
        }

    }

    // if this has a registered entry node, use it
    if (entryNodeMap.containsKey(node)) {
        targetNode = entryNodeMap.get(node);
        // if we haven't yet found a sourceNode, use the exitMap's or a new one
        if (sourceNode == null) {
            if (lastActivityNode != null) {
                sourceNode = lastActivityNode;
                lastActivityNode = null;
            } else if (lastStatement == null || !exitNodeMap.containsKey(lastStatement)) {
                sourceNode = initNode;
            } else if (exitNodeMap.containsKey(lastStatement)) {
                sourceNode = exitNodeMap.get(lastStatement);
            }
        }
    }
    createControlFlow(targetNode, sourceNode);
}