Skip to content
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

Make equals and hashCode generation deterministic (order of expressions) #1578

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

myhau
Copy link

@myhau myhau commented Dec 15, 2023

Description

Order of lines of code in the generated equals and hashCode methods was non-deterministic. This means that the generated files with code have changed between generations (even if nothing has changed in the schema itself).

After the change, the order of these lines will be the same as the order of fields in the schema.

To do

  • Integration tests.
    • I think I need some guidance here. @joelittlejohn, do you think this deserves to be tested? Are there any integration tests that inspect generated methods' code?

Example for hashCode

For a simple schema that includes two fields: field1 and field2, the generator randomly produced either

@Override
    public int hashCode() {
        int result = 1;
        result = ((result* 31)+((this.field1 == null)? 0 :this.field1.hashCode()));
        result = ((result* 31)+((this.field2 == null)? 0 :this.field2.hashCode()));
        return result;
    }

or

@Override
    public int hashCode() {
        int result = 1;
        result = ((result* 31)+((this.field2== null)? 0 :this.field2.hashCode()));
        result = ((result* 31)+((this.field1 == null)? 0 :this.field1.hashCode()));
        return result;
    }

Order of lines of code in equals and hashCode was non-deterministic.
This means that the generated files with code have changed between generations (even if nothing has changed in the schema itself).

After the change, the order of these lines will be the same as the order of fields in the schema.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant