Omitting Getter or Setter in Lombok – 在Lombok省略Getter或Setter

最后修改: 2021年 11月 21日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Sometimes, we want to hide the ability to get or set a field value in our objects. But Lombok generates the default getter/setter automatically. In this quick tutorial, we’ll show how we can omit the getters and setters from being generated by Lombok. A detailed look at the Project Lombok library is also available in Introduction to Project Lombok.

有时,我们想在我们的对象中隐藏获取或设置字段值的能力。但是Lombok会自动生成默认的getter/setter。在这个快速教程中,我们将展示我们如何省略Lombok生成的getter和setter。在Project Lombok简介中也有对Project Lombok库的详细介绍。

Before continuing, we should install the Lombok plugin in our IDE.

在继续之前,我们应该在我们的IDE中安装Lombok插件>。

2. Dependencies

2.依赖性

First, we need to add Lombok to our pom.xml file:

首先,我们需要将Lombok添加到我们的 pom.xml文件。

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.22</version>
    <scope>provided</scope>
</dependency>

3. Default Behavior

3.默认行为

Before we get into the details of how to omit the generation of getters and setters, let’s review the default behavior of the annotations responsible for generating them.

在我们讨论如何省略生成getters和setters的细节之前,让我们回顾一下负责生成这些注释的默认行为

3.1. @Getter and @Setter Annotations

3.1.@Getter 和 @Setter 注释

Lombok provides two accessor annotations, @Getter and @Setter. We can annotate every field or simply mark the whole class with them. Generated methods will be public by default. However, we can change the access level to protected, package, or private. Let’s see an example:

Lombok提供了两个访问器注释,@Getter@Setter。我们可以对每个字段进行注解,或者简单地用它们来标记整个类。生成的方法默认为public。然而,我们可以将访问级别改为protected、package或private。让我们看一个例子。

@Setter
@Getter
public class User {
    private long id;
    private String login;
    private int age;
}

We can use the delombok option from the plugin in our IDE and see the code Lombok generated:

我们可以在IDE中使用插件中的delombok选项,看到Lombok生成的代码。

public class User {
    private long id;
    private String login;
    private int age;

    public long getId() {
        return this.id;
    }

    public String getLogin() {
        return this.login;
    }

    public int getAge() {
        return this.age;
    }

    public void setId(long id) {
        this.id = id;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

As we can observe, all getters and setters were created. Getter and setters for all fields are public because we didn’t specify the access level on any field explicitly.

我们可以看到,所有的getters和setters都被创建了。所有字段的获取器和设置器都是公开的,因为我们没有明确指定任何字段的访问级别

3.2. @Data Annotation

3.2.@数据注释

@Data combines features of a few other annotations, including both @Getter and @Setter. So, in this case, default accessor methods will be generated as public also:

@Data结合了其他几个注解的特征,包括@Getter@Setter.,所以在这种情况下,默认的访问器方法也将被生成为public

@Data
public class Employee {
    private String name;
    private String workplace;
    private int workLength;
}

4. Omitting Getter or Setter Using AccessLevel.NONE 

4.使用AccessLevel.NONE省略Getter或Setter

To disable default getter/setter generation on a specific field, we should use a specific access level:

要禁用特定字段的默认getter/setter生成,我们应该使用一个特定的访问级别。

@Getter(AccessLevel.NONE)
@Setter(AccessLevel.NONE)

This access level lets us override the behavior of a @Getter, @Setter, or @Data annotation on a class. To override the access level, annotate the field or class with an explicit @Setter or @Getter annotation.

这个访问级别让我们覆盖类上的@Getter@Setter@Data注解的行为。要覆盖访问级别,需要用明确的@Setter@Getter注解来注释字段或类。

4.1. Overriding @Getter and @Setter Annotations

4.1.重写 @Getter@Setter 注释

Let’s change the AccessLevel to NONE on the getter for the age field and the setter for the id field:

让我们把年龄字段的getter和id字段的setter的AccessLevel改为NONE

@Getter
@Setter
public class User {
    @Setter(AccessLevel.NONE)
    private  long id;
    
    private String login;
    
    @Getter(AccessLevel.NONE)
    private int age;
}

Let’s delombok this code:

让我们delombok这个代码。

public class User {
    private  long id;

    private String login;

    private int age;

    public long getId() {
        return this.id;
    }

    public String getLogin() {
        return this.login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

As we can see, there’s no setter for the id field and go getter for the age field.

我们可以看到,id字段没有setter,age字段没有getter。

4.2. Overriding the @Data Annotation

4.2.重写@Data注释

Let’s see another example, in which we change the AccessLevel to NONE on the class with the @Data annotation:

让我们看看另一个例子,在这个例子中,我们将带有@Data注解的类上的AccessLevel改为NONE

@Data
public class Employee {

    @Setter(AccessLevel.NONE)
    private String name;

    private String workplace;
    
    @Getter(AccessLevel.NONE)
    private int workLength;
}

We added the explicit @Getter annotation to the workLength field and explicit @Setter annotation to the name field. The AccessLevel of both accessors is set to NONE. Let’s see the delombok code:

我们为workLength字段添加了明确的@Getter注释,为name字段添加了明确的@Setter注释。两个访问器的AccessLevel都被设置为NONE。让我们看看delombok代码。

public class Employee {

    private String name;

    private String workplace;

    private int workLength;
    
    public String getName() {
        return this.name;
    }

    public String getWorkplace() {
        return this.workplace;
    }

    public void setWorkplace(String workplace) {
        this.workplace = workplace;
    }

    public void setWorkLength(int workLength) {
        this.workLength = workLength;
    }
}

As we expected, our explicit settings of @Getter and @Setter override the getters and setters generated by the @Data annotation. There is no setter generated for the name field and no getter generated for the workLength field.

正如我们所期望的,我们对@Getter@Setter的明确设置覆盖了由@Data注解生成的getter和setter。没有为name字段生成setter,也没有为workLength字段生成getter。

5. Conclusion

5.总结

In this article, we explored how to omit getter and setter generation by Lombok for specific fields in our objects. Moreover, we saw examples for @Getter, @Setter, and @Data annotations. Next, we saw the code that Lombok generated for our annotation settings.

在这篇文章中,我们探讨了如何通过Lombok为我们对象中的特定字段省略getter和setter的生成。此外,我们看到了@Getter@Setter@Data注释的例子。接下来,我们看到了Lombok为我们的注解设置生成的代码。

As always, the code is available over on GitHub.

像往常一样,代码可在GitHub上获得