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

Static compilation with generic function wrapping BiFunction causes GroovyCastException

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Fixed
    • 2.4.8, 2.4.13
    • 4.0.0-beta-1, 2.5.17, 3.0.11
    • Static compilation
    • None
    • MacOS Sierra 10.12.6

    Description

      I have a statically compiled class with a method that declares Generic type T as it's return type and accepts a parameter of type java.util.function.BiFunction also with return type T. It makes a call to the passed in BiFunction and assigns the the result to a variable of type T.

      static <T> T actionWrapperT(BiFunction<Date, URL, T> action) {
              T result = action.apply(new Date(), new URL('http://www.example.com'))
              // do something else here
              return result
          }
      

      When actionWrapperT is called with runtime type T as something other than Date (e.g. XXX), it causes a GroovyCastException.

      org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'XXX@71b1176b' with class 'XXX' to class 'java.util.Date'

      because it incorrectly tries to cast result type to Date rather than to XXX.

      It appears to me that the compiler is conflating generic type T as declared for the generic method with generic type T as declared by BiFunction<T,U,R> because if I change the name of the generic type of the generic method to R (to match the return type name of BiFunction) or to some other name not used by BiFunction, then it works correctly, but if I change it to U to match the second parameter of the BiFunction then it fails trying to cast to the BiFunction generic type U instead of the method generic type U.

      Problem does not happen under normal compilation or with only type checking enabled.

      Attached file Bug.groovy reproduces the problem.

      Attachments

        1. Bug.groovy
          1 kB
          Shon Vella

        Issue Links

          Activity

            People

              emilles Eric Milles
              svella Shon Vella
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: