Reputation: 937
TL;DR: How can I access a class defined in a groovy script that is parsed dynamically?
Let's say I have a groovy script like this:
def foo(){ print "foo" }
def bar(){ print "bar" }
class Baz {
def waz(){
print "Baz#waz"
}
}
... and I can parse it out to a Script object like this:
Script myScript = new GroovyShell().parse(new File("my_script.groovy"))
Then I know I can access the functions:
myShell.foo()
myShell.bar()
But how can I access the Baz
class declaration?
Thanks
Upvotes: 0
Views: 311
Reputation: 28564
by default groovyshell uses own classloader to load script and nested classes.
so, you could access it through classloader
def script = '''
def foo(){ print "foo" }
def bar(){ print "bar" }
class Baz {
def waz(){
print "Baz#waz"
}
}
'''
def gshell = new GroovyShell()
Script myScript = gshell.parse(script)
myScript.foo()
println gshell.getClassLoader().loadClass('Baz')
Upvotes: 1