Jason McD
Jason McD

Reputation: 577

Ignore property when generating equals and hashcode

Let's say I have a class Customer:

public class Customer {

private String firstName;
private String lastName;
private String doNotAddMeToEquals;

//Getters and Setters below

}

I'm using the Guava Eclipse Plugin in Eclipse to generate my equals() and hashCode() methods; however, I could just as well use the eclipse -> Source -> Generate HashCode / Equals. Either way...doesn't matter.

Is there a way to Annotate property doNotAddMeToEquals such that when I generate the equals & hashcode methods with the guava plugin that property doesn't show in the list?

Without altering the plugin or creating a template.

Thanks in Advance!!

Upvotes: 30

Views: 38976

Answers (2)

i-tms
i-tms

Reputation: 607

Using Lombok you can exclude properties from hashcode and equals like such as:

@EqualsAndHashCode(exclude = {"nameOfField"})

That would be in your case

@EqualsAndHashCode(exclude = {"doNotAddMeToEqualsAndHashCode"})

Upvotes: 23

aet
aet

Reputation: 7292

It sounds like what you want is something like this:

http://projectlombok.org/features/EqualsAndHashCode.html

It lets you use annotations to drive what properties are included in the equals and hashcode methods.

Upvotes: 23

Related Questions