Scott Murphy Heiberg created GROOVY-12669:
---------------------------------------------

             Summary: Rehydrating or dehydrating a memoized closure keeps 
calling the original closure with its old owner and delegate
                 Key: GROOVY-12669
                 URL: https://issues.apache.org/jira/browse/GROOVY-12669
             Project: Groovy
          Issue Type: Bug
    Affects Versions: 5.0.8, 4.0.33, 6.0.0, 7.0.0-alpha-1
            Reporter: Scott Murphy Heiberg


{code:groovy}
class O { def foo() { 'o' } }
class P { def foo() { 'p' } }
def c = new O().with { o -> { x -> foo() + x } }
assert c.rehydrate(null, new P(), new P()).memoize()(1) == 'p1'
println c.memoize().rehydrate(null, new P(), new P())(1)        // prints o1
println c.memoizeAtMost(5).rehydrate(null, new P(), new P())(1) // prints o1
new ObjectOutputStream(new 
ByteArrayOutputStream()).writeObject(c.memoize().dehydrate())
{code}
*Actual:* a rehydrated copy of {{memoize()}}, {{memoizeAtMost}}, 
{{memoizeAtLeast}} or {{memoizeBetween}} prints {{o1}}. Serializing the 
dehydrated memoized closure fails with {{java.io.NotSerializableException: O}}.

*Expected:* {{p1}}, as for rehydrate followed by memoize. The dehydrated 
memoized closure serializes.

Cause: the memoized closure forwards calls to the closure it wraps, held in a 
separate field. The inherited {{rehydrate}} and {{dehydrate}} replace only the 
wrapper's own owner, delegate and this object, so the wrapped closure keeps the 
old ones. The copy also shares the original's cache. Related: GROOVY-12665 (the 
same for curried, writable, composed and trampolined closures).

Verified on 4.0.33, 5.0.8, 6.0.0 and master.



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

Reply via email to