By default, Lombok uses the get
prefix in order to generate the getters for fields of a class and the set
prefix in order to generate the setters for fields of a class. It uses the is
prefix to generate the getters for the boolean
fields of a class.
For example, consider the following Person
class.
class Person{private int age;private boolean married;}
Annotating the Person
class with the @Setter annotation will generate the following getters and setters.
class Person{
private int age;
private boolean married;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
}
The getters and setters of the fields are prefixed with the get
, set
, and is
prefix.
How can we remove the prefixes and use the field names to get and set the values?
Let’s find out.
lombok.accessors.fluent
configurationThe lombok.accessors.fluent
is a boolean configuration. When it’s set to true
, the generated getters and setters will not be prefixed with the bean-standard get
, is
, or set
prefixes. Instead, the methods will use the same name as the field name (minus the prefixes). This configuration can be used by specifying it as lombok.accessors.fluent = true
or lombok.accessors.fluent = false
in the lombok.config
file.
lombok.config
fileA lombok.config
is a file where different configuration directives are put together. It can be created in any directory. The configurations in the file apply to all source files in the same directory, where the file resides and to all of its child directories.
The configuration system is especially handy for Lombok features that are the same in the entire project, such as the name of our log variable. We can also use the configuration system to instruct Lombok to flag any use of a Lombok feature we don’t like as a warning or even an error.
The Person
class configured with the property lombok.accessors.fluent
set to true
works as follows:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>test</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>2.1</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>Main</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
pom.xml
The maven dependency for lombok
is included in the pom.xml
file.
lombok.config
We set the lombok.accessors.fluent
configuration to true
.
Main.java
Data
annotation.Person
class with age
and married
fields. We annotate this class with the Data
annotation.Person
class called person
.married
field using the married
setter method. Here, we add there no prefixes to the setter method.age
field using the age
setter method. Here, we add no prefixes to the setter method.age
field using the age()
getter method. Here, we add no prefixes to the getter method.married
field using the married()
getter method. Here, we add no prefixes to the getter method.