[
https://issues.apache.org/jira/browse/GROOVY-10635?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17541439#comment-17541439
]
Paul King commented on GROOVY-10635:
------------------------------------
[~blackdrag] Our logic for finding Class:instanceMethod type method references
(for better or worse) currently occurs after finding properties. This isn't
normally a problem with JavaBean naming conventions. For records, the private
backing field is being erroneously found before the same-named accessor method
and attempts to set it throw the exception and the method reference logic is
never reached. It would certainly be nicer to re-order that logic and avoid the
whole "flow-control via exception" approach. Re-ordering that logic however
would require a fair bit of work and might alter some other edge cases. I
figured getting the correct behavior for now was a good first step and if we
can refactor later to re-order, all the better.
> Method references not working for record components in dynamic code
> -------------------------------------------------------------------
>
> Key: GROOVY-10635
> URL: https://issues.apache.org/jira/browse/GROOVY-10635
> Project: Groovy
> Issue Type: Bug
> Reporter: Paul King
> Assignee: Paul King
> Priority: Major
> Fix For: 5.0.0-alpha-1, 4.0.3
>
>
> The following example currently fails.
> {code}
> record Bar(String name) { }
> def bars = [new Bar(name: 'A'), new Bar(name: 'B')]
> assert bars.stream().map(Bar::name).toList() == ['A', 'B']
> {code}
> The error message is:
> {noformat}
> java.lang.IllegalArgumentException: Can not set final java.lang.String field
> Bar.name to java.lang.Class
> {noformat}
> This is because the metaclass is picking the private field as a property
> ahead of the same-named accessor method.
> Workaround is to use @CompileStatic.
--
This message was sent by Atlassian Jira
(v8.20.7#820007)