groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Shon Vella (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (GROOVY-8409) Static compilation with generic function wrapping BiFunction causes GroovyCastException
Date Tue, 12 Dec 2017 13:39:00 GMT

     [ https://issues.apache.org/jira/browse/GROOVY-8409?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Shon Vella updated GROOVY-8409:
-------------------------------
    Description: 
I have a statically compiled with a method 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.

{code:java}
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
    }
{code}

When actionWrapperT is called with runtime type of T as something other the Date, it causes
a GroovyCastException.
{panel}
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'XXX@71b1176b'
with class 'XXX' to class 'java.util.Date'
{panel}

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

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.



  was:
I have a statically compiled with a method 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.

{code:java}
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
    }
{code}

When actionWrapperT is called with runtime type of T as something other the Date, it causes
a GroovyCastException.
{noformat}
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'XXX@71b1176b'
with class 'XXX' to class 'java.util.Date'
{noformat}

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

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.




> Static compilation with generic function wrapping BiFunction causes GroovyCastException
> ---------------------------------------------------------------------------------------
>
>                 Key: GROOVY-8409
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8409
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 2.4.8, 2.4.13
>         Environment: MacOS Sierra 10.12.6
>            Reporter: Shon Vella
>         Attachments: Bug.groovy
>
>
> I have a statically compiled with a method 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.
> {code:java}
> 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
>     }
> {code}
> When actionWrapperT is called with runtime type of T as something other the Date, it
causes a GroovyCastException.
> {panel}
> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'XXX@71b1176b'
with class 'XXX' to class 'java.util.Date'
> {panel}
> because it incorrectly tries to cast result type Date rather than to .
> 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.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message