The @Accessors(chain = true)
annotation is provided by Project Lombok to customize the generation of getter and setter methods for fields in a Java class. When you set chain = true
, Lombok generates setter methods that return this
(the current object) instead of void
. This allows for a fluent API style, enabling method chaining.
Usage Example
Here is a simple example to demonstrate how @Accessors(chain = true)
works in conjunction with Lombok's @Getter
and @Setter
annotations:
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
@Getter
@Setter
@Accessors(chain = true)
public class User {
private int id;
private String name;
private String email;
}
public class Main {
public static void main(String[] args) {
User user = new User()
.setId(1)
.setName("John Doe")
.setEmail("john.doe@example.com");
System.out.println(user);
}
}
Explanation
-
Annotations:
@Getter
: Generates getter methods for all fields.@Setter
: Generates setter methods for all fields.@Accessors(chain = true)
: Modifies the generated setter methods to returnthis
instead ofvoid
.
-
Method Chaining:
- The
setId()
,setName()
, andsetEmail()
methods return the currentUser
object, allowing for chaining of setter method calls. - This results in more readable and concise code when setting multiple properties.
- The
Generated Code
With @Accessors(chain = true)
, Lombok generates the following setter methods:
public User setId(int id) {
this.id = id;
return this;
}
public User setName(String name) {
this.name = name;
return this;
}
public User setEmail(String email) {
this.email = email;
return this;
}
Additional Options
The @Accessors
annotation has other parameters you can use to customize the generated methods:
- fluent: If set to
true
, Lombok generates getter and setter methods without theget
andset
prefixes. Default isfalse
. - prefix: A list of strings to be removed from the field name when generating getter and setter methods.
Example with fluent
and prefix
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
@Getter
@Setter
@Accessors(chain = true, fluent = true, prefix = "m")
public class User {
private int mId;
private String mName;
private String mEmail;
}
public class Main {
public static void main(String[] args) {
User user = new User()
.id(1)
.name("John Doe")
.email("john.doe@example.com");
System.out.println(user);
}
}
In this example:
- fluent = true: The generated getter and setter methods do not have the
get
andset
prefixes. For example,setId(int id)
becomesid(int id)
. - prefix = "m": The generated methods strip the prefix
m
from the field names. So,mId
becomesid
.
Summary
The @Accessors(chain = true)
annotation from Lombok allows for method chaining by modifying setter methods to return the current object instead of void
. This leads to a more fluent and readable API, especially when setting multiple properties. Additional customization options like fluent
and prefix
provide further control over the generated methods. This helps in writing clean and concise code while reducing boilerplate.