Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-5756

error during class generation: cannot define closures inside methods of an enum instance

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.0.5
    • 2.1.3, 2.2.0-beta-1
    • None
    • None
    • Mac OS X 10.7.5, IntelliJ IDEA 11.1.3

    Description

      The following code fails to run:

      enum MyEnum {
          INSTANCE {
              String foo() {
                  def clos = {
                      "foo"
                  }
                  clos.call()
              }
          }
      
          String foo() {
          }
      }
      
      assert "foo" == MyEnum.INSTANCE.foo()
      

      The Groovy runtime throws the following exception when trying to execute it:

      org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
      General error during class generation: NPE while processing ClosureDefiningEnumInstanceMethodTest.groovy
      
      groovy.lang.GroovyRuntimeException: NPE while processing ClosureDefiningEnumInstanceMethodTest.groovy
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:198)
      	at org.codehaus.groovy.control.CompilationUnit$14.call(CompilationUnit.java:786)
      	at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1027)
      	at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:564)
      	at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:542)
      	at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:519)
      	at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:279)
      	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:258)
      	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:244)
      	at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:185)
      	at groovy.lang.GroovyShell$2.run(GroovyShell.java:206)
      	at groovy.lang.GroovyShell$2.run(GroovyShell.java:204)
      	at java.security.AccessController.doPrivileged(Native Method)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:204)
      	at groovy.lang.GroovyShell.run(GroovyShell.java:150)
      	at groovy.ui.GroovyMain.processOnce(GroovyMain.java:557)
      	at groovy.ui.GroovyMain.run(GroovyMain.java:344)
      	at groovy.ui.GroovyMain.process(GroovyMain.java:330)
      	at groovy.ui.GroovyMain.processArgs(GroovyMain.java:119)
      	at groovy.ui.GroovyMain.main(GroovyMain.java:99)
      	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.tools.GroovyStarter.rootLoader(GroovyStarter.java:106)
      	at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:128)
      Caused by: java.lang.NullPointerException
      	at org.codehaus.groovy.classgen.asm.ClosureWriter.createClosureClass(ClosureWriter.java:184)
      	at org.codehaus.groovy.classgen.asm.ClosureWriter.getOrAddClosureClass(ClosureWriter.java:150)
      	at org.codehaus.groovy.classgen.asm.ClosureWriter.writeClosure(ClosureWriter.java:77)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitClosureExpression(AsmClassGenerator.java:546)
      	at org.codehaus.groovy.ast.expr.ClosureExpression.visit(ClosureExpression.java:43)
      	at org.codehaus.groovy.classgen.asm.BinaryExpressionHelper.evaluateEqual(BinaryExpressionHelper.java:295)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitDeclarationExpression(AsmClassGenerator.java:522)
      	at org.codehaus.groovy.ast.expr.DeclarationExpression.visit(DeclarationExpression.java:87)
      	at org.codehaus.groovy.classgen.asm.StatementWriter.writeExpressionStatement(StatementWriter.java:604)
      	at org.codehaus.groovy.classgen.asm.OptimizingStatementWriter.writeExpressionStatement(OptimizingStatementWriter.java:354)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitExpressionStatement(AsmClassGenerator.java:509)
      	at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:40)
      	at org.codehaus.groovy.classgen.asm.StatementWriter.writeBlockStatement(StatementWriter.java:81)
      	at org.codehaus.groovy.classgen.asm.OptimizingStatementWriter.writeBlockStatement(OptimizingStatementWriter.java:155)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitBlockStatement(AsmClassGenerator.java:455)
      	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:319)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitConstructorOrMethod(AsmClassGenerator.java:276)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:123)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethod(AsmClassGenerator.java:396)
      	at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1056)
      	at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:50)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:180)
      	... 25 more
      
      1 error
      

      As the stacktrace suggests, the problem is caused by the closure definition in the enum instance specific foo() method.

      Attachments

        Activity

          People

            paulk Paul King
            zagyvaibalazs Balazs Zagyvai
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: