Search in sources :

Example 41 with ReturnStatement

use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.

the class InnerClassVisitorHelper method setPropertyGetterDispatcher.

protected static void setPropertyGetterDispatcher(BlockStatement block, Expression thiz, Parameter[] parameters) {
    List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>();
    gStringStrings.add(new ConstantExpression(""));
    gStringStrings.add(new ConstantExpression(""));
    List<Expression> gStringValues = new ArrayList<Expression>();
    gStringValues.add(new VariableExpression(parameters[0]));
    block.addStatement(new ReturnStatement(new PropertyExpression(thiz, new GStringExpression("$name", gStringStrings, gStringValues))));
}
Also used : ArrayList(java.util.ArrayList) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Example 42 with ReturnStatement

use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.

the class InnerClassVisitorHelper method setMethodDispatcherCode.

protected static void setMethodDispatcherCode(BlockStatement block, Expression thiz, Parameter[] parameters) {
    List<ConstantExpression> gStringStrings = new ArrayList<ConstantExpression>();
    gStringStrings.add(new ConstantExpression(""));
    gStringStrings.add(new ConstantExpression(""));
    List<Expression> gStringValues = new ArrayList<Expression>();
    gStringValues.add(new VariableExpression(parameters[0]));
    block.addStatement(new ReturnStatement(new MethodCallExpression(thiz, new GStringExpression("$name", gStringStrings, gStringValues), new ArgumentListExpression(new SpreadExpression(new VariableExpression(parameters[1]))))));
}
Also used : ArrayList(java.util.ArrayList) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Example 43 with ReturnStatement

use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.

the class AntlrParserPlugin method statement.

// Statements
//-------------------------------------------------------------------------
protected Statement statement(AST node) {
    Statement statement = null;
    int type = node.getType();
    switch(type) {
        case SLIST:
        case LITERAL_finally:
            statement = statementList(node);
            break;
        case METHOD_CALL:
            statement = methodCall(node);
            break;
        case VARIABLE_DEF:
            statement = variableDef(node);
            break;
        case LABELED_STAT:
            return labelledStatement(node);
        case LITERAL_assert:
            statement = assertStatement(node);
            break;
        case LITERAL_break:
            statement = breakStatement(node);
            break;
        case LITERAL_continue:
            statement = continueStatement(node);
            break;
        case LITERAL_if:
            statement = ifStatement(node);
            break;
        case LITERAL_for:
            statement = forStatement(node);
            break;
        case LITERAL_return:
            statement = returnStatement(node);
            break;
        case LITERAL_synchronized:
            statement = synchronizedStatement(node);
            break;
        case LITERAL_switch:
            statement = switchStatement(node);
            break;
        case LITERAL_try:
            statement = tryStatement(node);
            break;
        case LITERAL_throw:
            statement = throwStatement(node);
            break;
        case LITERAL_while:
            statement = whileStatement(node);
            break;
        default:
            statement = new ExpressionStatement(expression(node));
    }
    if (statement != null) {
        configureAST(statement, node);
    }
    return statement;
}
Also used : CaseStatement(org.codehaus.groovy.ast.stmt.CaseStatement) ForStatement(org.codehaus.groovy.ast.stmt.ForStatement) CatchStatement(org.codehaus.groovy.ast.stmt.CatchStatement) IfStatement(org.codehaus.groovy.ast.stmt.IfStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) Statement(org.codehaus.groovy.ast.stmt.Statement) WhileStatement(org.codehaus.groovy.ast.stmt.WhileStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) ThrowStatement(org.codehaus.groovy.ast.stmt.ThrowStatement) ContinueStatement(org.codehaus.groovy.ast.stmt.ContinueStatement) BreakStatement(org.codehaus.groovy.ast.stmt.BreakStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) SynchronizedStatement(org.codehaus.groovy.ast.stmt.SynchronizedStatement) EmptyStatement(org.codehaus.groovy.ast.stmt.EmptyStatement) SwitchStatement(org.codehaus.groovy.ast.stmt.SwitchStatement) TryCatchStatement(org.codehaus.groovy.ast.stmt.TryCatchStatement) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement)

Example 44 with ReturnStatement

use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovy by apache.

the class AntlrParserPlugin method returnStatement.

