Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better way to only use callsuper in @EqualsAndHashCode and @ToString?

I have class where I inheritage some fields, among them my id fields and those I difference on for the object. I am using lombok to generate my @EqualsAndHashCode and @ToString, but only want to use my super class's version, and exclude all fields in my class. I am wondering if there is a way to do that, without using the exclude and then add all fields.

My classes:

@EqualsAndHashCode(of = { "id" })
@ToString(of = { "id", "email", "name" })
@Data
public abstract class Resource {
    @Id
    private Integer id;

    private String name;

    private String email;
}

@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@Data
public class Employee extends Resource {
    private Organisation organisation;

    private List<Skill> skills = new ArrayList<Skill>();
}

Here I will get organisation and skills in my ToString, Equals and HashCode.

To exclude them I can use:

@ToString(callSuper = true, exclude = { "organisation", "skills" })

However as I have more fields than the example, and there might come more in the future, I would prefer if I could exclude all except for the ones from my super class.

I can also add one field in with the of = {}.

Is there any better way to accomplish that?

like image 806
Larvalis Avatar asked Jul 27 '17 13:07

Larvalis


People also ask

Does @data generate equals and hashCode?

The annotation @Data with inheritance produces the next warning: Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java. lang.

What is@ EqualsAndHashCode annotation?

The @EqualsAndHashCode annotation instructs the compiler to execute an AST transformation which adds the necessary equals and hashCode methods to the class. The hashCode() method is calculated using Groovy's HashCodeHelper class which implements an algorithm similar to the one outlined in the book Effective Java.

What is EqualsAndHashCode exclude?

The @EqualsAndHashCode.Exclude annotation marks a field so that Lombok doesn't use that field when generating equals and hashCode: @EqualsAndHashCode public class Employee { private String name; @EqualsAndHashCode.

What is Lombok EqualsAndHashCode?

equalsAndHashCode. callSuper config key. NEW in Lombok 0.10: Unless your class is final and extends java. lang. Object , lombok generates a canEqual method which means JPA proxies can still be equal to their base class, but subclasses that add new state don't break the equals contract.


1 Answers

Wouldn't simply

@ToString(callSuper = true, of = {})

work? Lombok knows nothing about the superclass fields (as this information is unavailable at the time it runs) and you can't include id or exclude name. All you can do is to make it call super.toString(). When you include no fields at all, then you get something like

Employee(super=Resource(43, Larvalis, [email protected]))

which may or mayn't be what you want. You could instead write

public String toString() {
    return getClass().getSimpleName()
        + super.toString().replaceFirst("^[^(]+", "");
}

so you'd get just

Employee(43, Larvalis, [email protected])

Update:

The of parameter is obsolete in the meantime, see onlyExplicitlyIncluded in @Datz's answer.

like image 153
maaartinus Avatar answered Sep 27 '22 19:09

maaartinus