user123
user123

Reputation: 309

Can I use both @data and @builder annotations in one class Lombok?

The reason I want to use is because I want to generate setters in this format

For the class

public class Person {
    private String firstName;
    private String lastName;
}


 public Person setFirstName(String firstName) {
        this.firstName = firstName;
        return this;
    }

    public Person setLastName(String lastName) {
        this.lastName = lastName;
        return this;
    }

Instead of the @Data generated setters

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

Or is there any other way? Please let me know.

Upvotes: 1

Views: 551

Answers (1)

xxxception
xxxception

Reputation: 955

For it, you should use @Accessors(chain = true). For example:

@Accessors(chain = true)
@Setter
public class Person {
    private String firstName;
    private String lastName;
}

Vanilla java representation:

public class Person {
    private String firstName;
    private String lastName;

    public Person setFirstName(String firstName) {
        this.firstName = firstName;
        return this;
    }

    public Person setLastName(String lastName) {
        this.lastName = lastName;
        return this;
    }
}

Upvotes: 2

Related Questions