How to use EqualsAndHashCode.of with super class

4,753 views
Skip to first unread message

benmccann

unread,
May 17, 2012, 6:22:22 PM5/17/12
to project...@googlegroups.com
I've annotated my class with:
@Data @EqualsAndHashCode(callSuper = true, of = {"id"})

The id field is in the super class and the compiler is generating "warning: This field does not exist."  Is there a way to explicitly list fields from a super class?

Thanks,
Ben

Fabrizio Giudici

unread,
May 17, 2012, 6:27:21 PM5/17/12
to project...@googlegroups.com, benmccann
Why are you doing that? If id is parte of the superclass and you're
calling super.equals()/hashCode(), it should be managed by the methods in
the superclass.


--
Fabrizio Giudici - Java Architect, Project Manager
Tidalwave s.a.s. - "We make Java work. Everywhere."
fabrizio...@tidalwave.it
http://tidalwave.it - http://fabriziogiudici.it

Ben McCann

unread,
May 17, 2012, 6:31:43 PM5/17/12
to Fabrizio Giudici, project...@googlegroups.com
Hmm, I see.  I wanted to annotate my class with @Data, but it was then complaining "Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your type.".  This is why I was trying to add @EqualsAndHashCode.  I suppose I can instead add @ToString, @Getter, @Setter, and @RequiredArgsConstructor, but it's many more annotations to have to use.

Fabrizio Giudici

unread,
May 17, 2012, 6:42:57 PM5/17/12
to Ben McCann, project...@googlegroups.com
On Fri, 18 May 2012 00:31:43 +0200, Ben McCann <b...@benmccann.com> wrote:

> Hmm, I see. I wanted to annotate my class with @Data, but it was then
> complaining "Generating equals/hashCode implementation but without a call
> to superclass, even though this class does not extend java.lang.Object.
> If
> this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your
> type.". This is why I was trying to add @EqualsAndHashCode. I suppose I
> can instead add @ToString, @Getter, @Setter, and
> @RequiredArgsConstructor,
> but it's many more annotations to have to use.

Let's focus on equals/hashcode. The warning was telling you that you're
inheriting from a class that is not object and presumably has got status;
thus you must probe also fields from the superclass. So, callSuper=true is
correct if you have status in the superclass, and you should add
@EqualsAndHashCode in the superclass (or manually provide the required
methods).

Ben McCann

unread,
May 17, 2012, 6:48:23 PM5/17/12
to project...@googlegroups.com
I'm not sure what you mean by "status"

--
You received this message because you are subscribed to the Google
Groups group for http://projectlombok.org/

To post to this group, send email to project-lombok@googlegroups.com
To unsubscribe from this group, send email to
project-lombok+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/project-lombok?hl=en

Fabrizio Giudici

unread,
May 17, 2012, 6:52:05 PM5/17/12
to project...@googlegroups.com, Ben McCann
On Fri, 18 May 2012 00:48:23 +0200, Ben McCann <b...@benmccann.com> wrote:

> I'm not sure what you mean by "status"

Fields - such as your 'id', that if I understand correctly is in the
superclass.

Ben McCann

unread,
May 17, 2012, 7:11:13 PM5/17/12
to project...@googlegroups.com
Ah, I see.  Thank you very much for the help and quick responses!

-Ben


Reply all
Reply to author
Forward
0 new messages