groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Marc Paquette <>
Subject Reloading of script with GroovyClassLoader
Date Sat, 14 Nov 2015 15:31:45 GMT
Hello all,

I am using `GroovyClassLoader` to execute groovy scripts at runtime and I want modifications
to the script text (stored on the filesystem) to be picked up on subsequent execution of the
script but I don't want the script to be re-compiled each time.  From my understanding, creating
a `GroovyClassLoader` with a `CompilerConfiguration` where `recompileGroovySource` is set
to true should give this outcome, but it does not seems to work the way I am using it.

I expect the following script to pass, but the second assert fails, anybody can tell me what
I am doing wrong (or why my assumptions on how this is supposed to work are false) ?

import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.runtime.InvokerHelper
class MyGcl {

    public static void main(String[] args) {
        CompilerConfiguration config
        config = new CompilerConfiguration()
        GroovyClassLoader gcl = new GroovyClassLoader(getClassLoader(), config)
        Binding binding = new Binding()

        File fooFile = new File("foo.groovy")
        fooFile.text = '''
        def result = InvokerHelper.createScript(gcl.parseClass(fooFile), binding).run()
        assert 'abc' == result

        fooFile.text = fooFile.text + '''
        result = InvokerHelper.createScript(gcl.parseClass(fooFile), binding).run()
        assert '123' == result

Thanks in advance,

View raw message