Lombok learning and application

Lombok

The meaning of LomBok

Lombok can simplify java code in the form of simple annotations and improve the development efficiency of developers.
For example, for javaBeans that are often written in development, it takes time to add corresponding getter/setters, perhaps to write constructors, equals and other methods, and they need to be maintained. When there are many properties, a large number of getter/setter methods will appear.

Lombok can automatically generate constructors, getter/setter, equals, hashcode, and toString methods for properties at compile time through annotations. This saves the trouble of manually rebuilding the code and makes the code look more concise.

Lombok official website: https://projectlombok.org
Annotation official website analysis: https://projectlombok.org/features/all

Maven dependency:

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.16.20</version>
</dependency>

IDEA install lombok plugin

Since lombok's annotations belong to compile-time analytic annotations (described later), when writing code, the IDE will not think that this entity class does not have a get set, so an error will be reported when calling. At this point, you need to install the plug-in. IDEA and Eclipse have corresponding plug-ins, just search for lombok.
The following is how idea installs the plug-in:
File–>Settings–>Plugins Browse repositories on the bottom right side... Then search for lombok
idea install lombok plugin

Common notes

annotation Description
@Data Annotations on the 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.
@Getter/@Setter This annotation is on the property, and the Getter/Setter method can be automatically generated for the corresponding property
@NonNull This annotation is used on properties or constructors, Lombok will generate a non-empty declaration, which can be used to verify parameters and can help avoid null pointers.
@Cleanup This annotation can help us automatically call the close() method, such as IO stream.
@EqualsAndHashCode By default, all non-static and non-transient attributes are used to generate equals and hasCode, and some attributes can also be excluded through the exclude annotation.
@ToString The class is annotated with @ToString. Lombok will generate a toString() method. By default, it will output the class name and all attributes (in the order of attribute definition), separated by commas. By setting the includeFieldNames parameter to true, the toString() attribute can be output explicitly. Is this a bit convoluted? It will be clearer through the code.
@NoArgsConstructor Parameterless constructor
@RequiredArgsConstructor Partial parameter constructor
@AllArgsConstructor Full parameter constructor

For detailed content, it is highly recommended to view the examples on the official website of lombok, which is easy to understand

How Lombok works

How exactly is the automatically generated code generated? The core point is the analysis of annotations. While JDK5 introduces annotations, it also provides two parsing methods.

  • Runtime resolution

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. Familiar with this analysis method.

  • Compile-time parsing

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

  • The Annotation Processing Tool
    apt was produced from JDK5. JDK7 has been marked as expired and is not recommended for use. JDK8 has been completely deleted. Since JDK6, Pluggable Annotation Processing API can be used to replace it. There are two main reasons for apt to be replaced:

    • APIs are all under the com.sun.mirror non-standard package
    • Not integrated into javac, need additional operation
  • Pluggable Annotation Processing API
    JSR 269 has been 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 executed by javac is as follows:
    How lombok works


Lombok is essentially a program that implements the "JSR 269 API". In the process of using javac, the specific process of its effect is as follows:

  1. javac analyzes the source code and generates an abstract syntax tree (AST)
  2. Call the Lombok program that implements "JSR 269 API" during operation
  3. At this time, Lombok processes the AST obtained in the first step, finds the syntax tree (AST) corresponding to the class where the @Data annotation is located, and then modifies the syntax tree (AST), adding the corresponding tree nodes defined by the getter and setter methods
  4. javac uses the modified abstract syntax tree (AST) to generate bytecode files, that is, add new nodes (code blocks) to the class

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

Advantages and disadvantages of Lombok

  • advantage:
  1. It can automatically generate constructor, getter/setter, equals, hashcode, toString and other methods in the form of annotations, which improves certain development efficiency
  2. Make the code concise without paying too much attention to the corresponding method
  3. When properties are modified, it also simplifies the maintenance of the getter/setter methods generated for these properties, etc.
  • Disadvantages:
  1. Does not support overloading of multiple parameter constructors
  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

Copyright statement: The content of the article is summarized on the Internet. If it violates the rights of the original author, please contact me for deletion or authorization

Guess you like

Origin blog.csdn.net/qq845484236/article/details/98089127