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

CompileStatic doesn't call asBoolean

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Critical
    • Resolution: Fixed
    • 2.5.18, 3.0.12, 4.0.4
    • 4.0.5, 2.5.19, 3.0.13
    • Static compilation
    • None

    Description

      This appears to be a duplicate of GROOVY-7807, but the test cases presented don't quite cover the behavior I see. I have a custom record class

      @Immutable
      @CompileStatic
      @POJO
      static class Result {
        String channel
        String thread
      
        boolean asBoolean() {
          println 'in asBoolean'
          false // for debugging
        }
      }
      

      When I call this from another class that is also statically compiled, the asBoolean method is not called.

      if (!result) { // console prints diagnostic
        throw new NoChannelException() // block is not entered
      }
      
              12: invokeinterface #136,  3          // load the Result
              17: astore_2
              18: aload_2
              19: pop
              20: aload_2
              21: dup
              22: ifnonnull     30
              25: pop
              26: iconst_0
              27: goto          32
              30: pop
              31: iconst_1
              32: ifne          39
              35: iconst_1
              36: goto          40
              39: iconst_0
              40: ifeq          58
              43: new           #138                // class NoChannelException
      

      Both classes are in the same compilation unit, which may be relevant to the behavior, as @CompileStatic seems to correctly pull in {{asBoolean}}s from library code and extension method. However, in this case it's quite clearly disregarding the custom method and relying solely on a null check.

      Attachments

        Activity

          People

            emilles Eric Milles
            chrylis Christopher Smith
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: