tags:

views:

131

answers:

1

In Groovy Console I have this:

import groovy.util.*
import org.codehaus.groovy.runtime.*

def gse = new GroovyScriptEngine("c:\\temp")

def script = gse.loadScriptByName("say.groovy")

this.metaClass.mixin script

say("bye")

say.groovy contains

def say(String msg) {
  println(msg)
}

Edit: I filed a bug report: https://svn.dentaku.codehaus.org/browse/GROOVY-4214

+3  A: 

It's when it hits the line:

this.metaClass.mixin script

The loaded script probably has a reference to the class that loaded it (this class), so when you try and mix it in, you get an endless loop.

A workaround is to do:

def gse = new groovy.util.GroovyScriptEngine( '/tmp' )
def script = gse.loadScriptByName( 'say.groovy' )
script.newInstance().with {
  say("bye")
}

[edit]

It seems to work if you use your original script, but change say.groovy to

class Say {
  def say( msg ) {
    println msg
  }
}
tim_yates
my aim was at adding methods to the main script, not creating an instance of the loaded script
IttayD
Added a different method of doing this without newinstance
tim_yates