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
configurationThe 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
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.
<?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>
lombok.config
fileWe create the lombok.config
file in the src/main/java
directory. The noIsPrefix
configuration is set to true
.
Main.java
filePerson
class with a boolean field called married
. The class is annotated with the data annotation.Person
class called personObject
.married
field using the getter, which is prefixed with get
.