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

Annotation based logging of complex expression in closure throws GroovyCastException when statically compiled

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.4.8
    • 2.4.9
    • Static compilation
    • None

    Description

      Example:

      @groovy.util.logging.Log
      @groovy.transform.CompileStatic
      class Foo {
          void bar() {
              def callable = {
                  log.info('a' + 1)
              }
              callable.call()
          }
      }
      new Foo().bar()
      

      The log statement in the code above results in the following runtime exception:

      org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Foo@14ec4505' with class 'Foo' to class 'java.util.logging.Logger'
      

      The case where the closure is in a static method is similar; the object being cast to a Logger is a Class instead.

      This appears to be due to the implicitThis field being set incorrectly on the guard method call expression (log.isLoggable(Level) in the default case) created in implementations of LoggingStrategy.wrapLoggingMethodCall.

      Prior to Groovy 2.4.8, log statements inside closures were not guarded (https://issues.apache.org/jira/browse/GROOVY-6932), so this bug wasn't visible.

      Attachments

        Issue Links

          Activity

            People

              shils Shil Sinha
              shils Shil Sinha
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: