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

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

Introduction

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

Prototype

public LabeledStatement newLabeledStatement() 

Source Link

Document

Creates a new unparented labeled statement node owned by this AST.

Usage

From source file:com.google.devtools.j2cpp.translate.Rewriter.java

License:Open Source License

@SuppressWarnings("unchecked")
@Override//from w  ww  .ja va  2 s. c  o m
public boolean visit(LabeledStatement node) {
    Statement s = node.getBody();
    Statement statementBody = null;
    if (s instanceof DoStatement) {
        statementBody = ((DoStatement) s).getBody();
    } else if (s instanceof EnhancedForStatement) {
        statementBody = ((EnhancedForStatement) s).getBody();
    } else if (s instanceof ForStatement) {
        statementBody = ((ForStatement) s).getBody();
    } else if (s instanceof WhileStatement) {
        statementBody = ((WhileStatement) s).getBody();
    }
    if (statementBody != null) {
        AST ast = node.getAST();

        final boolean[] hasContinue = new boolean[1];
        final boolean[] hasBreak = new boolean[1];
        node.accept(new ASTVisitor() {
            @Override
            public void endVisit(ContinueStatement node) {
                if (node.getLabel() != null) {
                    hasContinue[0] = true;
                }
            }

            @Override
            public void endVisit(BreakStatement node) {
                if (node.getLabel() != null) {
                    hasBreak[0] = true;
                }
            }
        });

        List<Statement> stmts = null;
        if (hasContinue[0]) {
            if (statementBody instanceof Block) {
                // Add empty labeled statement as last block statement.
                stmts = ((Block) statementBody).statements();
                LabeledStatement newLabel = ast.newLabeledStatement();
                newLabel.setLabel(NodeCopier.copySubtree(ast, node.getLabel()));
                newLabel.setBody(ast.newEmptyStatement());
                stmts.add(newLabel);
            }
        }
        if (hasBreak[0]) {
            ASTNode parent = node.getParent();
            if (parent instanceof Block) {
                stmts = ((Block) parent).statements();
            } else {
                // Surround parent with block.
                Block block = ast.newBlock();
                stmts = block.statements();
                stmts.add((Statement) parent);

                // Replace parent in its statement list with new block.
                List<Statement> superStmts = ((Block) parent.getParent()).statements();
                for (int i = 0; i < superStmts.size(); i++) {
                    if (superStmts.get(i) == parent) {
                        superStmts.set(i, block);
                        break;
                    }
                }
                stmts = block.statements();
            }
            // Find node in statement list, and add empty labeled statement after it.
            for (int i = 0; i < stmts.size(); i++) {
                if (stmts.get(i) == node) {
                    LabeledStatement newLabel = ast.newLabeledStatement();
                    newLabel.setLabel(NodeCopier.copySubtree(ast, node.getLabel()));
                    newLabel.setBody(ast.newEmptyStatement());
                    stmts.add(i + 1, newLabel);
                    break;
                }
            }
        }

        if (hasContinue[0] || hasBreak[0]) {
            // Replace this node with its statement, thus deleting the label.
            ASTNode parent = node.getParent();
            if (parent instanceof Block) {
                stmts = ((Block) parent).statements();
                for (int i = 0; i < stmts.size(); i++) {
                    if (stmts.get(i) == node) {
                        stmts.set(i, NodeCopier.copySubtree(ast, node.getBody()));
                        break;
                    }
                }
            }
        }
    }
    return true;
}

From source file:org.jboss.forge.roaster.model.impl.BlockImpl.java

License:Open Source License

public org.eclipse.jdt.core.dom.Statement wireAndGetStatement(
        org.jboss.forge.roaster.model.statements.Statement<O, B, ?> statement, B parent, AST ast) {
    ASTNode<? extends Statement> node = (ASTNode<? extends Statement>) statement;
    statement.setOrigin(parent);/*from  ww  w .j  a  v a  2 s  .c om*/
    node.setAst(ast);
    Statement stat = node.materialize(ast);

    if (statement.hasLabel()) {
        LabeledStatement labeledStatement = ast.newLabeledStatement();
        labeledStatement.setBody(stat);
        labeledStatement.setLabel(ast.newSimpleName(statement.getLabel()));
        return labeledStatement;
    } else {
        return stat;
    }
}

From source file:org.jboss.forge.roaster.model.impl.statements.BodiedStatementImpl.java

License:Open Source License

public org.eclipse.jdt.core.dom.Statement wireAndGetStatement(Statement<O, S, ?> statement, S parent, AST ast) {
    ASTNode<J> node = (ASTNode<J>) statement;
    statement.setOrigin(parent);/*w w  w  .ja  va  2 s  .c  o m*/
    node.setAst(ast);
    org.eclipse.jdt.core.dom.Statement stat = node.materialize(ast);

    if (statement.hasLabel()) {
        LabeledStatement labeledStatement = ast.newLabeledStatement();
        labeledStatement.setBody(stat);
        labeledStatement.setLabel(ast.newSimpleName(statement.getLabel()));
        return labeledStatement;
    } else {
        return stat;
    }
}