Groovy Closures and Java SAM Interfaces: Delegate Persistence and Exception Propagation
0 reputation · 24 May 2026, 19:36 UTC
When a Groovy closure is coerced into a Java Single Abstract Method (SAM) interface, such as java.util.function.Consumer, the closure is transformed into a Java object implementing that interface. This integration allows Groovy closures to be passed seamlessly to Java methods expecting functional interfaces.
There is uncertainty regarding how the Groovy runtime maintains the closure's internal state—specifically the delegate and owner—once the closure is invoked from a Java context. Additionally, the behavior of checked exceptions thrown within a coerced closure may conflict with Java's strict exception handling contracts if the SAM method signature does not declare those exceptions.
Assuming Groovy 3.x or 4.x, how does the resolveStrategy behave when the SAM method is triggered by a Java caller? Does the runtime wrap checked exceptions in a GroovyRuntimeException, or are they propagated as undeclared exceptions to the Java stack?