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

        Graeme Rocher made changes -
        Field Original Value New Value
        Description Example code:

        {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
            }
        }
        {code}
        Example code:

        {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
            }
        }
        {code}

        Exception:

        {code}
        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
        {code}

        The issue appears to be that a FieldExpression is created with a null FieldNode when calling the constructor new Sql(connection)
        Graeme Rocher made changes -
        Summary Anonymous inner call constructor call referencing a getter caused NPE at compile time Anonymous inner class constructor call referencing a getter caused NPE at compile time
        Hide
        blackdrag blackdrag added a comment -

        fixed

        Show
        blackdrag blackdrag added a comment - fixed
        blackdrag blackdrag made changes -
        Status Open [ 1 ] Closed [ 6 ]
        Assignee Jochen Theodorou [ blackdrag ]
        Fix Version/s 1.9-beta-4 [ 17656 ]
        Resolution Fixed [ 1 ]

          People

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

            Dates

            • Created:
              Updated:
              Resolved: