GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid calling `actual.hashCode()` and `expected.hashCode()` in `DualValue` by davidboden · Pull Request #3340 · assertj/assertj

Conversation

@davidboden

@davidboden davidboden commented Jan 19, 2024

Copy link
Copy Markdown

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.

Check List:

  • Fixes #??? (ignore if not applicable)
  • Unit tests : YES
  • Javadoc with a code example (on API only) : NA
  • PR meets the contributing guidelines

Following the contributing guidelines will make it easier for us to review and accept your PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
joel-costigliola
joel-costigliola reviewed Jan 19, 2024
View reviewed changes
Comment thread ...re/src/test/java/org/assertj/core/api/recursive/comparison/DualValue_mutatedValues_Test.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread assertj-core/src/main/java/org/assertj/core/api/recursive/comparison/DualValue.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@joel-costigliola

joel-costigliola commented Jan 19, 2024

Copy link
Copy Markdown
Member

thanks for catching this up @davidboden, I commented the PR.

davidboden reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@davidboden

davidboden commented Jan 19, 2024

Copy link
Copy Markdown
Author

Thanks. All sounds good; I'll fix it up over the weekend.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

David Boden
DualValue avoid calling actual.hashCode() and expected.hashCode()
e56e475
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`.
@joel-costigliola

joel-costigliola commented Jan 21, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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 equals because:

  1. equals compares actual and expected fields reference
  2. we compute the hash code in the constructor
  3. as actual and expected are final, their references never change, thus even if their inner actual and expected are mutated the equals behavior stays the same and thus is still consistent with the computed hash code at construction time.

If we compute the hash code every time hashCode is called, then my last point does not hold as your test showed but your test succeeds if you compute the hash code only once at construction time.

Having said that, I think this PR is still relevant because the hash code computed based on actual and expected reference/identity is consistent with the equals implementation.

I'm happy to be proven wrong, so don't hesitate if you find a test case invalidating my reasoning.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@joel-costigliola

joel-costigliola commented Jan 21, 2024

Copy link
Copy Markdown
Member

I'm taking over the PR, but you will be the commit author so full credit to you @davidboden

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@PBoddington PBoddington mentioned this pull request Jan 22, 2024
satisfiesExactlyInAnyOrder fails if actual overrides equals #3339
Closed
@joel-costigliola joel-costigliola closed this in e9ebcc2 Jan 23, 2024
@joel-costigliola

joel-costigliola commented Jan 23, 2024

Copy link
Copy Markdown
Member

Thanks @davidboden for your work, this is integrated..

davidboden, PBoddington, danhamilt1, yestinj, edjeffreys, samc13, and SulphurDioxide reacted with hooray emoji
All reactions
  • 🎉 7 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@scordio scordio added the theme: recursive comparison An issue related to the recursive comparison label Jan 24, 2024
@scordio scordio added this to the 3.25.2 milestone Jan 24, 2024
@scordio scordio changed the title DualValue avoid calling actual.hashCode() and expected.hashCode() Avoid calling actual.hashCode() and expected.hashCode() in DualValue Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@joel-costigliola joel-costigliola joel-costigliola left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

theme: recursive comparison An issue related to the recursive comparison

Projects

None yet

Milestone

3.25.2

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@davidboden @joel-costigliola @scordio
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.