Article directory
problem background
Lombok uses @Data and @Builder at the same time to build a parameterless constructor and report an error! Compilation fails. As shown below:
Lombok @Data and @Builder analyze usage separately
Lombok can use @Data to generate getter/setter methods for no-argument construction and all properties in the class. Can simplify the development of our code. (Need to install the Lombok plugin and introduce Lombok dependencies).
For example, the following entity class, after introducing Lombok, can automatically generate GET/SET methods and no-argument constructors.
The compiled class is: You can see that not only get and set are generated for us, but also a default parameterless constructor
So how to automatically generate a constructor with parameters? Using the @Builder annotation will help us complete the construction method of the property.
The compiled class is: You can see that the construction method of all attributes has been built for us, but if the value only refers to the @Builder annotation, get and set cannot be generated.
But if @Data and @Builder are used at the same time, it can be seen that although the GET/SET method is generated, the no-argument construction method is gone, which is obviously unacceptable, because many frameworks will call no-argument construction to create objects.
Compiled class:
We try to manually add a no-argument constructor to the Tet1 class. The compilation found that the error report failed:
Solution
method one
When Lombok uses @Data and @Builder at the same time, if you want to generate a parameterless structure, you need to manually introduce the annotation @Tolerate in the code, so that Lombok will not be aware of the specified constructor when generating the class.
Method Two
Directly use the method of no-argument constructor + parameter constructor, @RequiredArgsConstructor to build a parameter, @NoArgsConstructor to build a no-argument constructor, as shown in the figure:
Compiled effect:
Lombok principle
Java compilation is divided into the following stages:
Parse and populate symbol table -> annotation processing -> analysis and bytecode generation -> generate binary class file.
- Lombok uses JSR 269: Pluggable Annotation Processing API (compilation-time annotation processor) implemented by JDK 6. It converts Lombok's annotation code into a conventional Java method at compile time to implement injection.
- In the compilation stage, when the Java source code is abstracted into a syntax tree (AST), Lombok will dynamically modify the AST according to its own annotation processor, and add new code (nodes). After all these are executed, it will be generated through analysis The final bytecode (.class) file, which is how Lombok works.
A simple Setter can be implemented with the help of annotation processors. Our implementation steps are:
- Define an annotation tag interface card and implement a custom annotation processor;
- Use tools.jar's javac api to process AST (Abstract Syntax Tree)
- Compile the code with a custom annotation processor.
1. Define custom annotations and annotation processors
First create a MySetter.java to customize an annotation, the code is as follows:
Then implement a custom annotation processor, the code is as follows:
The test class is as follows:
2. Compile the annotation processor, and then use the annotation processor to compile the class
First, you need to compile the annotation processor first (javac -cp is used to reference third-party jar packages for compilation)
Then use the annotation processor to compile the Person test class:
At this time, look at the generated Person.class again, and you can find that the Setter method has been generated:
Summarize
Of course, although the test class has already generated the Setter method, because it is generated during compilation, we cannot directly adjust the Setter method during development. Therefore, Lombok provides a plug-in mechanism so that we can directly adjust it during development. Features of Lombok.