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

Paul King resolved GROOVY-12830.
--------------------------------
    Fix Version/s: 7.0.0-alpha-1
       Resolution: Fixed

> SC: a serializable lambda inside a closure cannot use the closure's delegate
> ----------------------------------------------------------------------------
>
>                 Key: GROOVY-12830
>                 URL: https://issues.apache.org/jira/browse/GROOVY-12830
>             Project: Groovy
>          Issue Type: Bug
>            Reporter: Paul King
>            Assignee: Paul King
>            Priority: Major
>             Fix For: 7.0.0-alpha-1
>
>
> h3. Description
> Under {{@CompileStatic}}, an implicit call or property access in a lambda can 
> resolve to the delegate of a closure around the lambda. The code compiles, 
> but the name fails with a {{ClassCastException}} when evaluated, because the 
> owner of a native lambda is the enclosing instance and not the closure.
> GROOVY-12827 fixes this for a lambda that is not serializable, in 6.0.1: such 
> a lambda captures the closure around it. A serializable lambda was left as it 
> was there, so that its serialized form does not change in a patch release. 
> This issue covers the serializable case.
> {code:groovy}
> import groovy.transform.CompileStatic
> import java.util.function.Supplier
> interface SerializableSupplier<T> extends Supplier<T>, Serializable {}
> class D implements Serializable {
>     String name = 'delegate'
>     String greet() { 'hello' }
> }
> @CompileStatic
> class C implements Serializable {
>     static <T> T with(D d, @DelegatesTo(value = D, strategy = 
> Closure.DELEGATE_FIRST) Closure<T> c) {
>         c.resolveStrategy = Closure.DELEGATE_FIRST
>         c.delegate = d
>         c.call()
>     }
>     def plain() {
>         with(new D()) { -> Supplier<String> s = () -> greet() + ' ' + name; 
> s.get() }
>     }
>     def serializable() {
>         with(new D()) { -> SerializableSupplier<String> s = () -> greet() + ' 
> ' + name; s.get() }
>     }
> }
> def c = new C()
> assert c.plain() == 'hello delegate'
> assert c.serializable() == 'hello delegate'   // ClassCastException: class C 
> cannot be cast to class groovy.lang.Closure
> {code}
> ||Build||{{plain()}}||{{serializable()}}||
> |6.0.0|ClassCastException|ClassCastException|
> |6.0.1 with GROOVY-12827|hello delegate|ClassCastException|
> |Expected|hello delegate|hello delegate|
> h3. Expected
> A serializable lambda that resolves a name through the closure around it 
> captures that closure, and the name resolves as it would directly in the 
> closure, with the closure's resolve strategy and its delegate at the time of 
> the call. The lambda's own {{owner}}, {{delegate}} and {{thisObject}} remain 
> the enclosing instance.
> h3. Consequences to document
> These apply only to a serializable lambda containing such a name, which could 
> not evaluate it before:
> * Serializing the lambda also serializes the closure, including its delegate 
> and the values it holds. If one of them is not serializable, writing the 
> lambda throws {{NotSerializableException}}, even when the name is on a branch 
> that never runs. Dynamic Groovy behaves the same way.
> * The lambda is generated as a {{java.lang.reflect.Proxy}} over a 
> {{ConvertedClosure}}, unless its functional interface is or implements a 
> trait. A {{SerializedLambda}} is replaced only after its captured values are 
> read, so a delegate that refers back to the lambda would otherwise receive 
> the {{SerializedLambda}}.
> * An undeclared checked exception thrown from such a lambda reaches the 
> caller as {{UndeclaredThrowableException}}, as in dynamic Groovy.
> h3. Unchanged
> Non-serializable lambdas, and serializable lambdas that do not resolve a name 
> through the closure around them, keep their serialized form and behaviour.
> h3. Notes
> * Split from GROOVY-12827, whose 6.0.1 fix covers non-serializable lambdas 
> only.
> * Not suitable for 6.0.x: it changes the serialized form and the failure 
> behaviour of the affected lambdas.
> * Needs a COMPATIBILITY.md entry under Groovy 7.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to