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

Paul King commented on GROOVY-7996:
-----------------------------------

I haven't looked into why yet but if the bar field is protected or public you 
get the error. Workarounds are to make it a normal property (no visibility 
modifier) or private. Obviously, might not be ideal or work for you depending 
on your scenario.

> Using with method with a closure that references a protected property 
> produces ClassCastException
> -------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-7996
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7996
>             Project: Groovy
>          Issue Type: Bug
>    Affects Versions: 2.4.7
>            Reporter: Graeme Rocher
>
> The following example:
> {code}
> class Foo {
>     Object propertyMissing(String name) {
>          return "stuff"
>     }
>     
>     void build(Closure callable) {
>          this.with(callable)
>     }
> }
> @groovy.transform.CompileStatic
> class Bar {
>     protected List bar = []
>     
>     boolean doStuff() {
>         Foo foo = new Foo()
>         foo.build {
>            return bar.isEmpty() 
>         }
>     }
> }
> new Bar().doStuff()
> {code}
> Produces 
> {code}
> java.lang.ClassCastException: java.lang.String cannot be cast to 
> java.util.List
>       at Bar$_doStuff_closure1.doCall(ConsoleScript3:19)
>       at Bar$_doStuff_closure1.call(ConsoleScript3)
>       at 
> org.codehaus.groovy.runtime.DefaultGroovyMethods.with(DefaultGroovyMethods.java:242)
>       at Bar.doStuff(ConsoleScript3:18)
>       at Ba
> {code}
> The equivalent code without CompileStatic prints:
> {code}
> Result: false
> {code}
> The behaviour of both should be he same IMO



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to