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

Anonymous inner class constructor call referencing a getter caused NPE at compile time

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Blocker
    • Resolution: Fixed
    • 1.8.3
    • 1.8.3, 1.9-beta-4
    • None
    • None

    Description

      Example code:

      import java.sql.Connection
      import groovy.sql.Sql
      
      class GrailsPrecondition {
      
          Connection getConnection() { database?.connection?.wrappedConnection }
          /**
           * Called from the check closure. Creates a <code>Sql</code> instance from the current connection.
           *
           * @return the sql instance
           */
          Sql getSql() {
              if (!connection) return null
      
              if (!sql) {
                  sql = new Sql(connection) {
                      protected void closeResources(Connection c) {
                          // do nothing, let Liquibase close the connection
                      }
                  }
              }
      
              sql
          }
      }
      

      Exception:

      java.lang.NullPointerException
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitFieldExpression(AsmClassGenerator.java:1081)
      	at org.codehaus.groovy.classgen.asm.ClosureWriter.loadReference(ClosureWriter.java:123)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.loadVariableWithReference(InvocationWriter.java:433)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.writeAICCall(InvocationWriter.java:417)
      	at org.codehaus.groovy.classgen.asm.InvocationWriter.writeInvokeConstructor(InvocationWriter.java:385)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitConstructorCallExpression(AsmClassGenerator.java:914)
      	at org.codehaus.groovy.ast.expr.ConstructorCallExpression.visit(ConstructorCallExpression.java:43)
      	at org.codehaus.groovy.classgen.asm.BinaryExpressionHelper.evaluateEqual(BinaryExpressionHelper.java:283)
      	at org.codehaus.groovy.classgen.asm.BinaryExpressionHelper.eval(BinaryExpressionHelper.java:72)
      	at org.codehaus.groovy.classgen.AsmClassGenerator.visitBinaryExpression(AsmClassGenerator.java:527)
      	at org.codehaus.groovy.ast.expr.BinaryExpression.visit(BinaryExpression.java:49)
      	at org.codehaus.groovy.classgen.asm.StatementWriter.writeExpressionStatement(StatementWriter.java:599)
      	at org.cod
      

      The issue appears to be that a FieldExpression is created with a null FieldNode when calling the constructor new Sql(connection)

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            graemerocher Graeme Rocher
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: