1, Introduction
java, as a very popular programming language, although it has very rich language features, complete object-oriented programming and highly standardized programming, it also has one of the most criticized shortcomings: verbosity, especially after many years of development, you will obviously feel that compared with dynamic language, java needs to create classes before defining variables, Then define variable types. Each class needs to write many methods such as get/set/toString/hashCode/equals.
Especially when an entity class has up to dozens of variables, after writing the get and set methods, the length of an entity class is close to a thousand lines.
In order to avoid writing these "verbose" methods, many programmers have been looking for a tool that can make them get rid of this repetitive work, such as idea, the quick generation of get and set methods of eclipse development tools, and the Lombok tool we want to talk about today.
2, Lombok
Lombok is a very popular code concise tool. Using its annotation feature, it can directly help us save hundreds of lines of get and set methods, which is very convenient to operate.
If it is an idea development tool, you can directly search lombok in preferences - > plugins, and then click Install!
Next, import the lombok dependency package in the project!
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency>
Finally, you only need to add @ Data annotation to the corresponding entity class to complete the injection of class attribute get/set.
import lombok.Data;
@Data public class User { private String id; private String age; private String name; //There is no need to explicitly write get and set methods }
Using @ Data annotation on the class, the attributes in the entity class do not need to explicitly write get and set methods.
After compiling this class, we open the User.class file to see what the compiled file looks like?
public class User { private String id; private String age; private String name; public User() { } public String getId() { return this.id; } public String getAge() { return this.age; } public String getName() { return this.name; } public void setId(String id) { this.id = id; } public void setAge(String age) { this.age = age; } public void setName(String name) { this.name = name; } public boolean equals(Object o) { if (o == this) { return true; } else if (!(o instanceof User)) { return false; } else { User other = (User)o; if (!other.canEqual(this)) { return false; } else { label47: { Object this$id = this.getId(); Object other$id = other.getId(); if (this$id == null) { if (other$id == null) { break label47; } } else if (this$id.equals(other$id)) { break label47; } return false; } Object this$age = this.getAge(); Object other$age = other.getAge(); if (this$age == null) { if (other$age != null) { return false; } } else if (!this$age.equals(other$age)) { return false; } Object this$name = this.getName(); Object other$name = other.getName(); if (this$name == null) { if (other$name != null) { return false; } } else if (!this$name.equals(other$name)) { return false; } return true; } } } protected boolean canEqual(Object other) { return other instanceof User; } public int hashCode() { int PRIME = true; int result = 1; Object $id = this.getId(); int result = result * 59 + ($id == null ? 43 : $id.hashCode()); Object $age = this.getAge(); result = result * 59 + ($age == null ? 43 : $age.hashCode()); Object $name = this.getName(); result = result * 59 + ($name == null ? 43 : $name.hashCode()); return result; } public String toString() { return "User(id=" + this.getId() + ", age=" + this.getAge() + ", name=" + this.getName() + ")"; } }
It is clear that after using the @ Data annotation, the User class adds get, set, hashCode, equals and toString methods.
Through the above example, you can find that using @ Data annotation can greatly reduce the amount of code and make the code very concise. This is also the main reason why many developers are keen to use Lombok.
How does Lombok work?
Because the official version of Java does not provide annotation tools for this rapid generation method, tools like Lombok actually use method injection from the Annotation Processing technology of Java 6 and JSR 269.
In short, it uses the Java non-public API. When javac compiles code, it obtains the javacinnotationprocessor object through strong type conversion, then obtains the abstract syntax tree (AST) from the javacinnotationprocessor method for forced modification, and injects get, set and other methods.
The biggest advantage of using Lombok is that it can save a lot of duplicate code and make the code more concise! But there are also many disadvantages!
3, What are the disadvantages?
3.1 force teammates to install Lombok
When you are using Lombok tool plug-in to quickly develop a project, if other colleagues want to cooperate with you to develop the project, they have to install Lombok plug-in, otherwise the project compilation will report an error.
3.2 reduction of code debuggability
Code debuggability will be reduced. Why do you say so?
Lombok saves us the programming of get and set methods, but if I want to know which methods operate on a property of a class to set, I can know the caller well if I use native methods. But if you use Lombok plug-in to generate, you won't know at this time. I can't even debug!
3.3 if you don't understand Lombok notes, you will step on the pit
We know that using @ Data will override hashCode() and equals() methods. If it is a single entity class without inheritance, you will not have a problem using @ Data.
However, if the entity class inherits from the parent class, @ Data will only override the hashCode() and equals() methods of the child class and will not add the properties of the parent class, which may lead to unexpected results. For example, when you use HashMap and use the current entity class as the key.
In this case, you can add this annotation @ EqualsAndHashCode(callSuper=true) to the class, and the hashCode() and equals() methods of the subclass will add the attributes of the parent class.
3.4 destructive encapsulation
Encapsulation is a very important feature in java object-oriented programming.
For example, for the User entity class, I add a new tag attribute. I only want to expose its get method, not the set method to the outside. When I don't use @ Data annotation, I can program flexibly, but after using @ Data annotation, the attribute tag is completely exposed to the outside.
public class User { private String id; private String age; private String name; private String tag = "student"; public String getTag() { return tag; } }
3.5 jdk upgrade
In fact, the above pits are not big pits. In my opinion, the biggest pit is actually the working principle of Lombok. It uses unofficial API interfaces to modify classes through program forced implantation to realize the injection of get, set and other methods.
According to today's JDK upgrade frequency, a new version will be launched every six months, but Lombok, as a third-party tool and maintained by the open source team, its iteration speed can not be guaranteed.
If JDK blocks the back door one day, Lombok will basically not work. It will be a trouble at that time.
4, Summary
Lombok, as a very popular tool plug-in, must have its own advantages. Whether it is recommended to be used in daily development or not is actually a neutral attitude. If people in your team like it, it is recommended that you use it. Before using it, you'd better train what pits are there and avoid stepping on them.
If most people don't like to use it, they don't recommend you to use it. Many companies prohibit you from using it. In fact, this plug-in is a little similar to that kind of rogue plug-in. The working principle is not realized in an officially recognized way. If this leak is blocked by the new version of jdk one day, it will be more difficult for the project to upgrade jdk.
Therefore, when evaluating whether to introduce Lombok into the code, we should consider its advantages and what problems it will bring, so as to make better decisions!