1. Introduction of Lombok
Lombok is a code generator that can help us simplify the removal of some of the necessary but bloated Java code by using the corresponding annotations to generate the corresponding method when compiling the source.
such as: Getter Setter toString Equels method, etc. can be automatically generated
Using Lombok is required, and if not installed, the IDE cannot parse the Lombok annotations.
Official address: https://projectlombok.org/
2, Lombok installation
2.1 Automatic Installation
Download Lombok.jar from the official website and put it in the folder directory where Eclipse.ini is located;
There are two methods of operation:
One, open cmd Run as Administrator, CD to the above directory, Run command: Java-jar Lombok.jar
Second, direct double-click Run, wait a few seconds after the installation screen will pop up, select the Eclipse.exe you want to install, click Install (if the prompt permission is not enough, use method one run)
See if the Eclipse.ini file finally has more than one row-javaagent:lombok.jar, there is, then the installation is successful;
Import the Lombok.jar package into the project, restart Eclipse, and start using!
2.2 Manual Installation
Manually add-javaagent:lombok.jar to the Eclipse.ini file at the end, save;
Import the Lombok.jar package into the project, restart Eclipse, and start using!
2.3 Maven
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.2</version>
</dependency>
3. general use of Lombok
@Getter/@Setter
Can be used on classes and attributes, placed on a class, generates a Getter/setter method on all non-static (Non-static) properties, and on the property, generates Getter/setter methods on the property. You can also specify the access level of the Getter/setter method.
@EqualsAndHashCode
By default, all non-transient (non-transient) and non-static (non-static) fields are used to generate the Equals and Hascode methods, or you can specify which properties are used specifically.
@ToString
Generates the ToString method, which, by default, outputs the class name, all properties, and the attributes are output in sequential order, separated by commas.
@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
The parameterless constructor, the partial parameter constructor, and the full parameter constructor, Lombok is powerless when we need to overload multiple constructors.
@Data
@ToString, @EqualsAndHashCode, the @getter of all attributes, the combination of @setter and @requiredargsconstructor of all non-final properties, usually we use this annotation as sufficient.
4, Lombok principle
Once you have solved the simple use, you should now be more curious about how it is implemented. Throughout the use of the process, only need to use annotations, do not need to do any additional work, the mystery should be in the interpretation of annotations. JDK5 introduces annotations and provides two ways of parsing.
4.1 Run-time parsing
Annotations that can be parsed at run time must have @retention set to runtime so that the annotations can be obtained by reflection. An interface annotatedelement is provided in the Java.lang.reflect reflector package that defines several methods for obtaining annotation information, such as Class, Constructor, Field, Method, package, etc., which are implemented by the interface. Most developers should be familiar with this parsing method.
Boolean isannotationpresent (class<? extends annotation> Annotationclass);
<t extends annotation> T getannotation (class<t> annotationclass);
Annotation[] Getannotations ();
Annotation[] Getdeclaredannotations ();
4.2 Compile-time parsing
4.2.1 Annotation processing Tool
APT has been generated from JDK5, JDK7 has been marked as outdated, deprecated, JDK8 has been completely removed, since JDK6, you can use the Pluggable Annotation processing API to replace it, There are 2 reasons why apt was replaced: The API is not integrated into the javac under the Com.sun.mirror non-standard package, and more introduction to the additional run of apt can be found here.
4.2.2Pluggable Annotation Processing API
JSR 269, since JDK6 joined, as an alternative to APT, it solves the two problems of APT, javac implementation of the program will invoke the implementation of the API, so that we can do some enhancements to the compiler, the Javac execution of the following procedures:
Lombok is to use this way to achieve, if interested can go to see its Lombok source code, corresponding annotations are implemented in handlexxx, such as @getter annotation implementation is handlegetter.handle (). There are other class libraries that are implemented in this way, such as Google Auto, Dagger, and so on.
Java Development speed Artifact Lombok,eclipse end-of-Life installation tutorial