groovy
  1. groovy
  2. GROOVY-4215

exception trying to define method in closure

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.8-rc-4, 1.7.11
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      1

      Description

      {->
          def say(String msg) {
            println(msg)
          }
      }()
      

      throws:

      1 compilation error:
      
      Unknown type: METHOD_DEF at line: 2 column: 5. File: predef.groovy at Exception in thread "AWT-EventQueue-0" 
      groovy.lang.MissingPropertyException: No such property: column for class: org.codehaus.groovy.antlr.ASTParserException
      	at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:49)
      	at org.codehaus.groovy.runtime.callsite.GetEffectivePojoPropertySite.getProperty(GetEffectivePojoPropertySite.java:63)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGetProperty(AbstractCallSite.java:237)
      	at groovy.ui.Console$_finishException_closure9.doCall(Console.groovy:636)
      	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:272)
      	at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:886)
      	at groovy.lang.Closure.call(Closure.java:276)
      	at groovy.lang.Closure.call(Closure.java:289)
      	at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1176)
      	at org.codehaus.groovy.runtime.DefaultGroovyMethods.each(DefaultGroovyMethods.java:1152)
      	at org.codehaus.groovy.runtime.dgm$111.invoke(Unknown Source)
      	at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite$PojoMetaMethodSiteNoUnwrapNoCoerce.invoke(PojoMetaMethodSite.java:270)
      	at org.codehaus.groovy.runtime.callsite.PojoMetaMethodSite.call(PojoMetaMethodSite.java:52)
      	at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
      	at groovy.ui.Console.finishException(Console.groovy:619)
      	at sun.reflect.GeneratedMethodAccessor441.invoke(Unknown Source)
      	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:361)
      	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_closure29.doCall(Console.groovy:868)
      	at sun.reflect.GeneratedMethodAccessor655.invoke(Unknown Source)
      	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:272)
      	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_closure29.doCall(Console.groovy)
      	at sun.reflect.GeneratedMethodAccessor654.invoke(Unknown Source)
      	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:272)
      	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.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
      	at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
      	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
      	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
      	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
      	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
      	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
      	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
      

        Activity

        Hide
        Paul King added a comment -

        Yes, a better error message would be good. The fact that this is running in GroovyConsole adds a bit of noise to the stack trace (itself a smell, but we'll ignore that here). For the purposes of fixing this issue, here is a better view of the cause - obtained by running from the command-line:

        Caused by: org.codehaus.groovy.antlr.ASTRuntimeException: Unknown type: METHOD_DEF at line: 2 column: 5
                at org.codehaus.groovy.antlr.AntlrParserPlugin.unknownAST(AntlrParserPlugin.java:2952)
                at org.codehaus.groovy.antlr.AntlrParserPlugin.expressionSwitch(AntlrParserPlugin.java:1856)
                ...
        
        Show
        Paul King added a comment - Yes, a better error message would be good. The fact that this is running in GroovyConsole adds a bit of noise to the stack trace (itself a smell, but we'll ignore that here). For the purposes of fixing this issue, here is a better view of the cause - obtained by running from the command-line: Caused by: org.codehaus.groovy.antlr.ASTRuntimeException: Unknown type: METHOD_DEF at line: 2 column: 5 at org.codehaus.groovy.antlr.AntlrParserPlugin.unknownAST(AntlrParserPlugin.java:2952) at org.codehaus.groovy.antlr.AntlrParserPlugin.expressionSwitch(AntlrParserPlugin.java:1856) ...
        Hide
        Paul King added a comment - - edited

        Attached patch gives a slightly better error message:

        Caused by: org.codehaus.groovy.antlr.ASTRuntimeException: Found a method definition when expecting an expression. Possible attempt to define a method inside a closure? Define the method outside or use an inner closure. at line: 2 column: 5
                at org.codehaus.groovy.antlr.AntlrParserPlugin.unknownAST(AntlrParserPlugin.java:2953)
                at org.codehaus.groovy.antlr.AntlrParserPlugin.expressionSwitch(AntlrParserPlugin.java:1856)
                ...
        

        but we need to be sure that this error can't be stopped at the grammar level first or indeed if we might want to support such definitions at some point.

        Show
        Paul King added a comment - - edited Attached patch gives a slightly better error message: Caused by: org.codehaus.groovy.antlr.ASTRuntimeException: Found a method definition when expecting an expression. Possible attempt to define a method inside a closure? Define the method outside or use an inner closure. at line: 2 column: 5 at org.codehaus.groovy.antlr.AntlrParserPlugin.unknownAST(AntlrParserPlugin.java:2953) at org.codehaus.groovy.antlr.AntlrParserPlugin.expressionSwitch(AntlrParserPlugin.java:1856) ... but we need to be sure that this error can't be stopped at the grammar level first or indeed if we might want to support such definitions at some point.
        Paul King made changes -
        Field Original Value New Value
        Attachment groovy4215_betterErrorMsg.patch [ 49156 ]
        Hide
        Roshan Dawrani added a comment - - edited

        I think it might be better not to guess the enclosing syntax.

        Say I run the code

        switch(1) {
        	case 1: def say(){}
        }
        

        The guess and the suggestion are not applicable now as the wrong method def is not inside a closure and not an "inner" closure, but a closure would help.

        How about just saying "Method definition not expected here. Please define the method at an appropriate place or try using a closure instead."?

        Show
        Roshan Dawrani added a comment - - edited I think it might be better not to guess the enclosing syntax. Say I run the code switch (1) { case 1: def say(){} } The guess and the suggestion are not applicable now as the wrong method def is not inside a closure and not an "inner" closure, but a closure would help. How about just saying "Method definition not expected here. Please define the method at an appropriate place or try using a closure instead."?
        Hide
        Roshan Dawrani added a comment -

        Just looking through issues - this one seems on the verge of getting resolved , if we can finalize the error message.

        Show
        Roshan Dawrani added a comment - Just looking through issues - this one seems on the verge of getting resolved , if we can finalize the error message.
        Hide
        Paul King added a comment -

        Roshan, I like your suggested error message.

        Show
        Paul King added a comment - Roshan, I like your suggested error message.
        Hide
        blackdrag blackdrag added a comment -

        I suggest a small change to the error message: "Method definition not expected here. Please define the method at an appropriate place or try using a block instead."? We should be a bit carefull what we justify as closure

        Show
        blackdrag blackdrag added a comment - I suggest a small change to the error message: "Method definition not expected here. Please define the method at an appropriate place or try using a block instead."? We should be a bit carefull what we justify as closure
        Hide
        Paul King added a comment -

        improved error message was added - thanks for filing the issue

        Show
        Paul King added a comment - improved error message was added - thanks for filing the issue
        Paul King made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Assignee Paul King [ paulk ]
        Fix Version/s 1.7.11 [ 17244 ]
        Fix Version/s 1.8-rc-4 [ 17245 ]
        Resolution Fixed [ 1 ]
        Paul King made changes -
        Status Resolved [ 5 ] Closed [ 6 ]

          People

          • Assignee:
            Paul King
            Reporter:
            Ittay Dror
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: