You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2020/12/22 19:05:00 UTC

[jira] [Resolved] (GROOVY-9872) GroovyCastException when using a map while iterating over something in a @CompileStatic object

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

Eric Milles resolved GROOVY-9872.
---------------------------------
    Fix Version/s: 2.5.15
       Resolution: Fixed

This is the same issue as GROOVY-9771 and is already fixed in the Groovy 2.5 branch.

> GroovyCastException when using a map while iterating over something in a @CompileStatic object
> ----------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-9872
>                 URL: https://issues.apache.org/jira/browse/GROOVY-9872
>             Project: Groovy
>          Issue Type: Bug
>          Components: groovy-jdk
>    Affects Versions: 2.5.14
>            Reporter: Simon Rehm
>            Assignee: Eric Milles
>            Priority: Major
>             Fix For: 2.5.15
>
>
> This odd code works fine in 2.5.13, but explodes in 2.5.14. 
>  My workaround for now is to remove @CompileStatic in production.
> Minimal example (spock test):
> {code:java}
> import groovy.transform.CompileStatic
> import spock.lang.Specification
> class FooTest extends Specification {
>     def "test"() {
>         when:
>         new Foo()
>         then:
>         noExceptionThrown()
>     }
> }
> @CompileStatic
> class Foo {
>     private Map<String, String> cache = [:]
>     Foo() {
>         [''].each {
>             cache[''] = ''
>         }
>     }
> }{code}
>  Minimal example without Spock, while settings stuff in a map in an .each-iteration outside of Constructor:
> {code:java}
> new Foo().init()
> @CompileStatic
> class Foo {
>     private Map<String, String> cache = [:]
>     Foo() {
>     }
>     void init() {
>         [''].each {
>             cache[it] = it
>         }
>     }
> }
> {code}
>  
>  
> Full Error:
> {code:java}
> Expected no exception to be thrown, but got 'org.codehaus.groovy.runtime.typehandling.GroovyCastException'	at spock.lang.Specification.noExceptionThrown(Specification.java:118)
> 	at FooTest.test(FooTest.groovy:10)
> Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'Foo$_closure1@3fa2213' with class 'Foo$_closure1' to class 'Foo'
> 	at Foo$_closure1.doCall(FooTest.groovy:19)
> 	at Foo.<init>(FooTest.groovy:18)
> 	at FooTest.test(FooTest.groovy:7)
> {code}
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)