Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator
lombok
configuration

What is the "noIsPrefix" configuration in Lombok?

abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Overview

Getter methods are the methods that return the value of a field in the class.

The default naming of the getter methods for non-boolean fields is get<field_name>, that is, the field name follows the get word or the field name is prefixed with the get word.

For example, consider that we have a class called Person that has a long attribute called salary. The Getter method signature for this field would be as follows:

public long getSalary()

The default naming of the getter methods for boolean fields is is<field_name>, that is, the field name follows the is word or the field name is prefixed with the is word.

For example, consider that we have a class called Person that has a boolean attribute called married. The Getter method signature for this field would be as follows:

public boolean isMarried()

What if we want the getters of boolean fields to follow the get naming convention of non-boolean fields in Lombok?

We can use the noIsPrefix configuration to achieve this.

noIsPrefix configuration

The noIsPrefix configuration is a Getter configuration. If it’s set to true, the getters generated for boolean fields will use the get prefix instead of the default is prefix, and any generated code that calls getters, such as @ToString, will also use get instead of is. This configuration can be used by specifying it as lombok.getter.noIsPrefix = true or lombok.getter.noIsPrefix = false in the lombok.config file.

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.

Code

<?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>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.22</version>
            <scope>provided</scope>
        </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>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>9</source>
                    <target>9</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

Explanation

The lombok.config file

We create the lombok.config file in the src/main/java directory. The noIsPrefix configuration is set to true.

The Main.java file

  • Line 1: We import the @Data annotation.
  • Lines 5 to 7: We define a Person class with a boolean field called married. The class is annotated with the data annotation.
  • Line 11: We create an instance of the Person class called personObject.
  • Line 12: We retrieve the value of the married field using the getter, which is prefixed with get.

RELATED TAGS

java
communitycreator
lombok
configuration

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring