[jira] [Comment Edited] (GROOVY-7971) @CS flow typing incorrectly casting to map at runtime

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[jira] [Comment Edited] (GROOVY-7971) @CS flow typing incorrectly casting to map at runtime

JIRA jira@apache.org

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

Paul King edited comment on GROOVY-7971 at 4/16/18 11:33 AM:
-------------------------------------------------------------

A slightly simpler example:
{code}
import groovy.json.*
import groovy.transform.*

@CompileStatic
class Bar {
    def meth(value) {
        Writer out = new StringWriter()
        def isString = value instanceof String
//        if (isString || value instanceof Map) { // (1)
        if (value instanceof String || value instanceof Map) { // (2)
            out.append(JsonOutput.toJson(value))
        }
        out
    }
}

def writer = new Bar().meth('two')
println writer.toString()
{code}
Here (2) works but (1) fails. And it's not just that the instanceof String isn't detected but that Map is selected even though the {{||}} binary expression exists.


was (Author: paulk):
A slightly simpler example:
{code}
import groovy.json.*
import groovy.transform.*

@CompileStatic
class Bar {
    def meth(value) {
        Writer out = new StringWriter()
        def isString = value instanceof String
//        if (isString || value instanceof Map) { // (1)
        if (value instanceof String || value instanceof Map) { // (2)
            out.append(JsonOutput.toJson(value))
        }
        out
    }
}

def writer = new Bar().meth('two')
println writer.toString()
{code}
Here (2) works but (1) fails.

> @CS flow typing incorrectly casting to map at runtime
> -----------------------------------------------------
>
>                 Key: GROOVY-7971
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7971
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.4.7
>            Reporter: Graeme Rocher
>            Priority: Major
>
> The following code:
> {code}
> import groovy.json.*
> import groovy.transform.*
> @CompileStatic
> class Bar {
>     private Writable renderTemplate(Object o, Map args) {
>    
>     }
>     private boolean isSimpleType(Class type) {
>         return type == String
>     }
>     def foo(Map map, Map arguments) {
>    
>         def writable = new Writable() {
>             @Override
>             Writer writeTo(Writer out) throws IOException {
>                 for(entry in map.entrySet()) {
>                    def value = entry.value
>                    if(isSimpleType(value.getClass()) || (value instanceof Map)) {
>                         out.append(JsonOutput.toJson(value))
>                     }
>                 }
>                 return out
>             }
>         }
>     }
> }
> writable = new Bar().foo([one:'two'],[foo:'bar'])
> sw = new StringWriter()
> writable.writeTo(sw)
> println sw
> {code}
> Fails with:
> {code}
> org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'two' with class 'java.lang.String' to class 'java.util.Map'
> at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnSAM(DefaultTypeTransformation.java:405)
> at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.continueCastOnNumber(DefaultTypeTransformation.java:319)
> at org.codehaus.groovy.runtime.typehandling.DefaultTypeTransformation.castToType(DefaultTypeTransformation.java:232)
>
> {code}
> For some reason Groovy is attempting to cast value to Map when it isn't one.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)