Search in sources :

Example 26 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project grails-core by grails.

the class TestMixinTransformation method getOrCreateMethodBody.

protected static BlockStatement getOrCreateMethodBody(ClassNode classNode, MethodNode methodNode, String name) {
    BlockStatement methodBody;
    if (!methodNode.getDeclaringClass().equals(classNode)) {
        methodBody = new BlockStatement();
        methodNode = new MethodNode(name, Modifier.PUBLIC, methodNode.getReturnType(), GrailsArtefactClassInjector.ZERO_PARAMETERS, null, methodBody);
        classNode.addMethod(methodNode);
    } else {
        final Statement setupMethodBody = methodNode.getCode();
        if (!(setupMethodBody instanceof BlockStatement)) {
            methodBody = new BlockStatement();
            if (setupMethodBody != null) {
                if (!(setupMethodBody instanceof ReturnStatement)) {
                    methodBody.addStatement(setupMethodBody);
                }
            }
            methodNode.setCode(methodBody);
        } else {
            methodBody = (BlockStatement) setupMethodBody;
        }
    }
    return methodBody;
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 27 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.

the class ModuleNode method createStatementsClass.

protected ClassNode createStatementsClass() {
    ClassNode classNode = getScriptClassDummy();
    if (classNode.getName().endsWith("package-info")) {
        return classNode;
    }
    handleMainMethodIfPresent(methods);
    // return new Foo(new ShellContext(args)).run()
    classNode.addMethod(new MethodNode("main", ACC_PUBLIC | ACC_STATIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(ClassHelper.STRING_TYPE.makeArray(), "args") }, ClassNode.EMPTY_ARRAY, new ExpressionStatement(new MethodCallExpression(new ClassExpression(ClassHelper.make(InvokerHelper.class)), "runScript", new ArgumentListExpression(new ClassExpression(classNode), new VariableExpression("args"))))));
    MethodNode methodNode = new MethodNode("run", ACC_PUBLIC, ClassHelper.OBJECT_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, statementBlock);
    methodNode.setIsScriptBody();
    classNode.addMethod(methodNode);
    classNode.addConstructor(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BlockStatement());
    Statement stmt;
    // (like @BaseScript) that could change this.  But this is cautious and anticipates possible compiler changes.
    if (classNode.getSuperClass().getDeclaredConstructor(SCRIPT_CONTEXT_CTOR) != null) {
        stmt = new ExpressionStatement(new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(new VariableExpression("context"))));
    } else {
        // Fallback for non-standard base "script" classes with no context (Binding) constructor.
        stmt = new ExpressionStatement(new MethodCallExpression(new VariableExpression("super"), "setBinding", new ArgumentListExpression(new VariableExpression("context"))));
    }
    classNode.addConstructor(ACC_PUBLIC, new Parameter[] { new Parameter(ClassHelper.make(Binding.class), "context") }, ClassNode.EMPTY_ARRAY, stmt);
    for (MethodNode node : methods) {
        int modifiers = node.getModifiers();
        if ((modifiers & ACC_ABSTRACT) != 0) {
            throw new RuntimeException("Cannot use abstract methods in a script, they are only available inside classes. Method: " + node.getName());
        }
        // br: the old logic seems to add static to all def f().... in a script, which makes enclosing
        // inner classes (including closures) in a def function difficult. Comment it out.
        node.setModifiers(modifiers);
        classNode.addMethod(node);
    }
    return classNode;
}
Also used : InvokerHelper(org.codehaus.groovy.runtime.InvokerHelper) Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 28 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.

the class ClassNode method addStaticInitializerStatements.

public void addStaticInitializerStatements(List<Statement> staticStatements, boolean fieldInit) {
    MethodNode method = getOrAddStaticConstructorNode();
    BlockStatement block = null;
    Statement statement = method.getCode();
    if (statement == null) {
        block = new BlockStatement();
    } else if (statement instanceof BlockStatement) {
        block = (BlockStatement) statement;
    } else {
        block = new BlockStatement();
        block.addStatement(statement);
    }
    // before the other statements
    if (!fieldInit) {
        block.addStatements(staticStatements);
    } else {
        List<Statement> blockStatements = block.getStatements();
        staticStatements.addAll(blockStatements);
        blockStatements.clear();
        blockStatements.addAll(staticStatements);
    }
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 29 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project groovy-core by groovy.

the class ClassNode method positionStmtsAfterEnumInitStmts.

public void positionStmtsAfterEnumInitStmts(List<Statement> staticFieldStatements) {
    MethodNode method = getOrAddStaticConstructorNode();
    Statement statement = method.getCode();
    if (statement instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) statement;
        // add given statements for explicitly declared static fields just after enum-special fields
        // are found - the $VALUES binary expression marks the end of such fields.
        List<Statement> blockStatements = block.getStatements();
        ListIterator<Statement> litr = blockStatements.listIterator();
        while (litr.hasNext()) {
            Statement stmt = litr.next();
            if (stmt instanceof ExpressionStatement && ((ExpressionStatement) stmt).getExpression() instanceof BinaryExpression) {
                BinaryExpression bExp = (BinaryExpression) ((ExpressionStatement) stmt).getExpression();
                if (bExp.getLeftExpression() instanceof FieldExpression) {
                    FieldExpression fExp = (FieldExpression) bExp.getLeftExpression();
                    if (fExp.getFieldName().equals("$VALUES")) {
                        for (Statement tmpStmt : staticFieldStatements) {
                            litr.add(tmpStmt);
                        }
                    }
                }
            }
        }
    }
}
Also used : Statement(org.codehaus.groovy.ast.stmt.Statement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Example 30 with BlockStatement

use of org.codehaus.groovy.ast.stmt.BlockStatement in project grails-core by grails.

the class GroovyPageBytecodeOptimizer method performInjection.

public void performInjection(SourceUnit source, GeneratorContext context, ClassNode classNode) {
    // search run method in GSP script and get codeblock
    MethodNode runMethod = classNode.getMethod(RUN_METHOD, new Parameter[0]);
    if (runMethod != null && runMethod.getCode() instanceof BlockStatement) {
        BlockStatement block = (BlockStatement) runMethod.getCode();
        //scan all MethodExpressionCalls to optimize them
        GroovyPageOptimizerVisitor groovyPageVisitor = new GroovyPageOptimizerVisitor(classNode);
        groovyPageVisitor.visitBlockStatement(block);
    }
}
Also used : MethodNode(org.codehaus.groovy.ast.MethodNode) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement)

Aggregations

BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)199 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)85 Statement (org.codehaus.groovy.ast.stmt.Statement)81 Expression (org.codehaus.groovy.ast.expr.Expression)72 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)65 MethodNode (org.codehaus.groovy.ast.MethodNode)55 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)52 ClassNode (org.codehaus.groovy.ast.ClassNode)51 Parameter (org.codehaus.groovy.ast.Parameter)50 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)45 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)41 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)41 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)38 FieldNode (org.codehaus.groovy.ast.FieldNode)37 ClosureExpression (org.codehaus.groovy.ast.expr.ClosureExpression)36 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)33 ArrayList (java.util.ArrayList)31 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)27 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)27 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)25