[groovy-user] Special handling of main, won't go to delegate?

5 views
Skip to first unread message

Marc Palmer

unread,
May 20, 2011, 4:13:11 PM5/20/11
to Groovy User
Hi,

I'm having a trouble with a simple DSL. It seems that I cannot get synthetic method calls in my DSL to work with a name "main", even if I set DELEGATE_ONLY. My builder implements invokeMethod, perhaps the problem is there? The delegate never seems to see the call.

This is unfortunate, as the use case for this DSL (Grails resources DSL) leads people to often try "main" and they get an ugly error:

See the Groovy Console code/transcript below.

groovy> def dsl = {
groovy> whatever {
groovy> println "hello"
groovy> }
groovy> main {
groovy> println "oh"
groovy> }
groovy> }
groovy> class MyBuilder {
groovy> def invokeMethod(String name, args) {
groovy> System.out.println "Invoked $name"
groovy> }
groovy> }
groovy> dsl.delegate = new MyBuilder()
groovy> dsl.resolveStrategy = Closure.DELEGATE_ONLY
groovy> dsl()

Invoked whatever
Exception thrown
May 20, 2011 9:09:20 PM org.codehaus.groovy.runtime.StackTraceUtils sanitize
WARNING: Sanitizing stacktrace:
groovy.lang.MissingMethodException: No signature of method: static ConsoleScript2.main() is applicable for argument types: (ConsoleScript2$_run_closure1_closure3) values: [ConsoleScript2$_run_closure1_closure3@6521f956]
Possible solutions: main([Ljava.lang.String;), wait(), wait(long), run(), run(), any()
at groovy.lang.MetaClassImpl.invokeStaticMissingMethod(MetaClassImpl.java:1360)
at groovy.lang.MetaClassImpl.invokeStaticMethod(MetaClassImpl.java:1346)
at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.callStatic(StaticMetaClassSite.java:62)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallStatic(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:167)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callStatic(AbstractCallSite.java:175)
at ConsoleScript2$_run_closure1.doCall(ConsoleScript2:6)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCallCurrent(CallSiteArray.java:44)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:143)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:151)
at ConsoleScript2$_run_closure1.doCall(ConsoleScript2)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.call(PogoMetaClassSite.java:39)
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:121)
at ConsoleScript2.run(ConsoleScript2:19)
at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:264)
at groovy.lang.GroovyShell.run(GroovyShell.java:513)
at groovy.lang.GroovyShell.run(GroovyShell.java:170)
at groovy.lang.GroovyShell$run.call(Unknown Source)
at groovy.ui.Console$_runScriptImpl_closure16.doCall(Console.groovy:865)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at org.codehaus.groovy.runtime.callsite.PogoMetaClassSite.callCurrent(PogoMetaClassSite.java:66)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:151)
at groovy.ui.Console$_runScriptImpl_closure16.doCall(Console.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:273)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
at groovy.lang.Closure.call(Closure.java:276)
at groovy.lang.Closure.call(Closure.java:271)
at groovy.lang.Closure.run(Closure.java:354)
at java.lang.Thread.run(Thread.java:680)

groovy.lang.MissingMethodException: No signature of method: static ConsoleScript2.main() is applicable for argument types: (ConsoleScript2$_run_closure1_closure3) values: [ConsoleScript2$_run_closure1_closure3@6521f956]
Possible solutions: main([Ljava.lang.String;), wait(), wait(long), run(), run(), any()
at ConsoleScript2$_run_closure1.doCall(ConsoleScript2:6)
at ConsoleScript2$_run_closure1.doCall(ConsoleScript2)
at ConsoleScript2.run(ConsoleScript2:19)


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

http://xircles.codehaus.org/manage_email


virtualeyes

unread,
May 20, 2011, 5:11:14 PM5/20/11
to us...@groovy.codehaus.org
Do you have to define the dsl closure in script scope?

If the closure is defined in class scope, the error goes away:
class Foo {
def dsl = {
whatever {
println "hello"
}
main {
println "oh"
}
}
}
class MyBuilder {
def invokeMethod(String name, args) {
System.out.println "Invoked $name"
}
}
def foo = new Foo()
foo.dsl.delegate = new MyBuilder()
foo.dsl.resolveStrategy = Closure.DELEGATE_ONLY
foo.dsl()

--
View this message in context: http://groovy.329449.n5.nabble.com/Special-handling-of-main-won-t-go-to-delegate-tp4413462p4413550.html
Sent from the groovy - user mailing list archive at Nabble.com.

Reply all
Reply to author
Forward
0 new messages