Lombok @Data overrides existing toString and hashCode methods?

We recently started using the Lombok features in our project. we have the @Data annotation for the Domain object, which is why it is executed with some exception created by the hashCode() method provided by the Lombok api. Later, when I added @Setter and @Getter instead of @Data , I did not see any problems.

Question1: Lombok @Data overrides existing methods in a class like hashCode() and toString() ?

Question2: why does the hashCode() problem arise here?

+5
source share
1 answer

Yes, @Data implies @EqualsAndHashCode and @ToString . See @Data documentation .

The generated hashCode() method will call the hashCode methods for all fields. Therefore, if one of the field values โ€‹โ€‹throws an exception, then that is.

Another scenario is that you have references to round objects: if one object has a field containing an object that has a field related to the first object, calling the hashCode method will call StackOverflow.

Disclosure: I am one of the developers of Lombok.

+11
source

Source: https://habr.com/ru/post/1205758/


All Articles