[
https://issues.apache.org/jira/browse/GROOVY-9988?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17303378#comment-17303378
]
Paul King commented on GROOVY-9988:
-----------------------------------
This looks like intended behavior. For references to foo from inside the class,
the field gets preference - you could obviously write getFoo().length() in the
run method if you needed. External to the class the getter gets preference:
{code}
def t = new Test()
println t.foo // => Hello
t.run() // => 5
{code}
> Field is given precedence over getter
> -------------------------------------
>
> Key: GROOVY-9988
> URL: https://issues.apache.org/jira/browse/GROOVY-9988
> Project: Groovy
> Issue Type: Bug
> Reporter: Benedikt Ritter
> Priority: Major
> Attachments: test.groovy
>
>
> If a method references a property and there is a field and a getter for that
> field, the field is given precedence over the getter. This is problematic if
> the getter does some lazy initialization.
> Executing the attached script on Groovy 3.0.7 results in the following error:
> {code:java}
> ❯ groovy test.groovy
> Caught: java.lang.NullPointerException: Cannot invoke method length() on null
> object
> java.lang.NullPointerException: Cannot invoke method length() on null object
> at Test.run(test.groovy:12)
> at Test$run.call(Unknown Source)
> at test.run(test.groovy:16)
> at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> at
> java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
> at
> java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> {code}
> As a work around the code in run() can explicitly call the getter.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)