Should Lombok make people love and hate?

Posted Jun 16, 20204 min read

1 Introduction

Lombok, an island in Indonesia, Lombok. But in the Java world, it is a convenient class library that can provide a lot of convenience, so it is favored by many people. But there are also many objections. Why is this?


The sunset taken before going to Lombok.

2 Conveniences provided by Lombok

In general, when we use POJO in Java, it is easy to think of Lombok, such as VO, DTO, DO, etc. To use Lombok, you need to install the corresponding IDE plug-in, and you need to introduce dependencies:


For example, if Lombok is not used, the amount of code to implement getter/setter, equals, hashCode, and toString is very large, as shown below:

package com.pkslow.basic.lombok;

import java.util.Objects;

public class Book {
    private String name;
    private int id;
    private double price;
    private String author;
    private String desc;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public int getId() {
        return id;

    public void setId(int id) { = id;

    public double getPrice() {
        return price;

    public void setPrice(double price) {
        this.price = price;

    public String getAuthor() {
        return author;

    public void setAuthor(String author) { = author;

    public String getDesc() {
        return desc;

    public void setDesc(String desc) {
        this.desc = desc;

    public boolean equals(Object o) {
        if(this == o) return true;
        if(o == null || getClass() != o.getClass()) return false;
        Book book =(Book) o;
        return id == &&
      , price) == 0 &&
                Objects.equals(name, &&
                Objects.equals(author, &&
                Objects.equals(desc, book.desc);

    public int hashCode() {
        return Objects.hash(name, id, price, author, desc);

    public String toString() {
        return "Book{" +
                "name='" + name +'\'' +
                ", id=" + id +
                ", price=" + price +
                ", author='" + author +'\'' +
                ", desc='" + desc +'\'' +

And most of it is boilerplate code without much actual logic.

If you use Lombok, it is very simple. An annotation @Data can complete the above work(the method on the right can only be displayed after the Lombok plugin is installed):

Common annotations of Lombok are:

@NonNull:Used for short pointer anomaly detection;

@Cleanup:automatic resource shutdown;

@Getter/@Setter:Automatically generate get/set methods;

@ToString:Generate toString method for printing and debugging;

@EqualsAndHashCode:Generate equals and hashCode methods, note that these two should be implemented at the same time;

@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor:constructor;

@Data:Equivalent to @ToString+ @EqualsAndHashCode+@Getter+ @Setter +@RequiredArgsConstructor.

@Builder:Generate builder method;

@Log:log related:


So Lombok can really bring us great convenience, reduce a lot of repetitive, no business logic code, the code looks relatively clean; modify the name of the field, do not need to be modified in many places; improve development efficiency.

3 Problems caused by Lombok

Of course, there are also different opinions:

There is no specific code when debugging, which is not convenient for Debug;

Need to force others to install third-party plug-ins, otherwise it will display an error;

When checking the test coverage, it is impossible to visually show which codes are covered and not covered;

Cannot be implemented according to your wishes, such as the toString method, sometimes you need to output a different String format, which is not necessarily implemented according to Lombok;

For some commonly used methods, the IDE can already be automatically generated, and it can be developed efficiently without the need of Lombok.

IDEA does not provide Builder, but it can be used by installing plugins.

After successful installation, the Builder code can be generated:

4 Summary

At the beginning, I supported the use of Lombok. After a period of use and some problems, I still think that the way of automatically generating code through the IDE is more suitable. After all, it is too savage to force others to install plug-ins. When IDEA generates code, others will not report errors if they do not install plug-ins.

In addition, a few more lines of code can reflect the workload, ha ha ha ha ha ha ha...

Welcome to Pumpkin slow talk for more exciting articles!

Welcome to pay attention to the WeChat public account <Pumpkin slow talk>, will continue to update you...

Read more and share more; write more and organize more.