How to remove prefixes from getters and setters in Lombok

Overview

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.

The lombok.accessors.fluent configuration

The 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.

The lombok.config file

A 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>

Explanation

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

  • Line 1: We import the Data annotation.
  • Lines 6–9: We define the Person class with age and married fields. We annotate this class with the Data annotation.
  • Line 12: We create an instance of the Person class called person.
  • Line 13: We set the value for the married field using the married setter method. Here, we add there no prefixes to the setter method.
  • Line 14: We set the value for the age field using the age setter method. Here, we add no prefixes to the setter method.
  • Line 15: We retrieve the value of the age field using the age() getter method. Here, we add no prefixes to the getter method.
  • Line 16: We retrieve the value of the married field using the married() getter method. Here, we add no prefixes to the getter method.

Free Resources