gpt4 book ai didi

eclipse - 嵌入 groovy : CompilerConfiguration. setScriptBaseClass 不起作用

转载 作者:行者123 更新时间:2023-12-02 05:16:11 24 4
gpt4 key购买 nike

我正在尝试运行为我的嵌入式 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。

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

最佳答案

当我尝试使用 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 插件邮件列表中

关于eclipse - 嵌入 groovy : CompilerConfiguration. setScriptBaseClass 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14602683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com