Using Lombok is very simple, so let’s take a look:
1) Import the corresponding Maven package:
< the dependency > < groupId > org. Projectlombok < / groupId > < artifactId > lombok < / artifactId > < version > 1.16.18 < / version > <scope>provided</scope> </dependency>Copy the code
Lombok’s scope=provided means that it takes effect only at compile time and does not need to be inserted into packages. In fact, Lombok correctly compiles the Lombok annotated Java file into a complete Class file at compile time.
2) Add IDE support for Lombok:
Introduction of Lombok support in IDEA is as follows:
- Click File– Settings to install the Lombok plug-in:
- Click on the File– Settings screen to open Annocation Processors:
This is enabled for the Lombok annotations to work during compilation.
Eclipse Lombok plug-in can be installed by baidu, also relatively simple, it is worth mentioning that, because the Eclipse built-in Compiler is not Oracle Javac, but Eclipse’s own implementation of the Eclipse Compiler for Java (ECJ). To enable ECJ to support Lombok, you need to add the following two things to your eclipse.ini configuration file:
-xbootclasspath /a:[lombok. Jar directory]
– JavaAgent :[lombok. Jar directory]
3) Lombok implementation principle:
- Javac analyzes the source code to generate an abstract syntax tree (AST)
- The Lombok program that implements JSR 269 is called during javAC compilation
- Lombok then processes the AST obtained in the first step, finds the syntax tree (AST) corresponding to the class of the Lombok annotation, and modiizes the syntax tree (AST) to add the corresponding tree node defined by the Lombok annotation
- Javac uses a modified abstract syntax tree (AST) to generate bytecode files
4) Use of Lombok annotations:
POJO class:
@getter / @setter: Generates Getter/ Setter methods for all member variables on the class; Acts on a member variable to generate a getter/setter method for that member variable. You can set the access permission and whether the load is lazy.
package com.trace; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; /** * Created by Trace on 2018/5/19.<br/> ** Created by Trace on 2018/5/19."unused") public class TestClass { public static void main(String[] args) { } @Getter(value = AccessLevel.PUBLIC) @Setter(value = AccessLevel.PUBLIC) public static class Person { private String name; private int age; private boolean friendly; } public static class Animal { private String name; private int age; @Getter @Setter private boolean funny; }}Copy the code
In the Structure view, you can see that getters/setters and other methods have been generated:
The compiled code looks like this: [This is the same boilerplate code you would write in traditional Java programming]
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package com.trace;
public class TestClass {
public TestClass() {
}
public static void main(String[] args) {
}
public static class Animal {
private String name;
private int age;
private boolean funny;
public Animal() {
}
public boolean isFunny() {
return this.funny;
}
public void setFunny(boolean funny) {
this.funny = funny;
}
}
public static class Person {
private String name;
private int age;
private boolean friendly;
public Person() {
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public boolean isFriendly() {
return this.friendly;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setFriendly(boolean friendly) { this.friendly = friendly; }}}Copy the code
@toString: Applies to a class and overrides the default ToString () method. You can display certain fields with the of attribute and exclude certain fields with the exclude attribute.
@ EqualsAndHashCode:Overrides equals and hashCode by default
NonNull: applies primarily to member variables and arguments. The identifier cannot be null, otherwise a null pointer exception is thrown.
@ NoArgsConstructor @ RequiredArgsConstructor, @ AllArgsConstructor:Applied to a class to generate a constructor. Properties such as staticName and access are available.
The staticName property, once set, generates the instance using a static method, and the Access property restricts access.
@noargsconstructor: generates a constructor with no arguments;
@requiredargsconstructor: a constructor that generates a member variable with final and @nonNULL annotations;
@allargsconstructor: Generates a full parameter constructor.
The result is:
public static class Person {
@NonNull
private String name;
private int age;
private boolean friendly;
public String toString() {
return "TestClass.Person(name=" + this.getName() + ", age=" + this.getAge() + ")";
}
@NonNull
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public boolean isFriendly() {
return this.friendly;
}
public void setName(@NonNull String name) {
if(name == null) {
throw new NullPointerException("name");
} else {
this.name = name;
}
}
public void setAge(int age) {
this.age = age;
}
public void setFriendly(boolean friendly) {
this.friendly = friendly;
}
private Person() {
}
private static TestClass.Person of() {
return new TestClass.Person();
}
@ConstructorProperties({"name"})
Person(@NonNull String name) {
if(name == null) {
throw new NullPointerException("name");
} else {
this.name = name;
}
}
@ConstructorProperties({"name"."age"."friendly"})
public Person(@NonNull String name, int age, boolean friendly) {
if(name == null) {
throw new NullPointerException("name");
} else{ this.name = name; this.age = age; this.friendly = friendly; }}}Copy the code
@data: Applied to a class, is a collection of the following annotations: @toString @EqualSandHashCode @getter @setter @requiredargsConstructor
@Builder: Applies to a class, turning it into a Builder mode
@log: Applies to a class to generate a Log variable. For different log implementation products, there are different notes:
Cleanup: Automatically closes resources for objects that implement the Java.io.Closeable interface, such as typical IO stream objects
The results are as follows:
Is it too simple.
SneakyThrows: Catch and throw the checked exception. You can override the main method as follows:
Synchronized: at the method level, you can replace the synchronize keyword or lock, which is not very useful.
Finish the full!
Next: Intellij IDEA for Java Efficiency Tools