Lombok introduction, use, working principle, advantages and disadvantages

1. Introduction to Lombok

General meaning: Lombok is a Java library that can automatically plug into editors and build tools to simplify Java development. By adding annotations, there is no need to write getter or eques methods for the class, and log variables can be automated. Official website link

In short: Lombok can simplify Java code in the form of simple annotations and improve developers' development efficiency.

2.Lombok use

The development environment required to use Lombok is Java+Maven+IntelliJ IDEA or Eclipse (install Lombok Plugin)

2.1 Add maven dependencies

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

2.2 Installing plug-ins
Using Lombok also requires the cooperation of plug-ins. I use the development tool idea. Here I only explain how to install the lombok plug-in in idea, use eclipse and myeclipse partners and install it by yourself on Google.
Open the idea's settings, click Plugins, click Browse repositories, search for lombok in the pop-up window, and then install it.
img

2.3 Solve compile-time error problems

An error occurred during compilation, possibly because the annotation processor was not enabled. Annotation Processors > Enable annotation processing. After the setup is completed, the program runs normally.

img

2.4 Example

Here are two chestnuts to see the difference between using lombok and not using it.

Create a user class

Not using Lombok

public class User implements Serializable {
    
    

    private static final long serialVersionUID = -8054600833969507380L;

    private Integer id;

    private String username;

    private Integer age;

    public User() {
    
    
    }

    public Integer getId() {
    
    
        return id;
    }

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

    public String getUsername() {
    
    
        return username;
    }

    public void setUsername(String username) {
    
    
        this.username = username;
    }

    public Integer getAge() {
    
    
        return age;
    }

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

    @Override
    public String toString() {
    
    
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) {
    
    
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
    
    
            return false;
        }
        User user = (User) o;
        return Objects.equals(id, user.id) &&
                Objects.equals(username, user.username) &&
                Objects.equals(age, user.age);
    }

    @Override
    public int hashCode() {
    
    
        return Objects.hash(id, username, age);
    }

}

Use Lombok

@Data
public class User implements Serializable {
    
    

    private static final long serialVersionUID = -8054600833969507380L;

    private Integer id;

    private String username;

    private Integer age;

}

Compile the source file, and then decompile the class file. The decompilation result is as shown below. Note: @Data annotation on a class will automatically generate setter/getter, equals, canEqual, hashCode, and toString methods for all properties of the class. If it is a final property, no setter method will be generated for the property.

img

Automation log variables

@Slf4j
@RestController
@RequestMapping(("/user"))
public class UserController {
    
    

    @GetMapping("/getUserById/{id}")
    public User getUserById(@PathVariable Integer id) {
    
    
        User user = new User();
        user.setUsername("风清扬");
        user.setAge(21);
        user.setId(id);

        if (log.isInfoEnabled()) {
    
    
            log.info("用户 {}", user);
        }

        return user;
    }

}

Through decompilation, you can see that the @Slf4j annotation generates a log variable (strictly speaking, a constant). You can use log to record logs in a class without declaring a log.

img

2.5 Commonly used annotations
Here are some commonly used annotations:

@Setter 注解在类或字段,注解在类时为所有字段生成setter方法,注解在字段上时只为该字段生成setter方法。
@Getter 使用方法同上,区别在于生成的是getter方法。
@ToString 注解在类,添加toString方法。
@EqualsAndHashCode 注解在类,生成hashCode和equals方法。
@NoArgsConstructor 注解在类,生成无参的构造方法。
@RequiredArgsConstructor 注解在类,为类中需要特殊处理的字段生成构造方法,比如final和被@NonNull注解的字段。
@AllArgsConstructor 注解在类,生成包含类中所有字段的构造方法。
@Data 注解在类,生成setter/getter、equals、canEqual、hashCode、toString方法,如为final属性,则不会为该属性生成setter方法。
@Slf4j 注解在类,生成log变量,严格意义来说是常量。private static final Logger log = LoggerFactory.getLogger(UserController.class);

3. Lombok working principle

In the process of using Lombok, you only need to add the corresponding annotations, and there is no need to write any code for this. How is the automatically generated code generated?

The core point is the analysis of annotations. While JDK5 introduced annotations, it also provided two parsing methods.

Runtime analysis
For annotations that can be parsed at runtime, @Retention must be set to RUNTIME, so that the annotation can be obtained through reflection. The java.lang.reflect reflection package provides an interface AnnotatedElement, which defines several methods for obtaining annotation information. Class, Constructor, Field, Method, Package, etc. all implement this interface. Friends who are familiar with reflection should be very familiar with it. Be familiar with this method of parsing.

Compile-time parsing
There are two mechanisms for compile-time parsing, which are briefly described below:

1)Annotation Processing Tool

apt was generated from JDK5. JDK7 has been marked as expired and is not recommended for use. It has been completely deleted in JDK8. Starting from JDK6, you can use the Pluggable Annotation Processing API to replace it. There are two main reasons for apt to be replaced:

(1) The APIs are all under the com.sun.mirror non-standard package
  (2) They are not integrated into javac and require additional operations.

2)Pluggable Annotation Processing API

JSR 269 was added since JDK6. As an alternative to apt, it solves two problems of apt. When javac is executed, it will call the program that implements the API, so that we can make some enhancements to the compiler. The process of javac execution as follows:

img

Lombok本质上就是一个实现了“JSR 269 API”的程序。在使用javac的过程中,它产生作用的具体流程如下:

1.javac对源代码进行分析,生成了一棵抽象语法树(AST2.运行过程中调用实现了“JSR 269 API”的Lombok程序
3.此时Lombok就对第一步骤得到的AST进行处理,找到@Data注解所在类对应的语法树(AST),然后修改该语法树(AST),增加getter和setter方法定义的相应树节点
4.javac使用修改后的抽象语法树(AST)生成字节码文件,即给class增加新的节点(代码块)

By reading the Lombok source code, I found that the implementation of the corresponding annotations is in HandleXXX. For example, the implementation of the @Getter annotation is in HandleGetter.handle(). There are some other libraries implemented in this way, such as Google Auto , Dagger , etc.

4. Advantages and disadvantages of Lombok

advantage:

(1) It can automatically generate constructors, getters/setters, equals, hashcode, toString and other methods in the form of annotations, which improves a certain development efficiency. (2) It makes the code concise and does not need to pay too much attention to the
corresponding methods
( 3) When attributes are modified, it also simplifies the maintenance of getter/setter methods generated for these attributes.

shortcoming:

(1) Overloading of multiple parameter constructors is not supported
(2) Although it saves the trouble of manually creating getter/setter methods, it greatly reduces the readability and integrity of the source code, and reduces the comfort of reading the source code. Spend

Guess you like

Origin blog.csdn.net/qq_43842093/article/details/133250441