protected Statement returnStatement(AST node) {
    AST exprNode = node.getFirstChild();
    // This will pick up incorrect sibling node if 'node' is a plain 'return'
    //
    //if (exprNode == null) {
    //    exprNode = node.getNextSibling();
    //}
    Expression expression = exprNode == null ? ConstantExpression.NULL : expression(exprNode);
    ReturnStatement returnStatement = new ReturnStatement(expression);
    configureAST(returnStatement, node);
    return returnStatement;
}
Also used : AST(antlr.collections.AST) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement)

Example 45 with ReturnStatement

use of org.codehaus.groovy.ast.stmt.ReturnStatement in project groovity by disney.

the class GroovityASTTransformation method visit.

public void visit(ASTNode[] nodes, final SourceUnit sourceUnit) {
    ModuleNode mn = sourceUnit.getAST();
    try {
        if (mn != null) {
            ClassNode scriptClassNode = mn.getScriptClassDummy();
            LoadFieldVisitor loadFieldVisitor = new LoadFieldVisitor(sourceUnit);
            loadFieldVisitor.visitClass(scriptClassNode);
            if (mn.getStatementBlock().isEmpty()) {
                // System.out.println("Adding dummy statement to force script");
                mn.getStatementBlock().addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("___groovy__run__stub___"), Token.newSymbol(Types.EQUAL, 1, 1), new ConstantExpression(null))));
            } else {
                // check whether script body really does anything, if so add ScriptBody marker API
                final AtomicBoolean runnable = new AtomicBoolean(false);
                mn.getStatementBlock().visit(new CodeVisitorSupport() {

                    public void visitExpressionStatement(ExpressionStatement statement) {
                        Expression expression = statement.getExpression();
                        if (expression instanceof DeclarationExpression) {
                            List<AnnotationNode> fa = expression.getAnnotations();
                            for (AnnotationNode n : fa) {
                                if ("Field".equals(n.getClassNode().getName()) || Field.class.equals(n.getClassNode().getTypeClass())) {
                                    // short-circuit and ignore the field declaration
                                    return;
                                }
                            }
                            DeclarationExpression de = (DeclarationExpression) expression;
                            if ((de.getVariableExpression().getModifiers() & ACC_STATIC) != 0) {
                                // static declarations are implied fields
                                return;
                            }
                        }
                        runnable.set(true);
                    }

                    public void visitReturnStatement(ReturnStatement statement) {
                        runnable.set(true);
                    }

                    public void visitAssertStatement(AssertStatement statement) {
                        runnable.set(true);
                    }
                });
                if (runnable.get()) {
                    scriptClassNode.addInterface(new ClassNode(ScriptBody.class));
                }
            }
            LoadFinder loadFinder = new LoadFinder(sourceUnit);
            loadFinder.visitClass(scriptClassNode);
            if (loadFinder.loadMethod != null) {
                scriptClassNode.addInterface(new ClassNode(Loadable.class));
            }
            LineNumberVisitor lineNumberVisitor = null;
            if (sourceLineNumbers != null) {
                lineNumberVisitor = new LineNumberVisitor(sourceUnit);
            }
            boolean isLibrary = false;
            ArgVisitor argVisitor = new ArgVisitor(sourceUnit);
            InitDependencyVisitor initVisitor = new InitDependencyVisitor(sourceUnit);
            TagFinder tagFinder = new TagFinder(sourceUnit);
            TagCallFinder tagCallFinder = new TagCallFinder(sourceUnit, scriptClassNode, factory.getTaggables());
            StaticBindingTransformer staticBindingTransformer = new StaticBindingTransformer(sourceUnit);
            StaticFieldVisitor staticFieldVisitor = new StaticFieldVisitor(sourceUnit);
            staticFieldVisitor.visitClass(scriptClassNode);
            List<MethodNode> methods = mn.getMethods();
            if (methods != null) {
                for (MethodNode method : methods) {
                    boolean isFunction = false;
                    for (AnnotationNode annotation : method.getAnnotations()) {
                        if ("Function".equals(annotation.getClassNode().getName())) {
                            // If this script contains at least one Function declaration, mark it as a library
                            isFunction = true;
                            break;
                        }
                    }
                    if (isFunction) {
                        isLibrary = true;
                        break;
                    }
                }
            }
            List<ClassNode> cnodes = mn.getClasses();
            iterateClassNodes: for (final ClassNode cn : cnodes) {
                // remap GSP line numbers so they match up with original source
                if (lineNumberVisitor != null) {
                    lineNumberVisitor.visitClass(cn);
                }
                tagCallFinder.visitClass(cn);
                // add arg annotations to methods to preserve parameter names
                argVisitor.visitClass(cn);
                if (cn.isInterface()) {
                    continue;
                }
                // Skip further processing for Traits as they don't handle static things well yet ...
                if (cn.getAnnotations() != null) {
                    for (AnnotationNode anno : cn.getAnnotations()) {
                        if (anno.getClassNode().getName().equals("groovy.transform.Trait")) {
                            // System.out.println("SKIPPING TRAIT "+cn.getName());
                            continue iterateClassNodes;
                        }
                    }
                }
                staticBindingTransformer.visitClass(cn);
                // add statistics gathering to methods and closures
                initVisitor.visitClass(cn);
                tagFinder.visitClass(cn);
                final String internalClassName = BytecodeHelper.getClassInternalName(cn);
                // add static missing property support to all classes
                BytecodeExpression staticGetPropertyMissingExpression = new BytecodeExpression() {

                    @Override
                    public void visit(MethodVisitor mv) {
                        mv.visitFieldInsn(GETSTATIC, internalClassName, GROOVITY_SCRIPT_HELPER_FIELD, BytecodeHelper.getTypeDescription(ScriptHelper.class));
                        // BytecodeHelper.visitClassLiteral(mv, cn);
                        mv.visitVarInsn(ALOAD, 0);
                        mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ScriptHelper.class), "staticPropertyMissing", BytecodeHelper.getMethodDescriptor(Object.class, new Class[] { String.class }), false);
                        mv.visitInsn(ARETURN);
                    }
                };
                MethodNode staticGetPropMethod = new MethodNode("$static_propertyMissing", ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, new ClassNode(Object.class), new Parameter[] { new Parameter(ClassHelper.make(String.class), "propertyName") }, new ClassNode[] {}, new BlockStatement(new Statement[] { new ReturnStatement(staticGetPropertyMissingExpression) }, new VariableScope()));
                staticGetPropMethod.setSynthetic(true);
                staticGetPropMethod.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, true);
                cn.addMethod(staticGetPropMethod);
                // add static missing method support to all classes
                BytecodeExpression staticGetMethodMissingExpression = new BytecodeExpression() {

                    @Override
                    public void visit(MethodVisitor mv) {
                        mv.visitFieldInsn(GETSTATIC, internalClassName, GROOVITY_SCRIPT_HELPER_FIELD, BytecodeHelper.getTypeDescription(ScriptHelper.class));
                        // BytecodeHelper.visitClassLiteral(mv, cn);
                        mv.visitVarInsn(ALOAD, 0);
                        mv.visitVarInsn(ALOAD, 1);
                        mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ScriptHelper.class), "invokeMethod", BytecodeHelper.getMethodDescriptor(Object.class, new Class[] { String.class, Object.class }), false);
                        mv.visitInsn(ARETURN);
                    }
                };
                MethodNode staticGetMissingMethod = new MethodNode("$static_methodMissing", ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, new ClassNode(Object.class), new Parameter[] { new Parameter(ClassHelper.make(String.class), "methodName"), new Parameter(ClassHelper.make(Object.class), "methodArgs") }, new ClassNode[] {}, new BlockStatement(new Statement[] { new ReturnStatement(staticGetMethodMissingExpression) }, new VariableScope()));
                staticGetMissingMethod.setSynthetic(true);
                staticGetMissingMethod.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, true);
                cn.addMethod(staticGetMissingMethod);
                if (!(cn instanceof InnerClassNode) || cn.isStaticClass()) {
                    FieldNode gsfNode = new FieldNode(GROOVITY_SCRIPT_HELPER_FIELD, ACC_PROTECTED | ACC_STATIC, new ClassNode(ScriptHelper.class), cn, ConstantExpression.NULL);
                    cn.addField(gsfNode);
                    // add missing method support to all classes
                    BytecodeExpression getMethodMissingExpression = new BytecodeExpression() {

                        @Override
                        public void visit(MethodVisitor mv) {
                            mv.visitFieldInsn(GETSTATIC, internalClassName, GROOVITY_SCRIPT_HELPER_FIELD, BytecodeHelper.getTypeDescription(ScriptHelper.class));
                            // BytecodeHelper.visitClassLiteral(mv, cn);
                            mv.visitVarInsn(ALOAD, 1);
                            mv.visitVarInsn(ALOAD, 2);
                            mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ScriptHelper.class), "invokeMethod", BytecodeHelper.getMethodDescriptor(Object.class, new Class[] { String.class, Object.class }), false);
                            mv.visitInsn(ARETURN);
                        }
                    };
                    MethodNode getMissingMethod = new MethodNode("methodMissing", ACC_PUBLIC | ACC_SYNTHETIC, new ClassNode(Object.class), new Parameter[] { new Parameter(ClassHelper.make(String.class), "methodName"), new Parameter(ClassHelper.make(Object.class), "methodArgs") }, new ClassNode[] { new ClassNode(Exception.class) }, new BlockStatement(new Statement[] { new ReturnStatement(getMethodMissingExpression) }, new VariableScope()));
                    getMissingMethod.setSynthetic(true);
                    getMissingMethod.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, true);
                    cn.addMethod(getMissingMethod);
                    // add missing property lookup to top-level classes
                    BytecodeExpression instanceGetPropertyMissingExpression = new BytecodeExpression() {

                        @Override
                        public void visit(MethodVisitor mv) {
                            mv.visitFieldInsn(GETSTATIC, internalClassName, GROOVITY_SCRIPT_HELPER_FIELD, BytecodeHelper.getTypeDescription(ScriptHelper.class));
                            mv.visitVarInsn(ALOAD, 1);
                            mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ScriptHelper.class), "getProperty", BytecodeHelper.getMethodDescriptor(Object.class, new Class[] { String.class }), false);
                            mv.visitInsn(ARETURN);
                        }
                    };
                    MethodNode instanceGetMethod = new MethodNode("propertyMissing", ACC_PUBLIC | ACC_SYNTHETIC, new ClassNode(Object.class), new Parameter[] { new Parameter(ClassHelper.make(String.class), "propertyName") }, new ClassNode[] {}, new BlockStatement(new Statement[] { new ReturnStatement(instanceGetPropertyMissingExpression) }, new VariableScope()));
                    instanceGetMethod.setSynthetic(true);
                    instanceGetMethod.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, true);
                    cn.addMethod(instanceGetMethod);
                    BytecodeExpression instanceSetPropertyMissingExpression = new BytecodeExpression() {

                        @Override
                        public void visit(MethodVisitor mv) {
                            mv.visitFieldInsn(GETSTATIC, internalClassName, GROOVITY_SCRIPT_HELPER_FIELD, BytecodeHelper.getTypeDescription(ScriptHelper.class));
                            mv.visitVarInsn(ALOAD, 1);
                            mv.visitVarInsn(ALOAD, 2);
                            mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ScriptHelper.class), "setProperty", Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(String.class), Type.getType(Object.class)), false);
                            mv.visitInsn(RETURN);
                        }
                    };
                    MethodNode setMethod = new MethodNode("propertyMissing", ACC_PUBLIC | ACC_SYNTHETIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(ClassHelper.make(String.class), "propertyName"), new Parameter(new ClassNode(Object.class), "newValue") }, new ClassNode[] {}, new BlockStatement(new Statement[] { new ExpressionStatement(instanceSetPropertyMissingExpression) }, new VariableScope()));
                    setMethod.setSynthetic(true);
                    setMethod.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, true);
                    cn.addMethod(setMethod);
                    if (cn != scriptClassNode) {
                        // add getBinding to other classes
                        BytecodeExpression getFactoryCall = new BytecodeExpression() {

                            @Override
                            public void visit(MethodVisitor mv) {
                                mv.visitFieldInsn(GETSTATIC, internalClassName, GROOVITY_SCRIPT_HELPER_FIELD, BytecodeHelper.getTypeDescription(ScriptHelper.class));
                                mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ScriptHelper.class), "getBinding", BytecodeHelper.getMethodDescriptor(Binding.class, new Class[] {}), false);
                                mv.visitInsn(ARETURN);
                            }
                        };
                        MethodNode getMethod = new MethodNode("getBinding", ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, new ClassNode(Binding.class), new Parameter[] {}, new ClassNode[] {}, new BlockStatement(new Statement[] { new ReturnStatement(getFactoryCall) }, new VariableScope()));
                        getMethod.setSynthetic(true);
                        getMethod.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, true);
                        cn.addMethod(getMethod);
                    }
                    // add load, run and tag methods to all top level classes
                    BytecodeExpression loadFactoryCall = new BytecodeExpression() {

                        @Override
                        public void visit(MethodVisitor mv) {
                            mv.visitFieldInsn(GETSTATIC, internalClassName, GROOVITY_SCRIPT_HELPER_FIELD, BytecodeHelper.getTypeDescription(ScriptHelper.class));
                            mv.visitVarInsn(ALOAD, 0);
                            mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ScriptHelper.class), LOAD, BytecodeHelper.getMethodDescriptor(Script.class, new Class[] { String.class }), false);
                            mv.visitInsn(ARETURN);
                        }
                    };
                    MethodNode loadMethod = new MethodNode(LOAD, ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, new ClassNode(Script.class), new Parameter[] { new Parameter(new ClassNode(String.class), "path") }, new ClassNode[] { new ClassNode(InstantiationException.class), new ClassNode(IllegalAccessException.class), new ClassNode(ClassNotFoundException.class) }, new BlockStatement(new Statement[] { new ReturnStatement(loadFactoryCall) }, new VariableScope()));
                    loadMethod.setSynthetic(true);
                    loadMethod.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, true);
                    cn.addMethod(loadMethod);
                    BytecodeExpression runFactoryCall = new BytecodeExpression() {

                        @Override
                        public void visit(MethodVisitor mv) {
                            mv.visitFieldInsn(GETSTATIC, internalClassName, GROOVITY_SCRIPT_HELPER_FIELD, BytecodeHelper.getTypeDescription(ScriptHelper.class));
                            mv.visitVarInsn(ALOAD, 0);
                            mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ScriptHelper.class), RUN, BytecodeHelper.getMethodDescriptor(Object.class, new Class[] { String.class }), false);
                            mv.visitInsn(ARETURN);
                        }
                    };
                    MethodNode runMethod = new MethodNode(RUN, ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.OBJECT_TYPE, new Parameter[] { new Parameter(new ClassNode(String.class), "path") }, new ClassNode[] { new ClassNode(InstantiationException.class), new ClassNode(IllegalAccessException.class), new ClassNode(ClassNotFoundException.class), new ClassNode(IOException.class) }, new BlockStatement(new Statement[] { new ReturnStatement(runFactoryCall) }, new VariableScope()));
                    runMethod.setSynthetic(true);
                    runMethod.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, true);
                    cn.addMethod(runMethod);
                    BytecodeExpression doStreamCall = new BytecodeExpression() {

                        @Override
                        public void visit(MethodVisitor mv) {
                            mv.visitFieldInsn(GETSTATIC, internalClassName, GROOVITY_SCRIPT_HELPER_FIELD, BytecodeHelper.getTypeDescription(ScriptHelper.class));
                            mv.visitVarInsn(ALOAD, 0);
                            mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ScriptHelper.class), STREAM, Type.getMethodDescriptor(Type.VOID_TYPE, Type.getType(Object.class)), false);
                            mv.visitInsn(RETURN);
                        }
                    };
                    MethodNode doStreamMethod = new MethodNode(STREAM, ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.VOID_TYPE, new Parameter[] { new Parameter(ClassHelper.make(Object.class), "obj") }, new ClassNode[] {}, new BlockStatement(new Statement[] { new ExpressionStatement(doStreamCall) }, new VariableScope()));
                    doStreamMethod.setSynthetic(true);
                    doStreamMethod.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, true);
                    cn.addMethod(doStreamMethod);
                    BytecodeExpression doTagFullFactoryCall = new BytecodeExpression() {

                        @Override
                        public void visit(MethodVisitor mv) {
                            mv.visitFieldInsn(GETSTATIC, internalClassName, GROOVITY_SCRIPT_HELPER_FIELD, BytecodeHelper.getTypeDescription(ScriptHelper.class));
                            mv.visitVarInsn(ALOAD, 0);
                            mv.visitVarInsn(ALOAD, 1);
                            mv.visitVarInsn(ALOAD, 2);
                            mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ScriptHelper.class), TAG, BytecodeHelper.getMethodDescriptor(Object.class, new Class[] { String.class, Map.class, Closure.class }), false);
                            mv.visitInsn(ARETURN);
                        }
                    };
                    MethodNode doTagFullMethod = new MethodNode(TAG, ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.OBJECT_TYPE, new Parameter[] { new Parameter(ClassHelper.make(String.class), "tagName"), new Parameter(new ClassNode(Map.class), "attributes"), new Parameter(new ClassNode(Closure.class), "body") }, new ClassNode[] { new ClassNode(Exception.class) }, new BlockStatement(new Statement[] { new ReturnStatement(doTagFullFactoryCall) }, new VariableScope()));
                    doTagFullMethod.setSynthetic(true);
                    doTagFullMethod.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, true);
                    cn.addMethod(doTagFullMethod);
                    BytecodeExpression doTagShortBodyFactoryCall = new BytecodeExpression() {

                        @Override
                        public void visit(MethodVisitor mv) {
                            mv.visitFieldInsn(GETSTATIC, internalClassName, GROOVITY_SCRIPT_HELPER_FIELD, BytecodeHelper.getTypeDescription(ScriptHelper.class));
                            mv.visitVarInsn(ALOAD, 0);
                            mv.visitVarInsn(ALOAD, 1);
                            mv.visitMethodInsn(INVOKEVIRTUAL, BytecodeHelper.getClassInternalName(ScriptHelper.class), TAG, BytecodeHelper.getMethodDescriptor(Object.class, new Class[] { String.class, Closure.class }), false);
                            mv.visitInsn(ARETURN);
                        }
                    };
                    MethodNode doTagShortBodyMethod = new MethodNode(TAG, ACC_PUBLIC | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.OBJECT_TYPE, new Parameter[] { new Parameter(ClassHelper.make(String.class), "tagName"), new Parameter(new ClassNode(Closure.class), "body") }, new ClassNode[] { new ClassNode(Exception.class) }, new BlockStatement(new Statement[] { new ReturnStatement(doTagShortBodyFactoryCall) }, new VariableScope()));
                    doTagShortBodyMethod.setSynthetic(true);
                    doTagShortBodyMethod.putNodeMetaData(StaticCompilationMetadataKeys.STATIC_COMPILE_NODE, true);
                    cn.addMethod(doTagShortBodyMethod);
                }
            }
            // Record whether the class is a library with @Functions in a static final boolean.
            final FieldNode isLibraryFieldNode = new FieldNode("isGroovyLibrary", ACC_PUBLIC | ACC_STATIC | ACC_FINAL, new ClassNode(Boolean.class), scriptClassNode, new ConstantExpression(isLibrary));
            scriptClassNode.addField(isLibraryFieldNode);
            ListExpression dependencyExpression = new ListExpression();
            for (String dep : initDependencies) {
                dependencyExpression.addExpression(new ConstantExpression(dep));
            }
            // Store pointers to all dependencies to help control load order
            final FieldNode initDependenciesFieldNode = new FieldNode("initDependencies", ACC_PUBLIC | ACC_STATIC | ACC_FINAL, new ClassNode(new ArrayList<String>().getClass()), scriptClassNode, dependencyExpression);
            scriptClassNode.addField(initDependenciesFieldNode);
        }
    } catch (Exception e) {
        log.error("Error generating stats AST: ", e);
    }
}
Also used : Loadable(com.disney.groovity.Loadable) Closure(groovy.lang.Closure) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) BlockStatement(org.codehaus.groovy.ast.stmt.BlockStatement) MethodVisitor(groovyjarjarasm.asm.MethodVisitor) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ArrayList(java.util.ArrayList) List(java.util.List) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ModuleNode(org.codehaus.groovy.ast.ModuleNode) ClassNode(org.codehaus.groovy.ast.ClassNode) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) FieldNode(org.codehaus.groovy.ast.FieldNode) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) InnerClassNode(org.codehaus.groovy.ast.InnerClassNode) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CodeVisitorSupport(org.codehaus.groovy.ast.CodeVisitorSupport) ClassCodeVisitorSupport(org.codehaus.groovy.ast.ClassCodeVisitorSupport) UnaryMinusExpression(org.codehaus.groovy.ast.expr.UnaryMinusExpression) ClosureListExpression(org.codehaus.groovy.ast.expr.ClosureListExpression) VariableExpression(org.codehaus.groovy.ast.expr.VariableExpression) ArgumentListExpression(org.codehaus.groovy.ast.expr.ArgumentListExpression) TernaryExpression(org.codehaus.groovy.ast.expr.TernaryExpression) CastExpression(org.codehaus.groovy.ast.expr.CastExpression) BooleanExpression(org.codehaus.groovy.ast.expr.BooleanExpression) PropertyExpression(org.codehaus.groovy.ast.expr.PropertyExpression) StaticMethodCallExpression(org.codehaus.groovy.ast.expr.StaticMethodCallExpression) PrefixExpression(org.codehaus.groovy.ast.expr.PrefixExpression) PostfixExpression(org.codehaus.groovy.ast.expr.PostfixExpression) Expression(org.codehaus.groovy.ast.expr.Expression) UnaryPlusExpression(org.codehaus.groovy.ast.expr.UnaryPlusExpression) BitwiseNegationExpression(org.codehaus.groovy.ast.expr.BitwiseNegationExpression) GStringExpression(org.codehaus.groovy.ast.expr.GStringExpression) ListExpression(org.codehaus.groovy.ast.expr.ListExpression) MapExpression(org.codehaus.groovy.ast.expr.MapExpression) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) RangeExpression(org.codehaus.groovy.ast.expr.RangeExpression) BytecodeExpression(org.codehaus.groovy.classgen.BytecodeExpression) SpreadExpression(org.codehaus.groovy.ast.expr.SpreadExpression) ArrayExpression(org.codehaus.groovy.ast.expr.ArrayExpression) NotExpression(org.codehaus.groovy.ast.expr.NotExpression) BinaryExpression(org.codehaus.groovy.ast.expr.BinaryExpression) FieldExpression(org.codehaus.groovy.ast.expr.FieldExpression) ElvisOperatorExpression(org.codehaus.groovy.ast.expr.ElvisOperatorExpression) MethodCallExpression(org.codehaus.groovy.ast.expr.MethodCallExpression) SpreadMapExpression(org.codehaus.groovy.ast.expr.SpreadMapExpression) ConstructorCallExpression(org.codehaus.groovy.ast.expr.ConstructorCallExpression) DeclarationExpression(org.codehaus.groovy.ast.expr.DeclarationExpression) ClosureExpression(org.codehaus.groovy.ast.expr.ClosureExpression) ClassExpression(org.codehaus.groovy.ast.expr.ClassExpression) TupleExpression(org.codehaus.groovy.ast.expr.TupleExpression) AttributeExpression(org.codehaus.groovy.ast.expr.AttributeExpression) MethodPointerExpression(org.codehaus.groovy.ast.expr.MethodPointerExpression) MapEntryExpression(org.codehaus.groovy.ast.expr.MapEntryExpression) ScriptBody(com.disney.groovity.ScriptBody) ExpressionStatement(org.codehaus.groovy.ast.stmt.ExpressionStatement) AssertStatement(org.codehaus.groovy.ast.stmt.AssertStatement) Parameter(org.codehaus.groovy.ast.Parameter) Map(java.util.Map) VariableScope(org.codehaus.groovy.ast.VariableScope)

