RFR: 8335637: Add explicit well-behaved expectations to Object.{toString, hashCode} [v2]

Joe Darcy darcy at openjdk.org
Mon Jul 8 19:15:36 UTC 2024


On Mon, 8 Jul 2024 19:11:43 GMT, Pavel Rappo <prappo at openjdk.org> wrote:

>>> While I understand and agree with the intent, we have several exceptions in the OpenJDK itself.
>>> 
>>> Circular structures that throw `StackOverflowError` on `hashCode()` and huge numbers that take 20-30 min to compute `toString()` are some examples.
>> 
>> I wouldn't be surprised if there are some cases like that in the JDK. However, if those types are generally usable outside of the JDK, I would advocate for the implementations being updated to behave better.
>> 
>> I added the cyclic/circular guidance in part based on work I did add suppressed exceptions to Throwable in JDK 7. One tricky part of the implementation was making sure the stack trace output did cycle detection since there were various ways exceptions could end up referring to each other.
>
>> One tricky part of the implementation was making sure the stack trace output did cycle detection since there were various ways exceptions could end up referring to each other.
> 
> Yes, the tricky part was to "Guard against malicious overrides of Throwable.equals".

> 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);
> ```

PS There are cases where cyclic structures are not supported, such as in java.util.Set:

" A special case of this prohibition is that it is not permissible for a set to contain itself as an element."

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/20063#discussion_r1669153919


More information about the core-libs-dev mailing list