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 2019/04/18 20:05:00 UTC

[jira] [Comment Edited] (GROOVY-7996) Using with method with a closure that references a protected property produces ClassCastException

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

Eric Milles edited comment on GROOVY-7996 at 4/18/19 8:04 PM:
--------------------------------------------------------------

If written this way, no error occurs on Groovy 2.4:
{code}
class Foo {
  def propertyMissing(String name) {
    return 'stuff'
  }
  def build(@DelegatesTo(value=Foo, strategy=Closure.OWNER_FIRST) Closure block) {
    block.delegate = this
    return block.call()
  }
}

@groovy.transform.CompileStatic
class Bar {
  protected List bars = []
  boolean doStuff() {
    new Foo().build {
      return bars.isEmpty()
    }
  }
}
{code}
This should return {{true}} for {{new Bar().doStuff()}} since {{build}} now returns the value returned from the closure.


was (Author: emilles):
If written this way, no error occurs on Groovy 2.4:
{code:groovy}
class Foo {
  def propertyMissing(String name) {
    return 'stuff'
  }
  def build(@DelegatesTo(value=Foo, strategy=Closure.OWNER_FIRST) Closure block) {
    block.delegate = this
    return block()
  }
}

@groovy.transform.CompileStatic
class Bar {
  protected List bars = []
  boolean doStuff() {
    new Foo().build {
      return bars.isEmpty()
    }
  }
}
{code}

This should return {{true}} for {{new Bar().doStuff()}} since {{build}} now returns the value returned from the closure.

> 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
>            Assignee: Paul King
>            Priority: Major
>             Fix For: 2.5.6, 3.0.0-beta-1
>
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>
> 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
(v7.6.3#76005)