Scott Murphy Heiberg created GROOVY-12671:
---------------------------------------------

             Summary: Closure property access throws NullPointerException 
instead of falling through to the delegate when the owner is null
                 Key: GROOVY-12671
                 URL: https://issues.apache.org/jira/browse/GROOVY-12671
             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 D { def bar = 'delegate' }
def c = { -> bar }.dehydrate()
c.delegate = new D()
assert c() == 'delegate'
{code}
*Actual:* {{java.lang.NullPointerException: Cannot get property 'bar' on null 
object}}. A write ({{bar = 1}}) throws {{Cannot set property 'bar' on null 
object}}, and so does {{rehydrate(d, null, null)}}.

*Expected:* with OWNER_FIRST (the default) a null owner is skipped and the 
delegate supplies the property, the same as for method calls (GROOVY-12667). 
When no non-null candidate remains (both null, or a null target under 
OWNER_ONLY or DELEGATE_ONLY), MissingPropertyException is thrown, matching the 
MissingMethodException that method calls give.

{{Closure.resolveGetProperty}} and {{resolveSetProperty}} skip a null receiver 
only for DELEGATE_FIRST with a null delegate (GROOVY-1974). Every other null 
receiver reaches {{NullObject}}. Separately, {{setPropertyTryThese}} tests {{o1 
!= null}} where it should test {{o2 != null}}, so a missing property on the 
owner with a null delegate also throws NPE instead of MissingPropertyException.

Verified on 2.4.15, 3.0.25, 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