JDK-8280168: Add Objects.toIdentityString#7139
JDK-8280168: Add Objects.toIdentityString#7139jddarcy wants to merge 9 commits intoopenjdk:masterfrom
Conversation
|
👋 Welcome back darcy! A progress list of the required criteria for merging this PR into |
Webrevs
|
| /** | ||
| * {@return a string equivalent to the string returned by {@code | ||
| * Object.toString} if that method is not overridden} | ||
| * |
There was a problem hiding this comment.
The original Object.toString() invokes the hashCode() method.
Perhaps the spec should indicate that it calls IdentityhashCode() instead of hashCode().
Though not incorrect, in Valhalla, Not all objects have identity and will need to different implementation.
Are there any use cases in the JDK that will use this method?
There was a problem hiding this comment.
I updated the return statement to say if hashCode isn't overridden either. The implSpec tag does explicitly mention identityHashCode already.
There are a few instances of this sort of code in the JDK src directory already; I may include a refactoring of at least one of them in a future revision of the PR. Thanks.
| } | ||
| Object o = new Object(){}; | ||
| errors += (Objects.toDefaultString(o).equals(o.toString()))? 0 : 1; | ||
| return errors; |
There was a problem hiding this comment.
Another potential test is on an instance of a class that overrides toString(), that would ensure that toDefaultString() doesn't invoke toString.
mlchung
left a comment
There was a problem hiding this comment.
Looks good. I reviewed the CSR but please update the CSR with the updated spec.
|
Expanding the scope of the proposed change, perhaps outside of a good cost/benefit ratio, there are now two new methods: toDefaultString(Object o) // uses o.hashCode The revision refactors two occurrences of the logic of toDefaultString with calls to the new method. The toIdentityString method has the sometimes helpful property that no overridden methods of the object are called in constructing the string. I'll revise the CSR once the final set of methods and their spec for this fix is determined. |
| assert(isObjectMethod(m)) : m; | ||
| return switch (m.getName()) { | ||
| case "toString" -> self.getClass().getName() + "@" + Integer.toHexString(self.hashCode()); | ||
| case "toString" -> java.util.Objects.toDefaultString(self); |
There was a problem hiding this comment.
It might be better if toString is changed to invoke toIdentityString(self), only because hashCode returns the identity hash code, it doesn't invoke hashCode().
There was a problem hiding this comment.
Yes; that looks like an inconsistency/bug. Updated in a subsequent push.
There was a problem hiding this comment.
Yes,it should call the one with identity hash code to avoid the confusion. The toString method of a Proxy instance should return the result of the default implementation of Object::toString method as specified.
|
I'm wondering if we want to have It may be that the current use cases in the JDK can be replaced with |
Updated the PR to just have toIdentityString and make corresponding changes to the CSR. |
|
toIdentityString is a better name than toDefaultString. It's fine for me but given that "identity" has a slightly different meaning in the context of Valhalla that in System.identityHashCode(), it may be good to ask Brian about that name. |
Modulo the method name, I have discussed with Brian adding a method with toIdentityString's semantics to Objects and he agreed the method was worth having. |
AlanBateman
left a comment
There was a problem hiding this comment.
Updated proposal and naming looks okay.
|
/integrate |
|
@jddarcy This pull request has not yet been marked as ready for integration. |
|
/integrate |
|
@jddarcy This pull request has not yet been marked as ready for integration. |
|
@jddarcy This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be: You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 25 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
|
/integrate |
|
Going to push as commit cbe8395.
Your commit was automatically rebased without conflicts. |
While it is strongly recommend to not use the default toString for a class, at times it is the least-bad alternative. When that alternative needs to be used, it would be helpful to have the implementation already available, such as in Objects.toDefaultString(). This method is analagous to System.identityHashCode.
Please also review the CSR: https://bugs.openjdk.java.net/browse/JDK-8280184
Progress
Issues
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/7139/head:pull/7139$ git checkout pull/7139Update a local copy of the PR:
$ git checkout pull/7139$ git pull https://git.openjdk.java.net/jdk pull/7139/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 7139View PR using the GUI difftool:
$ git pr show -t 7139Using diff file
Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/7139.diff