-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[FEATURE] @EqualsAndHashCode for records #3246
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
Comments
I understand the intent of the request to have Lombok generate the equals and hashcode via annotations but it is not accurate to say there is no way to customize the equals and hashcode method for record classes. You can definitely overridde them and implement your own logic public record MyEntity(UUID id, String name) {
@Override
public boolean equals(Object o) {
return o instanceof MyEntity other &&
Objects.equals(this.id, other.id);
}
@Override
public int hashCode() {
return Objects.hashCode(this.id);
}
} |
Well, yes, but that's true also for classes, which are supported by |
Agreed. I was only responding to your comment about record classes:
Not disputing the utility of the feature request itself. |
Updated the description: |
There is another reason for a |
Exactly that. After an update from Sonar 8 LTS to Sonar 9 LTS I had some new findings about https://rules.sonarsource.com/java/RSPEC-6218 and was expecting to be able to use The same goes for |
One other use for that is oracle/graal#4348 ... Where in native image right now at least records own implementation lacks performance since they are doing reflection... I know that they will be improving it in a new GraalVM release, but a "handwritten" method ( |
Another place where it would be good to have the support of the annotations is the exclude. e.g. when writing a test, using mockito to verify the content of an instance that is called on a specific method, where a timestamp is included, then the equals.exclude would be very useful. |
@ukv001 For that you don't normally want to override equals (modifying production code for the sake of the tests), you can use assertJ and, to make sure you really compare the fields and you don't rely on the implemented equals, use |
Testing convenience may or may not be a proper usage for this, but the main point of requesting IMHO, though, when supporting the Include/Exclude, the Lombok implementation should be careful, and NOT to change anything (at least semantically) from the implementation that come with Java record. e.g. if the default Java record equals() compares array fields with |
Uh oh!
There was an error while loading. Please reload this page.
Describe the feature
Yes, I know -
equals()
andhashCode()
are automatically generated for records, but there's no easy way to customise them. Sometimes you don't want to compare all object properties, e.g. in case of DB entities it's usually enough to implement equals/hashCode using only the ID. In case of classes, it's very easy to customise it with Lombok:Unfortunately, I can't do the same with records - they are not supported by Lombok's
@EqualsAndHashCode
annotation. This piece of code fails to compile withjava: @EqualsAndHashCode is only supported on a class.
error:I think it would be great to have this annotation also for records, and throw the compilation error only if
@EqualsAndHashCode.Include/Exclude
is not used in the code.Describe the target audience
Anyone who migrates POJO classes to Java records, but has specific requirements regarding
equals()
andhashCode()
methods.The text was updated successfully, but these errors were encountered: