Java Code Examples for org.codehaus.groovy.ast.stmt.BlockStatement#addStatements()

The following examples show how to use org.codehaus.groovy.ast.stmt.BlockStatement#addStatements() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GradleModellingLanguageTransformer.java    From pushfish-android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void call(SourceUnit source) throws CompilationFailedException {
    ModelRegistryDslHelperStatementGenerator statementGenerator = new ModelRegistryDslHelperStatementGenerator();
    BlockStatement rootStatementBlock = source.getAST().getStatementBlock();
    ListIterator<Statement> statementsIterator = rootStatementBlock.getStatements().listIterator();
    while (statementsIterator.hasNext()) {
        Statement statement = statementsIterator.next();
        ScriptBlock scriptBlock = AstUtils.detectScriptBlock(statement, SCRIPT_BLOCK_NAMES);
        if (scriptBlock != null) {
            statementsIterator.remove();
            ScopeVisitor scopeVisitor = new ScopeVisitor(source, statementGenerator);
            scriptBlock.getClosureExpression().getCode().visit(scopeVisitor);
        }
    }
    rootStatementBlock.addStatements(statementGenerator.getGeneratedStatements());
}
 
Example 2
Source File: ClassNode.java    From groovy with Apache License 2.0 6 votes vote down vote up
public void addStaticInitializerStatements(List<Statement> staticStatements, boolean fieldInit) {
    MethodNode method = getOrAddStaticConstructorNode();
    BlockStatement block = getCodeAsBlock(method);

    // while anything inside a static initializer block is appended
    // we don't want to append in the case we have a initialization
    // expression of a static field. In that case we want to add
    // before the other statements
    if (!fieldInit) {
        block.addStatements(staticStatements);
    } else {
        List<Statement> blockStatements = block.getStatements();
        staticStatements.addAll(blockStatements);
        blockStatements.clear();
        blockStatements.addAll(staticStatements);
    }
}
 
Example 3
Source File: GradleModellingLanguageTransformer.java    From Pushjet-Android with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public void call(SourceUnit source) throws CompilationFailedException {
    ModelRegistryDslHelperStatementGenerator statementGenerator = new ModelRegistryDslHelperStatementGenerator();
    BlockStatement rootStatementBlock = source.getAST().getStatementBlock();
    ListIterator<Statement> statementsIterator = rootStatementBlock.getStatements().listIterator();
    while (statementsIterator.hasNext()) {
        Statement statement = statementsIterator.next();
        ScriptBlock scriptBlock = AstUtils.detectScriptBlock(statement, SCRIPT_BLOCK_NAMES);
        if (scriptBlock != null) {
            statementsIterator.remove();
            ScopeVisitor scopeVisitor = new ScopeVisitor(source, statementGenerator);
            scriptBlock.getClosureExpression().getCode().visit(scopeVisitor);
        }
    }
    rootStatementBlock.addStatements(statementGenerator.getGeneratedStatements());
}
 
Example 4
Source File: SortableASTTransformation.java    From groovy with Apache License 2.0 5 votes vote down vote up
private static Statement createCompareToMethodBody(List<PropertyNode> properties, boolean reversed) {
    List<Statement> statements = new ArrayList<Statement>();

    // if (this.is(other)) return 0;
    statements.add(ifS(callThisX("is", args(OTHER)), returnS(constX(0))));

    if (properties.isEmpty()) {
        // perhaps overkill but let compareTo be based on hashes for commutativity
        // return this.hashCode() <=> other.hashCode()
        statements.add(declS(localVarX(THIS_HASH, ClassHelper.Integer_TYPE), callX(varX("this"), "hashCode")));
        statements.add(declS(localVarX(OTHER_HASH, ClassHelper.Integer_TYPE), callX(varX(OTHER), "hashCode")));
        statements.add(returnS(compareExpr(varX(THIS_HASH), varX(OTHER_HASH), reversed)));
    } else {
        // int value = 0;
        statements.add(declS(localVarX(VALUE, ClassHelper.int_TYPE), constX(0)));
        for (PropertyNode property : properties) {
            String propName = property.getName();
            // value = this.prop <=> other.prop;
            statements.add(assignS(varX(VALUE), compareExpr(propX(varX("this"), propName), propX(varX(OTHER), propName), reversed)));
            // if (value != 0) return value;
            statements.add(ifS(neX(varX(VALUE), constX(0)), returnS(varX(VALUE))));
        }
        // objects are equal
        statements.add(returnS(constX(0)));
    }

    final BlockStatement body = new BlockStatement();
    body.addStatements(statements);
    return body;
}