RFR: 8335637: Add explicit well-behaved expectations to Object.{toString, hashCode}
Pavel Rappo
prappo at openjdk.org
Mon Jul 8 09:58:36 UTC 2024
On Mon, 8 Jul 2024 09:33:54 GMT, Jaikiran Pai <jpai at openjdk.org> wrote:
>> Make well-behaved implementation expectations of Object.{toString, hashCode} explicit.
>
> src/java.base/share/classes/java/lang/Object.java line 101:
>
>> 99: * implementation should not use excessive memory or time for its
>> 100: * computations and should return a result for cyclic data
>> 101: * structures.
>
> Hello Joe, adding this text to set the expectations looks reasonable. However, I think the text "should return a result for cyclic data structures." feels a bit odd. If I understand correctly what it's trying to state is that for a class of the form:
>
>
> class Foo {
> Bar bar;
> Foo self;
>
> public void setSelf() {
> this.self = this;
> }
> }
>
> then:
>
>
> Foo foo = new Foo();
> foo.toString(); // or foo.hashCode()
>
> should be able to return the output from hashCode() and toString() even when an instance of `Foo` has a field which holds the same `Foo` instance. i.e. `toString()` and `hashCode()` should be able to handle re-entrancy and shouldn't consume excessive memory or time.
>
> If that understanding is correct, then maybe we could improve that text to state it differently instead of cyclic data structures? I checked the JDK repo to see if this term has previously been used but it hasn't. Maybe we should just state that the hashCode() toString() methods should be able to deal with re-entrancy?
My understanding is the same. Personally, the wording reads unambiguously. The words "circular" and "circularity" can be seen throughout codebase, in similar contexts.
FWIW, a simpler example can be constructed like this:
var list = new ArrayList<>();
list.add(list);
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/20063#discussion_r1668342145
More information about the core-libs-dev
mailing list