1. Introduction
1.介绍
Project Lombok is a popular library for reducing Java boilerplate.
Project Lombok是一个用于减少Java模板的流行库。
In this quick 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.
在这个快速教程中,我们将看看Lombok的@Getter注解如何在布尔字段上工作,以消除创建其相应getter方法的需要。
2. Maven Dependency
2.Maven的依赖性
Let’s start by adding Project Lombok to our pom.xml:
让我们先把Project Lombok添加到我们的pom.xml。
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
3. Using @Getter on a boolean Field
3.在一个boolean字段上使用@Getter
Let’s say that we want Lombok to generate an accessor method for our private boolean field.
假设我们希望Lombok为我们的私有布尔字段生成一个访问器方法。
We can annotate that field with @Getter:
我们可以用@Getter来注解该字段。
@Getter
private boolean running;
And Lombok will use its annotation processor to generate an isRunning() method in the class.
而Lombok将使用其annotation处理器在类中生成一个isRunning()方法。
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 as Its Accessor
3.1.一个boolean字段的名称与它的访问器相同
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.
如果Lombok创建一个名为isIsRunning的方法,那就有点麻烦了。
Instead, Lombok creates isRunning like before:
相反,Lombok创建了isRunning像以前一样。
@Test
public void whenBooleanFieldPrefixedWithIs_thenMethodNameIsSameAsFieldName() {
LombokExamples lombokExamples = new LombokExamples();
assertTrue(lombokExamples.isRunning());
}
3.2. Two boolean Fields With the Same Accessor Name
3.2.两个具有相同访问器名称的boolean字段
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的惯例,这两个字段会有相同的访问器方法名。isRunning。但是在同一个类中有两个同名的方法会产生一个编译器错误。
Lombok solves this by creating only one accessor method and, in this case, pointing it at running, based on field declaration order:
Lombok通过只创建一个访问器方法来解决这个问题,在这种情况下,根据字段声明顺序,将其指向running。
@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
4.在Boolean字段上使用@Getter
Now, Lombok treats the Boolean type just a bit differently.
现在,Lombok对待Boolean类型的方式有点不同。
Let’s try our same running example one last time, but with Boolean instead of the primitive type:
让我们最后一次尝试同样的运行例子,但用Boolean代替原始类型。
@Getter
private Boolean running;
Instead of creating isRunning, Lombok will generate getRunning:
Lombok不会创建isRunning,而是生成getRunning。
@Test
public void whenFieldOfBooleanType_thenLombokPrefixesMethodWithGetInsteadOfIs() {
LombokExamples lombokExamples = new LombokExamples();
assertTrue(lombokExamples.getRunning());
}
5. Conclusion
5.结论
In this article, we explored how to use Lombok’s @Getter annotation for boolean primitives and Boolean objects.
在这篇文章中,我们探讨了如何将Lombok的@Getter注解用于布尔基元和布尔对象。
And make sure to check out the samples over on Github.