-
-
Notifications
You must be signed in to change notification settings - Fork 683
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
Avoid calling actual.hashCode()
and expected.hashCode()
in DualValue
#3340
Conversation
...re/src/test/java/org/assertj/core/api/recursive/comparison/DualValue_mutatedValues_Test.java
Outdated
Show resolved
Hide resolved
assertj-core/src/main/java/org/assertj/core/api/recursive/comparison/DualValue.java
Outdated
Show resolved
Hide resolved
thanks for catching this up @davidboden, I commented the PR. |
Thanks. All sounds good; I'll fix it up over the weekend. |
The hashCode of `DualValue` needs to match the object references of actual and expected, because that's what `equals` uses. The current implementation caches the hashCode of both objects at construction, but this doesn't really match the requirement exactly. The motivation for changing this is around avoiding calling hashCode unnecessarily on actual and expected objects, but I think it fixes potential bugs too. You could: * Create a DualValue. * Mutate a value in actual or expected that's present in the object's hashCode method. * Create another DualValue. Both of the DualValues would be equal to each other according to equals() but would have different hashcodes. We can continue to cache the hashCode if you prefer, but better to generate it based on the object references using `System.identityHashCode`.
I took a deeper look at the code and if we compute the hash code at construction time, I don't see a way that it could inconsistent with
If we compute the hash code every time Having said that, I think this PR is still relevant because the hash code computed based on I'm happy to be proven wrong, so don't hesitate if you find a test case invalidating my reasoning. |
I'm taking over the PR, but you will be the commit author so full credit to you @davidboden |
Thanks @davidboden for your work, this is integrated.. |
actual.hashCode()
and expected.hashCode()
in DualValue
The hashCode of
DualValue
needs to match the object references of actual and expected, because that's whatequals
uses.The current implementation caches the hashCode of both objects at construction, but this doesn't really match the requirement exactly.
The motivation for changing this is around avoiding calling hashCode unnecessarily on actual and expected objects, but I think it fixes potential bugs too. You could:
Both of the DualValues would be equal to each other according to equals() but would have different hashcodes.
We can continue to cache the hashCode if you prefer, but better to generate it based on the object references using
System.identityHashCode
.Check List:
Following the contributing guidelines will make it easier for us to review and accept your PR.