Groovy web console

subscribe to the feed Subscribe
to this
site
compile configuration (via #groovywebconsole)
tweet this snippet Tweet
this
script

compile configuration

Published 5 months ago by so43058346 with tags script compile
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import org.codehaus.groovy.control.CompilerConfiguration

abstract class MyScript extends Script {
    String name
    String greet() {
       "Hello, $name"
    }
}

def config = new CompilerConfiguration()
config.scriptBaseClass = 'MyScript'
shell = new GroovyShell(this.class.classLoader, new Binding(), config)
script = shell.parse('greet()')
assert script instanceof MyScript
script.setName('Foo')
println script.greet()
assert script.run() == 'Hello, Foo'