groovy
  1. groovy
  2. GROOVY-5041

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

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: 1.8.3
    • Fix Version/s: 1.8.3, 1.9-beta-4
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      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)

        Activity

          People

          • Assignee:
            blackdrag blackdrag
            Reporter:
            Graeme Rocher
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: