How can I generate both standard accessors and fluent accessors with lombok?
@Accessors(chain = true)
@Setter @Getter
public class Person {
private String firstName;
private String lastName;
private int height;
}
....
@Test
public void testAccessors() {
Person person = new Person();
person.setFirstName("Jack")
.setLastName("Bauer")
.setHeight(100);
assertEquals("Jack", person.getFirstName());
assertEquals("Bauer", person.getLastName());
assertEquals(100, person.getHeight());
}
I'm afraid you can't.
From the doc (emphasis is mine):
The
@Accessors
annotation is used to configure how lombok generates and looks for getters and setters.
So @Accessors
doesn't generate anything, it's just a way to configure @Getter
and @Setter
.
If you really want fluent and regular getter/setter, you can add (manually) the regular one and make them delegate to the fluent ones.
Unfortunately this is impossible. You need to implement own getters and setters, and add @Getter @Setter and @Accessors(fluent = true) annotaions to achieve this.
@Getter
@Setter
@Accessors(fluent = true)
public class SampleClass {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
In result you will have class like:
public class SampleClass {
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int id(){
return id;
}
public SampleClass id(int id){
this.id=id;
return this;
}
}