[jira] [Comment Edited] (GROOVY-8255) Odd problems with flow typing and generics in Groovy 2.4.12+

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

[jira] [Comment Edited] (GROOVY-8255) Odd problems with flow typing and generics in Groovy 2.4.12+

JIRA jira@apache.org

    [ https://issues.apache.org/jira/browse/GROOVY-8255?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16087365#comment-16087365 ]

Graeme Rocher edited comment on GROOVY-8255 at 7/14/17 2:12 PM:
----------------------------------------------------------------

Another incarnation of this problem has appeared:

https://travis-ci.org/grails/gorm-mongodb/builds/253607974#L430

Compilation error is:

{code}
EnumType.groovy: 144: [Static type checking] - Inconvertible types: cannot cast int to java.lang.Enum
 @ line 144, column 31.
                   value = getId((Enum)value)
                                 ^
{code}

It thinks that {{value}} is an integer which makes no sense given the logic:

{code}
        if (value instanceof Enum) {
            if (isOrdinalTypeEnum(property)) {
                value = ((Enum) value).ordinal()
            } else if (value.hasProperty(GormProperties.IDENTITY)) {
                value = getId((Enum)value)
            } else {
                value = ((Enum)value).name()
            }
        }
{code}

See https://github.com/grails/gorm-mongodb/blob/master/grails-datastore-gorm-mongodb/src/main/groovy/org/grails/datastore/gorm/mongo/simple/EnumType.groovy#L144

Seems Groovy is not taking into account the conditional logic when associated the type with an untyped value. It is associating the return type of {{original()}} in the previous {{if}} block with the {{value}} which is incorrect.


was (Author: graemerocher1):
Another incarnation of this problem has appeared:

https://travis-ci.org/grails/gorm-mongodb/builds/253607974#L430

Compilation error is:

{code}
EnumType.groovy: 144: [Static type checking] - Inconvertible types: cannot cast int to java.lang.Enum
 @ line 144, column 31.
                   value = getId((Enum)value)
                                 ^
{code}

It thinks that {{value}} is an integer which makes no sense given the logic:

{code}
        if (value instanceof Enum) {
            if (isOrdinalTypeEnum(property)) {
                value = ((Enum) value).ordinal()
            } else if (value.hasProperty(GormProperties.IDENTITY)) {
                value = getId((Enum)value)
            } else {
                value = ((Enum)value).name()
            }
        }
{code}

See https://github.com/grails/gorm-mongodb/blob/master/grails-datastore-gorm-mongodb/src/main/groovy/org/grails/datastore/gorm/mongo/simple/EnumType.groovy#L144

Seems Groovy is not taking into account the conditional logic when associated the type with an untyped value.

> Odd problems with flow typing and generics in Groovy 2.4.12+
> ------------------------------------------------------------
>
>                 Key: GROOVY-8255
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8255
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.4.12
>            Reporter: Graeme Rocher
>
> In order to get the GORM codebase to compile I had to make this change:
> https://github.com/grails/grails-data-mapping/commit/1ef850c496d13d8ca915b27e76b6bfdb4e27377e
> The code in question is:
> {code}
>     /**
>      * Sets multipart values within the request body
>      *
>      * @param name The name of the multipart
>      * @param value The value of the multipart
>      */
>     void setProperty(String name, value) {
>         if (value instanceof File) {
>             value = new FileSystemResource(value)
>         }
>         else if (value instanceof URL) {
>             value = new UrlResource(value)
>         }
>         else if (value instanceof InputStream) {
>             value = new InputStreamResource(value)
>         }
>         else if (value instanceof GString) {
>             value = value.toString()
>         }
>         if( mvm[name] ) {
>             mvm[name].add value    
>         }
>         else {
>             mvm.put(name, [value]) // <--- FAILS COMPILATION HERE
>         }        
>     }
> {code}
> No matter what I tried I could not get it into to compile. The method accepts `put(String, List<Object>)` but fails compilation with:
> {code}
> RequestCustomizer.groovy: 392: [Static type checking] - Cannot call org.springframework.util.MultiValueMap <String, Object>#put(java.lang.String, java.lang.Object) with arguments [java.lang.String, java.util.List <java.lang.String>]
>  @ line 392, column 13.
>                mvm.put(name, [value])
>                ^
> {code}
> Altering the code to:
> {code}
>        List<Object> values = [value]
>        mvm.put(name, values)
> {code}
> Fails with:
> {code}
> RequestCustomizer.groovy: 392: [Static type checking] - Incompatible generic argument types. Cannot assign java.util.List <java.lang.String> to: java.util.List <Object>
>  @ line 392, column 35.
>                List<Object> values = [value]
>                                      ^
> RequestCustomizer.groovy: 393: [Static type checking] - Cannot call org.springframework.util.MultiValueMap <String, Object>#put(java.lang.String, java.lang.Object) with arguments [java.lang.String, java.util.List <java.lang.String>]
>  @ line 393, column 13.
>                mvm.put(name, values)
>                ^
> 2 errors
> {code}



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