Skip to content

GROOVY-12665: rehydrate and dehydrate the closure that a curried, writable, composed or trampolined closure wraps - #3186

Closed
codeconsole wants to merge 4 commits into
apache:masterfrom
codeconsole:curried-rehydrate-cce
Closed

codeconsole wants to merge 4 commits into
apache:masterfrom
codeconsole:curried-rehydrate-cce

Conversation

@codeconsole

Copy link
Copy Markdown
Contributor

rehydrate and dehydrate on a closure wrapper did not reach the closure being wrapped:

class O { def foo() { 'o' } }
class D { def foo() { 'd' } }
def c = { x, y -> foo() + x + y }
c.curry(1).rehydrate(new D(), new O(), new O())(2)
// ClassCastException: class O cannot be cast to class groovy.lang.Closure

Closure.rehydrate and Closure.dehydrate copy the closure and replace its owner, delegate and thisObject fields.

  • CurriedClosure and Closure.WritableClosure keep the wrapped closure in owner. Rehydrating replaced it with the new owner, and the next call failed with a ClassCastException. Dehydrating set it to null, and the next call failed with a NullPointerException.
  • ComposedClosure and TrampolineClosure call through their own fields. Rehydrating them changed nothing that is called, so methods still resolved against the original owner. Dehydrating them left that owner reachable, so the result could not be serialized, which is the reason dehydrate exists.

Seen in 4.0.33, 5.0.8 and 6.0.0 too.

Each of the four wrappers now overrides dehydrate() and rehydrate(Object, Object, Object). The override applies the operation to the wrapped closure (both closures for a composition) and wraps the copy again, so the result behaves like rehydrating the original closure and then currying, composing, trampolining or making it writable. There is one commit per wrapper. Memoized closures are left out: their cache needs a separate decision.

Tests

These fail without the change:

  • ClosureCurryTest.testRehydrateCurriedClosure and testDehydrateCurriedClosure
  • ClosureTest.testDehydrateAndRehydrateWritableClosure
  • ClosureComposeTest.testRehydrateComposedClosure and testDehydrateComposedClosure
  • TrampolineTest.testRehydrateTrampolinedClosure and testDehydrateTrampolinedClosure

They cover curry, rcurry, ncurry and nested curry, DELEGATE_FIRST, >> and <<, and serialize, deserialize and rehydrate round trips. The groovy.lang.*Closure*, groovy.*Closure*, org.codehaus.groovy.runtime.*Closure*, org.codehaus.groovy.runtime.trampoline.* and org.codehaus.groovy.runtime.memoize.* tests pass, as do ClosuresSTCTest, DelegatesToSTCTest, ClosuresStaticCompileTest and PackedClosuresTransformTest. The Checkstyle and CodeNarc gates pass.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 74.1533%. Comparing base (0941dba) to head (f47448d).
⚠️ Report is 11 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@              Coverage Diff              @@
##               master      #3186   +/-   ##
=============================================
  Coverage     74.1533%   74.1533%           
- Complexity      43751      43756    +5     
=============================================
  Files            1692       1692           
  Lines          149516     149524    +8     
  Branches        28366      28366           
=============================================
+ Hits           110871     110877    +6     
  Misses          28448      28448           
- Partials        10197      10199    +2     
Files with missing lines Coverage Δ
src/main/java/groovy/lang/Closure.java 85.2495% <100.0000%> (+0.2821%) ⬆️
src/main/java/groovy/lang/TrampolineClosure.java 84.2105% <100.0000%> (+1.8576%) ⬆️
...a/org/codehaus/groovy/runtime/ComposedClosure.java 91.6667% <100.0000%> (+14.3939%) ⬆️
...va/org/codehaus/groovy/runtime/CurriedClosure.java 91.3978% <100.0000%> (+0.1891%) ⬆️

... and 15 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@testlens-app

testlens-app Bot commented Nov 3, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: f47448d
▶️ Tests: 126069 executed
⚪️ Checks: 27/27 completed


Learn more about TestLens at testlens.app/docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants