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

asType(null) called in @CompileStatic method returns a string 'null'

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.4.7
    • 5.0.0-alpha-3
    • Static compilation
    • None
    • Win, jdk1.7.79

    Description

      The following code:

      import groovy.transform.CompileStatic
      class Test {
          @CompileStatic
          static Object castToType(Object o, Class clazz) { 
              return o.asType(clazz)
          }
      }
      def foo = Test.castToType(null, String.class)
      println "'${foo}' class: ${foo.getClass()} bytes: ${foo?.getBytes()}"
      

      prints:

      'null' class: class java.lang.String bytes: [110, 117, 108, 108]

      i.e. the method annotated with @CompileStatic converts null to the actual string 'null' (which is quite a nasty surprise when trying to "speed things up" by adding @CompileStatic to existing non-static code).

      The same without @CompileStatic returns:

      'null' class: class org.codehaus.groovy.runtime.NullObject bytes: null

      as expected.

      Attachments

        Activity

          People

            emilles Eric Milles
            qjacekk Jacek Karas
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: