简体   繁体   中英

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!!

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.

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"})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM