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

GroovyCastException when @CompileStatic and !in keyword are used together

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.9
    • 4.0.0-beta-2, 3.0.10
    • Static compilation
    • None
    • macOs Big Sur 11.5.2
      2,6 GHz 6-Core Intel Core i7
      Java 11.0.11/16.0.1 (probably reproducible in other version)

    Description

      Dear colleagues,

      I've recently updated my projects from 3.0.8 to 3.0.9 and I started getting some GroovyCastExceptions when trying to use !in keyword along with CompileStatic annotation:

      org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'true' with class 'java.lang.Boolean' to class 'Xyz'
      

      We realised that it only works on 3.0.9 if we remove the CompileStatic annotation(3.0.8 works with and without).

      Here's an example of how to reproduce the exception:

      import groovy.transform.CompileStatic
      
      /**
       *  REMOVE @CompileStatic and the main method runs fine for both versions 3.0.8 and 3.0.9.
       * 'Cannot cast object \'true\' with class \'java.lang.Boolean\' to class \'MyEnum\''
       *  @CompileStatic only works with 3.0.8
       */
      @CompileStatic
      class Groovy309Error {
          static void main(String[] args) {
              def myObj = new MyObj(status: MyEnum.A)
              List<MyObj> items = [myObj]
              def newItems = items.findAll { it.status !in [MyEnum.B] }
              println newItems
          }
      }
      
      enum MyEnum {
          A, B
      }
      
      class MyObj {
          MyEnum status
      }

       
      Please, forgive me if I missed something on the bug checklist but I think this ticket belongs here.
      Thank you in advance 

      Attachments

        Issue Links

          Activity

            People

              emilles Eric Milles
              rodolfo.yanke Rodolfo Yanke
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: