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 2021/05/10 15:23:00 UTC
[jira] [Resolved] (GROOVY-2641) Dynamically created closure handles
delegate differently from statically created closure
[ https://issues.apache.org/jira/browse/GROOVY-2641?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]
Eric Milles resolved GROOVY-2641.
---------------------------------
Resolution: Information Provided
The default resolve strategy of Closure is OWNER_FIRST. The dynamicClosure case is displaying this behavior. When adding the following before calling dynamicClosure, the expected behavior is present:
{code:groovy}
dynamicClosure.setResolveStrategy(Closure.DELEGATE_FIRST)
{code}
> Dynamically created closure handles delegate differently from statically created closure
> ----------------------------------------------------------------------------------------
>
> Key: GROOVY-2641
> URL: https://issues.apache.org/jira/browse/GROOVY-2641
> Project: Groovy
> Issue Type: Sub-task
> Components: groovy-runtime
> Affects Versions: 1.5.4
> Environment: CentOS 5 n Intel with java 1.6.0_03-b05
> Reporter: Bruce Cota
> Priority: Major
>
> The following script executes two closures. One is statically defined in class Test,
> the other is returned by Test.makeClosure(). In each case the delegate of the
> Closure is set to be an instance of class Delegate, but in one case a call to
> method "foo()" appears to resolve to Test.foo(), while in the second it appears
> to resolve to Delegate.foo().
> I showed this to Dierk at the G2 conference in Reston, and he asked me to post
> it as a bug.
> =====================================================================
> class Test {
> def foo() {
> "NOT the delegate"
> }
> def makeClosure() {
> def closure = {
> println foo()
> println delegate.foo()
> }
> }
> static staticClosure = {
> println foo()
> println delegate.foo()
> }
> }
> class Delegate {
> def foo() {
> "delegate"
> }
> }
> Closure dynamicClosure = new Test().makeClosure()
> dynamicClosure.setDelegate(new Delegate())
> dynamicClosure.call()
> Test.staticClosure.setDelegate(new Delegate())
> Test.staticClosure.call()
> ==================================================================
> Expected Output:
> delegate
> delegate
> delegate
> delegate
> Actual Output:
> NOT the delegate
> delegate
> delegate
> delegate
--
This message was sent by Atlassian Jira
(v8.3.4#803005)