zedoo
zedoo

Reputation: 11287

Embedding groovy: CompilerConfiguration.setScriptBaseClass doesn't work

I'm trying to run the basic example for setting a base class for my embedded groovy expressions. The test is taken from 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"
  }
}

If I run this as a JUnit test (within Eclipse), I get the following stacktrace:

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)

So this looks like something's wrong ;) Any tips to make it work?

I'm running this in Eclipse Juno, Java 1.7, with the Groovy Eclipse Plugin 2.7.2.xx-RELEASE-20121219-0800-e42. Groovy compiler is set to 2.0.6.

UPDATE: Works fine when I run it on the command line.

Upvotes: 2

Views: 2056

Answers (2)

cemonds
cemonds

Reputation: 225

I had the same problem, when I tried to use the GroovyScriptEngine. Initalizing via

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

    GroovyScriptEngine gse = new GroovyScriptEngine(roots, compilerConfiguration);

didn't work. The custom script base class wasn't used.

I changed the code to create a GroovyClassLoader with the compiler configuration upfront and using this class loader as parent class loader. This seems to work.

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

    GroovyScriptEngine gse = new GroovyScriptEngine(roots, scriptClassLoader);

Someone (maybe you?) posted it to the eclipse groovy plugin mailing list at http://groovy-eclipse-plugin.42567.n3.nabble.com/setScriptBaseClass-not-working-inside-eclipse-td4025243.html

Upvotes: 4

Aaron Digulla
Aaron Digulla

Reputation: 328624

setScriptBaseClass() expects the fully qualified name (i.e. java.lang.String instead of just String).

To make your life easier, use this code:

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

Also, the base class must not be abstract.

Upvotes: 1

Related Questions