New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JDK-8280168: Add Objects.toIdentityString #7139
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d05c6dd
JDK-8280168 Add Objects.toDefaultString
jddarcy 6ea4de5
Respond to review feedback.
jddarcy a854766
Respond to review feedback to augment test.
jddarcy 48b596e
Add toIdentityString
jddarcy 0f6f078
Appease jcheck.
jddarcy 0a4d8a4
Merge branch 'master' into JDK-8280168
jddarcy b6365d7
Merge branch 'master' into JDK-8280168
jddarcy b7ef301
Respond to review feedback.
jddarcy 2cf7a54
Update copyright, remove author tag.
jddarcy File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved. | ||
* Copyright (c) 2009, 2022, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
|
@@ -23,9 +23,8 @@ | |
|
||
/* | ||
* @test | ||
* @bug 6797535 6889858 6891113 8013712 8011800 8014365 | ||
* @bug 6797535 6889858 6891113 8013712 8011800 8014365 8280168 | ||
* @summary Basic tests for methods in java.util.Objects | ||
* @author Joseph D. Darcy | ||
*/ | ||
|
||
import java.util.*; | ||
|
@@ -40,6 +39,7 @@ public static void main(String... args) { | |
errors += testHash(); | ||
errors += testToString(); | ||
errors += testToString2(); | ||
errors += testToIdentityString(); | ||
errors += testCompare(); | ||
errors += testRequireNonNull(); | ||
errors += testIsNull(); | ||
|
@@ -134,6 +134,37 @@ private static int testToString2() { | |
return errors; | ||
} | ||
|
||
private static int testToIdentityString() { | ||
int errors = 0; | ||
// Test null behavior | ||
try { | ||
Objects.toIdentityString(null); | ||
errors++; | ||
} catch (NullPointerException npe) { | ||
; // Expected | ||
} | ||
// Behavior on typical objects | ||
Object o = new Object(){}; | ||
errors += (Objects.toIdentityString(o).equals(o.toString()))? 0 : 1; | ||
// Verify object's toString *not* called | ||
Object badToString = new Object() { | ||
@Override | ||
public String toString() { | ||
throw new RuntimeException(); | ||
} | ||
}; | ||
Objects.toIdentityString(badToString); | ||
// Verify object's hashCode *not* called | ||
Object badHashCode = new Object() { | ||
@Override | ||
public int hashCode() { | ||
throw new RuntimeException("0xDEADBEFF"); | ||
} | ||
}; | ||
Objects.toIdentityString(badHashCode); | ||
return errors; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another potential test is on an instance of a class that overrides toString(), that would ensure that toDefaultString() doesn't invoke toString. |
||
} | ||
|
||
private static int testCompare() { | ||
int errors = 0; | ||
String[] values = {"e. e. cummings", "zzz"}; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.