@EqualsAndHashCode for a Subclass that inherits all identifying Attributes from its Superclass

1,403 views
Skip to first unread message

gigi.com

unread,
Oct 3, 2017, 1:03:05 PM10/3/17
to Project Lombok

Hello,


I'm currently struggeling on how to comfortably define @EqualsAndHashCode for a subclass that inherits its id attribute from its superclass:

Example:

@EqualsAndHashCode(of = "id", callSuper = false)
public class Site {
    private Integer id;
}


@EqualsAndHashCode(exclude = { "name" }, callSuper = true)
public class SubSite extends Site {
    private String name;
}

The superclass "Site" has an attribute that identifies it, namely "id". The subclass "SubSite" doesn't have any identifying attribute, but it ihnerits it from its superclass.

How can I now define the @EqualsAndHashCode annotation in the subclass in a comfortable way?

I tried the following out:

1. @EqualsAndHashCode(of = "id", callSuper = true)
-> This gives me a compiler warning in Eclipse ("This field does not exist, or would have been excluded anyway."), which I want to avoid. It is the same warning as I would write of = "dummy" or so.

2. @EqualsAndHashCode(exclude = { "name" }, callSuper = true)
-> This is dangerous when the next developer adds an additional attribute, e.g. "street", and forgets to adapt the exclude list.

3. add a dummy attribute to the subclass, and all other subclasses, which are many
-> No professional solution

Does anyone have another idea or is there missing a feature in Lombok?

I would wish to be able to define also super class attributes in the of-statemant in the case where callSuper is equals to true. Would this be a solution for future Lombok versions?

Ciao, Michael

Martin Grajcar

unread,
Oct 3, 2017, 1:34:45 PM10/3/17
to project...@googlegroups.com
See my answer on https://stackoverflow.com/a/45395547/581205. In short of={} is the solution.

--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombok+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages