开发者

Why am I getting StackOverflowError?

In Groovy Console I have this:

i开发者_StackOverflowmport 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


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
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