ObjectMethods seems generating wrong methods for array-type field
Raffaello Giulietti
raffaello.giulietti at gmail.com
Wed Apr 14 09:38:40 UTC 2021
Hi Kengo,
I don't think this is a bug, as records are designed to "act as
transparent carriers for immutable data". A record having an array-typed
component is far from being immutable because everyone can change the
array contents. So you probably shouldn't opt for records for such use
cases, since you would loose the semantic benefits of records.
But if you need deeper hashing and comparison over array-typed
components of records, as you are probably expecting in your example,
you have to override the record hashCode() and equals() yourself. But
then (http://openjdk.java.net/jeps/395):
"Any explicit implementation of accessors or the equals or hashCode
methods should be careful to preserve the semantic invariants of the
record class."
Greetings
Raffaello
> Hello there,
>
>
> I'm Kengo TODA, an engineer learning about the Record feature.
> Today I found a nonintentional behavior, and it seems that the bug database
> has no info about it.
> Let me ask here to confirm it's by-design or not. If it's a bug, I want to
> try to send a patch to OpenJDK.
>
> Here is the code reproducing the nonintentional behavior:
> https://gist.github.com/KengoTODA/4d7ef6a5226d347ad9385241fee6bc63
>
> In short, the ObjectMethods class in OpenJDK v16 generates code
> that invokes the fields' method, even when the field is an array.
>
> Please help me to understand this behavior, or
> make an entry in the bug database to propose a patch.
> Thanks in advance! :)
>
> ***
> Kengo TODA
> skypencil at gmail.com
More information about the core-libs-dev
mailing list