Aggregations

ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)74 BlockStatement (org.codehaus.groovy.ast.stmt.BlockStatement)44 Statement (org.codehaus.groovy.ast.stmt.Statement)38 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)37 ExpressionStatement (org.codehaus.groovy.ast.stmt.ExpressionStatement)35 VariableExpression (org.codehaus.groovy.ast.expr.VariableExpression)32 ArgumentListExpression (org.codehaus.groovy.ast.expr.ArgumentListExpression)30 MethodNode (org.codehaus.groovy.ast.MethodNode)28 MethodCallExpression (org.codehaus.groovy.ast.expr.MethodCallExpression)26 Expression (org.codehaus.groovy.ast.expr.Expression)25 ConstructorCallExpression (org.codehaus.groovy.ast.expr.ConstructorCallExpression)24 IfStatement (org.codehaus.groovy.ast.stmt.IfStatement)23 ClassNode (org.codehaus.groovy.ast.ClassNode)22 Parameter (org.codehaus.groovy.ast.Parameter)22 StaticMethodCallExpression (org.codehaus.groovy.ast.expr.StaticMethodCallExpression)22 BinaryExpression (org.codehaus.groovy.ast.expr.BinaryExpression)21 ClassExpression (org.codehaus.groovy.ast.expr.ClassExpression)18 ArrayList (java.util.ArrayList)17 EmptyStatement (org.codehaus.groovy.ast.stmt.EmptyStatement)17 BooleanExpression (org.codehaus.groovy.ast.expr.BooleanExpression)15