Mastering Lombok @Accessors: Fluent, Chain, and Prefix Explained

Learn how Lombok's @Accessors annotation works by exploring its source code and understanding the three key attributes—fluent, chain, and prefix—so you can control getter and setter generation, customize method naming, and enable method chaining in your Java classes.

Programmer DD
Programmer DD
Programmer DD
Mastering Lombok @Accessors: Fluent, Chain, and Prefix Explained

Preface

In your work you may encounter the annotation @Accessors(chain = true), which belongs to the Lombok library; this article explains its meaning.

1. @Accessors Source Code

Opening the source of @Accessors reveals two main points:

1) The annotation primarily influences how getter and setter methods are generated for fields, allowing related customizations.

2) When applied to a class, it affects all fields in that class; when applied to a specific field, it only affects that field.

The annotation defines three attributes: fluent , chain , and prefix . The following sections explain each.

2. @Accessors Attribute Description

2.1 fluent attribute

If omitted, the default is false. When set to true, the generated getter method does not have the get prefix and the setter method does not have the set prefix.

2.2 chain attribute

If omitted, the default is false. When set to true, the setter method returns the current object, enabling method chaining.

2.3 prefix attribute

This attribute is a string array. When it contains values, those prefixes are ignored during getter and setter generation.

For example, with fields xxName and yyAge, xx and yy are considered prefixes. The generated getter and setter methods will retain those prefixes.

If the prefixes are added to the @Accessors attribute, you can invoke the getter and setter methods as if the prefixes did not exist.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaannotationsLombokmethod chainingFluent API
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.