2

我正在尝试运行为我的嵌入式 groovy 表达式设置基类的基本示例。测试取自http://groovy.codehaus.org/Embedding+Groovy

class ScriptBaseTest {

  @Test
  void extend_groovy_script() {
    def compiler = new CompilerConfiguration()
    compiler.setScriptBaseClass("ScriptBaseTestScript")

    def shell = new GroovyShell(this.class.classLoader, new Binding(), compiler)

    assertEquals shell.evaluate("foo()"), "this is foo"
  }
}

abstract class ScriptBaseTestScript extends Script {
  def foo() {
    "this is foo"
  }
}

如果我将它作为 JUnit 测试运行(在 Eclipse 中),我会得到以下堆栈跟踪:

groovy.lang.MissingMethodException: No signature of method: Script1.foo() is applicable for argument types: () values: []
Possible solutions: run(), run(), find(), any(), use([Ljava.lang.Object;), find(groovy.lang.Closure)
    at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:55)
    at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:78)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:49)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:133)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:137)
    at Script1.run(Script1.groovy:1)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:518)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:556)
    at groovy.lang.GroovyShell.evaluate(GroovyShell.java:527)
    at groovy.lang.GroovyShell$evaluate.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:45)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:108)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:116)
    at ScriptBaseTest.extend_groovy_script(ScriptBaseTest.groovy:13)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

所以这看起来有些不对劲;)有什么技巧可以让它发挥作用吗?

我使用 Groovy Eclipse 插件 2.7.2.xx-RELEASE-20121219-0800-e42 在 Eclipse Juno、Java 1.7 中运行它。Groovy 编译器设置为 2.0.6。

更新:当我在命令行上运行它时工作正常。

4

2 回答 2

4

当我尝试使用 GroovyScriptEngine 时,我遇到了同样的问题。通过初始化

    CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
    compilerConfiguration.setScriptBaseClass(DSLScriptBase.class.getName());

    GroovyScriptEngine gse = new GroovyScriptEngine(roots, compilerConfiguration);

没用。未使用自定义脚本基类。

我更改了代码以预先使用编译器配置创建一个GroovyClassLoader,并将此类加载器用作父类加载器。这似乎有效。

    CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
    compilerConfiguration.setScriptBaseClass(DSLScriptBase.class.getName());
    GroovyClassLoader scriptClassLoader = new GroovyClassLoader(DSLEngine.class.getClassLoader(), compilerConfiguration);

    GroovyScriptEngine gse = new GroovyScriptEngine(roots, scriptClassLoader);

有人(也许是你?)将它发布到http://groovy-eclipse-plugin.42567.n3.nabble.com/setScriptBaseClass-not-working-inside-eclipse-td4025243.html的 eclipse groovy 插件邮件列表

于 2013-06-11T15:01:15.793 回答
1

setScriptBaseClass()需要完全限定的名称(即java.lang.String,而不是 just String)。

为了让您的生活更轻松,请使用以下代码:

compiler.setScriptBaseClass(ScriptBaseTestScript.class.getName());

此外,基类不能是abstract.

于 2013-01-30T13:29:02.540 回答