1. Introduction
Project Lombok is a popular library for reducing Java boilerplate.
In this tutorial, we’ll take a look at how Lombok’s @Getter annotation works on boolean fields to remove the need to create its corresponding getter methods.
2. Maven Dependency
Let’s start by adding Project Lombok to our pom.xml:
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.2</version> </dependency>
3. Using @Getter on a boolean Field
Let’s say that we want Lombok to generate an accessor method for our private boolean field.
We can annotate that field with @Getter:
@Getter private boolean running;
And Lombok will use its annotation processor to generate an isRunning() method in the class.
And now, we can refer to it, even though we haven’t written the method ourselves:
@Test public void whenBasicBooleanField_thenMethodNamePrefixedWithIsFollowedByFieldName() { LombokExamples lombokExamples = new LombokExamples(); assertFalse(lombokExamples.isRunning()); }
3.1. A boolean Field Having the Same Name With Its Accessor
Let’s add another line of code to make the example a little bit tricky:
@Getter private boolean isRunning = true;
It’d be a bit cumbersome if Lombok created a method called isIsRunning.
Instead, Lombok creates isRunning like before:
@Test public void whenBooleanFieldPrefixedWithIs_thenMethodNameIsSameAsFieldName() { LombokExamples lombokExamples = new LombokExamples(); assertTrue(lombokExamples.isRunning()); }
3.2. Two boolean Fields With the Same Accessor Name
Sometimes, there can be conflicts.
Let’s say that we need to have the following lines in the same class:
@Getter public boolean running = true; @Getter public boolean isRunning = false;
There are many reasons we should avoid a confusing naming convention like this. One of them is that it creates a conflict for Lombok.
Using Lombok’s convention, these two fields would have the same accessor method name: isRunning. But having two methods with the same name in the same class will create a compiler error.
Lombok solves this by creating only one accessor method and, in this case, pointing it at running, based on field declaration order:
@Test public void whenTwoBooleanFieldsCauseNamingConflict_thenLombokMapsToFirstDeclaredField() { LombokExamples lombokExamples = new LombokExamples(); assertTrue(lombokExamples.isRunning() == lombokExamples.running); assertFalse(lombokExamples.isRunning() == lombokExamples.isRunning); }
4. Using @Getter on a Boolean Field
Now, Lombok treats the Boolean type just a bit differently.
Let’s try our same running example one last time, but with Boolean instead of the primitive type:
@Getter private Boolean running;
Instead of creating isRunning, Lombok will generate getRunning:
@Test public void whenFieldOfBooleanType_thenLombokPrefixesMethodWithGetInsteadOfIs() { LombokExamples lombokExamples = new LombokExamples(); assertTrue(lombokExamples.getRunning()); }
5. Conclusion
In this article, we explored how to use Lombok’s @Getter annotation for boolean primitives and Boolean objects.
And make sure to check out the samples on Github.