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

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

Introduction

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

Prototype

public EmptyStatement newEmptyStatement() 

Source Link

Document

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

Usage

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

License:Open Source License

@SuppressWarnings("unchecked")
@Override//w ww .  j  a v  a 2s  .  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.evosuite.testcarver.codegen.CodeGenerator.java

License:Open Source License

@SuppressWarnings("unchecked")
private void restorceCodeFromLastPosTo(final String packageName, final int oid, final int end,
        final boolean postprocessing, final AST ast, final Block block) {
    final int oidInfoRecNo = this.log.oidRecMapping.get(oid);

    // start from last OID modification point

    int currentRecord = log.getRecordIndexOfWhereObjectWasInitializedFirst(oid);
    if (currentRecord > 0) {
        // last modification of object happened here
        // -> we start looking for interesting records after retrieved record
        currentRecord++;// ww w.  jav a2s  . c  om
    } else {
        // object new instance statement
        // -> retrieved loc record no is included
        currentRecord = -currentRecord;
    }

    String methodName;
    int currentOID;
    Object[] methodArgs;
    Integer methodArgOID;

    Integer returnValue;
    Object returnValueObj;

    for (; currentRecord <= end; currentRecord++) {
        currentOID = this.log.objectIds.getQuick(currentRecord);
        returnValueObj = this.log.returnValues.get(currentRecord);
        returnValue = returnValueObj.equals(CaptureLog.RETURN_TYPE_VOID) ? -1 : (Integer) returnValueObj;

        if (oid == currentOID || returnValue == oid) {
            methodName = this.log.methodNames.get(currentRecord);

            if (CaptureLog.PLAIN_INIT.equals(methodName)) // e.g. String var = "Hello World";
            {
                PostProcessor.notifyRecentlyProcessedLogRecNo(currentRecord);
                this.createPlainInitStmt(currentRecord, block, ast);

                currentRecord = findEndOfMethod(currentRecord, currentOID);

                this.updateInitRec(currentOID, currentRecord);
            } else if (CaptureLog.NOT_OBSERVED_INIT.equals(methodName)) // e.g. Person var = (Person) XSTREAM.fromXML("<xml/>");
            {
                PostProcessor.notifyRecentlyProcessedLogRecNo(currentRecord);
                this.createUnobservedInitStmt(currentRecord, block, ast);

                currentRecord = findEndOfMethod(currentRecord, currentOID);
            } else if (CaptureLog.PUTFIELD.equals(methodName) || CaptureLog.PUTSTATIC.equals(methodName) || // field write access such as p.id = id or Person.staticVar = "something"
                    CaptureLog.GETFIELD.equals(methodName) || CaptureLog.GETSTATIC.equals(methodName)) // field READ access such as "int a =  p.id" or "String var = Person.staticVar"
            {

                if (CaptureLog.PUTFIELD.equals(methodName) || CaptureLog.PUTSTATIC.equals(methodName)) {
                    // a field assignment has always one argument
                    methodArgs = this.log.params.get(currentRecord);
                    methodArgOID = (Integer) methodArgs[0];
                    if (methodArgOID != null && methodArgOID != oid) {
                        // create history of assigned value
                        this.restorceCodeFromLastPosTo(packageName, methodArgOID, currentRecord, postprocessing,
                                ast, block);
                    }

                    this.createFieldWriteAccessStmt(packageName, currentRecord, block, ast);
                } else {
                    this.createFieldReadAccessStmt(packageName, currentRecord, block, ast);
                }

                currentRecord = findEndOfMethod(currentRecord, currentOID);

                if (CaptureLog.GETFIELD.equals(methodName) || CaptureLog.GETSTATIC.equals(methodName)) {
                    // GETFIELD and GETSTATIC should only happen, if we obtain an instance whose creation has not been observed
                    this.updateInitRec(currentOID, currentRecord);

                    if (returnValue != -1) {
                        this.updateInitRec(returnValue, currentRecord);
                    }
                }
            } else // var0.call(someArg) or Person var0 = new Person()
            {
                methodArgs = this.log.params.get(currentRecord);

                for (int i = 0; i < methodArgs.length; i++) {
                    // there can only be OIDs or null
                    methodArgOID = (Integer) methodArgs[i];
                    if (methodArgOID != null && methodArgOID != oid) {
                        this.restorceCodeFromLastPosTo(packageName, methodArgOID, currentRecord, postprocessing,
                                ast, block);
                    }
                }

                PostProcessor.notifyRecentlyProcessedLogRecNo(currentRecord);
                this.createMethodCallStmt(packageName, currentRecord, postprocessing, block, ast);
                block.statements().add(ast.newEmptyStatement());

                // forward to end of method call sequence

                currentRecord = findEndOfMethod(currentRecord, currentOID);

                // each method call is considered as object state modification -> so save last object modification
                this.updateInitRec(currentOID, currentRecord);

                if (returnValue != -1) {
                    // if returnValue has not type VOID, mark current log record as record where the return value instance was created
                    // --> if an object is created within an observed method, it would not be semantically correct
                    //     (and impossible to handle properly) to create an extra instance of the return value type outside this method
                    this.updateInitRec(returnValue, currentRecord);
                }

                // consider each passed argument as being modified at the end of the method call sequence
                for (int i = 0; i < methodArgs.length; i++) {
                    // there can only be OIDs or null
                    methodArgOID = (Integer) methodArgs[i];
                    if (methodArgOID != null && methodArgOID != oid) {
                        this.updateInitRec(methodArgOID, currentRecord);
                    }
                }
            }
        }
    }
}