Groovy
  1. Groovy
  2. GROOVY-4787

BUG! exception in phase 'class generation' generating class from map in closure

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 1.7.10, 1.8-rc-4, 2.4.0-rc-1
    • Fix Version/s: 2.5.0-beta-1
    • Component/s: class generator
    • Labels:
      None

      Description

      If I have a class like so:

      class Person {
        String name
        int age
      }
      

      Then it is possible to create an instance of Person using the little known construct:

      def tim = Person [ name:'tim', age:49 ]
      

      This works fine.

      However, if I try to do the following:

      def ppl = [ [ name:'Tim', age:49 ], [ name:'Dave', age:32 ], [ name:'Steve', age:28 ] ]
      
      ppl.collect { Person [ *it ] }
      

      I get the class generation error

      BUG! exception in phase 'class generation' in source unit 'ConsoleScript10' SpreadExpression should not be visited here
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitSpreadExpression(AsmClassGenerator.java:1871)
      	at org.codehaus.groovy.ast.expr.SpreadExpression.visit(SpreadExpression.java:39)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitAndAutoboxBoolean(AsmClassGenerator.java:4122)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.makeBinopCallSite(AsmClassGenerator.java:2268)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.evaluateBinaryExpression(AsmClassGenerator.java:3902)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitBinaryExpression(AsmClassGenerator.java:1662)
      	at org.codehaus.groovy.ast.expr.BinaryExpression.visit(BinaryExpression.java:49)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitAndAutoboxBoolean(AsmClassGenerator.java:4122)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.evaluateExpression(AsmClassGenerator.java:1447)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitReturnStatement(AsmClassGenerator.java:1408)
      	at org.codehaus.groovy.ast.stmt.ReturnStatement.visit(ReturnStatement.java:47)
      	at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:35)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:165)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitBlockStatement(AsmClassGenerator.java:738)
      	at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:69)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:101)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:112)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitStdMethod(AsmClassGenerator.java:626)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitConstructorOrMethod(AsmClassGenerator.java:601)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:123)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethod(AsmClassGenerator.java:696)
      	at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1039)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:50)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:276)
      	at org.codehaus.groovy.control.CompilationUnit$12.call(CompilationUnit.java:748)
      	at org.codehaus.groovy.control.CompilationUnit$12.call(CompilationUnit.java:765)
      	at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:942)
      	at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:519)
      	at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:497)
      	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:474)
      	at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306)
      	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:287)
      	at groovy.lang.GroovyShell.parseClass(GroovyShell.java:731)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:516)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:172)
      	at groovy.lang.GroovyShell$run.call(Unknown Source)
      	at groovy.ui.Console$_runScriptImpl_closure16.doCall(Console.groovy:904)
      	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:90)
      	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:149)
      	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:90)
      	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:282)
      	at groovy.lang.Closure.call(Closure.java:277)
      	at groovy.lang.Closure.run(Closure.java:360)
      	at java.lang.Thread.run(Thread.java:680)
      

      I know I should be doing:

      ppl.collect { new Person( *:it ) }
      

      (which works fine), but I just thought you should know about this BUG! exception

        Issue Links

          Activity

          Tim Yates created issue -
          Hide
          Tim Yates added a comment - - edited

          PS: For completeness, this:

          ppl.collect { Person it }
          

          gives

          The current scope already contains a variable of the name it
          

          and

          ppl.collect { Person [ *:it ] }
          

          gives the error

          groovy.lang.MissingMethodException: No signature of method: static Person.getAt() is applicable for argument types: (groovy.lang.SpreadMap) values: [[:]]
          
          Show
          Tim Yates added a comment - - edited PS: For completeness, this: ppl.collect { Person it } gives The current scope already contains a variable of the name it and ppl.collect { Person [ *:it ] } gives the error groovy.lang.MissingMethodException: No signature of method: static Person.getAt() is applicable for argument types: (groovy.lang.SpreadMap) values: [[:]]
          Hide
          Jochen Theodorou added a comment -
          Person it

          is a variable declaration, that's why it complains about a variable "it" already being available

          Show
          Jochen Theodorou added a comment - Person it is a variable declaration, that's why it complains about a variable "it" already being available
          Hide
          Tim Yates added a comment -

          Of course! Didn't see that! (doh)

          Show
          Tim Yates added a comment - Of course! Didn't see that! (doh)
          Pascal Schumacher made changes -
          Field Original Value New Value
          Affects Version/s 2.4.0-rc-1 [ 20785 ]
          Mark Thomas made changes -
          Project Import Sun Apr 05 13:32:57 UTC 2015 [ 1428240777691 ]
          Mark Thomas made changes -
          Workflow jira [ 12733715 ] Default workflow, editable Closed status [ 12745527 ]
          Mark Thomas made changes -
          Project Import Mon Apr 06 02:11:23 UTC 2015 [ 1428286283443 ]
          Mark Thomas made changes -
          Workflow jira [ 12973689 ] Default workflow, editable Closed status [ 12980852 ]
          Jochen Theodorou made changes -
          Assignee Jochen Theodorou [ blackdrag ]
          Paul King made changes -
          Link This issue relates to GROOVY-7447 [ GROOVY-7447 ]
          Paul King made changes -
          Status Open [ 1 ] Resolved [ 5 ]
          Assignee Paul King [ paulk ]
          Fix Version/s 2.5.0-beta-1 [ 12331949 ]
          Resolution Fixed [ 1 ]
          Hide
          Paul King added a comment -

          Thanks for raising the issue Tim.

          Show
          Paul King added a comment - Thanks for raising the issue Tim.
          Transition Time In Source Status Execution Times Last Executer Last Execution Date
          Open Open Resolved Resolved
          1504d 7h 38m 1 Paul King 01/Jun/15 14:00

            People

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

              Dates

              • Created:
                Updated:
                Resolved:

                Development