Quantcast

Obtaining source code for a closure in a Grails app

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Obtaining source code for a closure in a Grails app

asinesio
Hey all,

I'm working on a project that uses closures to do some extensive configuration, and I'd love to be able to show the actual source code of each closure in the web application.

I've got code that works from an integration test, based on this StackOverflow post:


The code looks like this (code is a closure):

    String getSourceCode() {
        def node = code?.metaClass?.classNode?.getDeclaredMethods("doCall")
        if (node)
            node = node[0]?.code
        def writer = new StringWriter()
        if (node) {
            node.visit new AstNodeToScriptVisitor(writer)
        } else {
            writer.write("Source code not available.")
        }
        writer.toString()
    }

When I execute this method in an integration test, it works great.  When I execute it via a unit test or in a GSP via run-app, code.metaClass.classNode is null, presumably because the source is not on the classpath.

All of the closures are defined in the same Config file in grails-app/conf.

So:

1) Does anyone know how I can compile a Grails app and keep sources on the classpath during run-app/deployment?
2) Does anyone have a suggestion for getting the source of something defined in a Config.groovy file?

Any help or suggestions people could provide would be greatly appreciated.

Thanks,
Andy


Loading...