[groovy-user] Please help me to figure out why a builder's methods are not called

181 views
Skip to first unread message

Serge

unread,
Sep 5, 2010, 2:20:14 PM9/5/10
to us...@groovy.codehaus.org
Hello All,

I need to read a file containg a DSL from filesystem.  I have written a test1 for that, but it fails. Surprisingly test2 works. Any idea how to make test1 work ? Maven project with sources is attached.

Test1 fails:  
          groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.compile() is applicable for argument types: (Script1$_run_closure1_closure2) values: ...

Maven project with sources is attached,
---------------- TestDsl.groovy--------------------------------
mport org.testng.annotations.Test
import javax.script.ScriptEngine
import javax.script.ScriptEngineManager
import org.testng.annotations.BeforeMethod
import javax.script.ScriptContext

class TestDsl {

  MyBuilder builder

  @BeforeMethod
  void init() {
    builder = new MyBuilder()
  }

  @Test
  void test1() { // This test does the same as test2, but fails
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine engine = factory.getEngineByName("Groovy");
    InputStreamReader reader = new InputStreamReader(getClass().getResourceAsStream('script.groovy'))
    try {
      def bindings = engine.createBindings()
      bindings.put('builder', builder)
      engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE)
      engine.eval(reader)
    } finally {
      reader.close()
    }
  }

  @Test
  void test2() {   // This test is ok
    builder.build {
      compile {
        main {
          println 'hello main'
        }
        test {
          println 'hello test'
        }
      }
    }
  }
}

------------------ script.groovy ------------
builder.build {
   compile {
     main {
        println 'hello main'
     }
     test {
        println 'hello test'
     }
   }
}
-------------  MyBuilder.groovy --------------
class MyBuilder extends BuilderSupport {
  protected void setParent(Object parent, Object child) {
  }

  protected Object createNode(Object name) {

  }

  protected Object createNode(Object name, Object value) {

  }

  protected Object createNode(Object name, Map attributes) {

  }

  protected Object createNode(Object name, Map attributes, Object value) {

  }
}
script.zip

Roshan Dawrani

unread,
Sep 5, 2010, 6:34:26 PM9/5/10
to us...@groovy.codehaus.org
Which groovy version are you on?

If I try it on 1.7.5, it almost works.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email


Serge

unread,
Sep 6, 2010, 6:10:30 AM9/6/10
to us...@groovy.codehaus.org
I have upgraded Groovy dependency to 1.7.4, it still fails.  I am using JDK 1.6.20.
Updated code  attached.
script.zip

Roshan Dawrani

unread,
Sep 6, 2010, 6:12:14 AM9/6/10
to us...@groovy.codehaus.org
Did you notice any change in the error message? Can you provide the error that you get with 1.7.4?

What was the groovy version that you were earlier using?

Serge

unread,
Sep 6, 2010, 6:23:06 AM9/6/10
to us...@groovy.codehaus.org
Yes, I there is a difference in error messages

With Groovy 1.6.5 I had  : -------------------------------------------------------------------------------------------------------------------------------
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.compile() is applicable for argument types: (Script1$_run_closure1_closure2) values: [Script1$_run_closure1_closure2@16daa9]
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:118)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:104)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:229)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at TestDsl.test1(TestDsl.groovy:25)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:62)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:141)
at org.apache.maven.surefire.Surefire.run(Surefire.java:180)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
Caused by: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.compile() is applicable for argument types: (Script1$_run_closure1_closure2) values: [Script1$_run_closure1_closure2@16daa9]
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:318)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:112)
... 41 more
With Groovy 1.7.4 I have         -------------------------------------------------------------------------------------------------------------------------
javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: static Script1.main() is applicable for argument types: (Script1$_run_closure1_closure2_closure3) values: [Script1$_run_closure1_closure2_closure3@17b40fe]
Possible solutions: main([Ljava.lang.String;), wait(), wait(long), run(), run(), any()
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:117)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:103)
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:232)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoCachedMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:229)
at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at TestDsl.test1(TestDsl.groovy:27)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:62)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:141)
at org.apache.maven.surefire.Surefire.run(Surefire.java:180)
at org.apache.maven.surefire.booter.SurefireBooter.runSuitesInProcess(SurefireBooter.java:350)
at org.apache.maven.surefire.booter.SurefireBooter.main(SurefireBooter.java:1021)
Caused by: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: static Script1.main() is applicable for argument types: (Script1$_run_closure1_closure2_closure3) values: [Script1$_run_closure1_closure2_closure3@17b40fe]
Possible solutions: main([Ljava.lang.String;), wait(), wait(long), run(), run(), any()
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:317)
at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:111)
script.zip

Roshan Dawrani

unread,
Sep 6, 2010, 6:31:10 AM9/6/10
to us...@groovy.codehaus.org
Great - that is why I had initially written that I am almost seeing it working.

When you have the DSL as:
------------------------------script1.groovy-------------------------
builder.build {
   compile {
     main {
        ...
     }
...
   }
}
-------------------------------------------------------

the error you are getting (No signature of method: static Script1.main()) is correct behavior because every groovy script implicitly gets a main() as the entry point of that script and the in the compiled DSL, main {..} is getting mapped to that static main(String[]), which fails at runtime when the DSL closure gets passed to it.

So, change "main { }"...to use something other than "main" to avoid the conflict and it should be fine!

Cheers,
Roshan

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email

Serge

unread,
Sep 6, 2010, 6:36:59 AM9/6/10
to us...@groovy.codehaus.org
Thank you very much, Roshan, it works now.
Reply all
Reply to author
Forward
0 new messages