LS Price Increase Launch

The Price of all “Learn Spring” course packages will increase by $40 on next Friday:

>> GET ACCESS NOW

1. Overview

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.

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

2. Dependencies

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

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

3. Default Behavior

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.

3.1. @Getter and @Setter Annotations

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:

@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:

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.

3.2. @Data Annotation

@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
public class Employee {
    private String name;
    private String workplace;
    private int workLength;
}

4. Omitting Getter or Setter Using AccessLevel.NONE 

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

@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.

4.1. Overriding @Getter and @Setter Annotations

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

@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:

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.

4.2. Overriding the @Data Annotation

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

@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:

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.

5. Conclusion

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.

As always, the code is available over on GitHub.

LS Price Increase Launch

The Price of all “Learn Spring” course packages will increase by $40 on next Friday:

>> GET ACCESS NOW
Generic footer banner
guest
0 Comments
Inline Feedbacks
View all comments