In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "the function and use of Lombok". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
How to use Lombok
Lombok can simplify java code and improve the development efficiency of developers in the form of simple annotations. For example, development often need to write javabean, need to take the time to add the corresponding getter/setter, and perhaps to write constructors, equals and other methods, and need to maintain, when there are many attributes will appear a large number of getter/setter methods, these appear very tedious and not too much technical content, once you modify the properties, it is easy to forget to modify the corresponding method mistakes.
Lombok can automatically generate constructor, getter/setter, equals, hashcode, toString methods for attributes at compile time through annotations. The magic is that there are no getter and setter methods in the source code, but there are getter and setter methods in the compiled bytecode file. This saves the hassle of manually rebuilding the code and makes the code look more concise.
The use of Lombok is the same as referencing the jar package. You can download the jar package on the official website (https://projectlombok.org/download), or you can use maven to add dependencies:
Org.projectlombok lombok 1.16.20 provided@Data
@ Data annotations on the class will automatically generate setter/getter, equals, canEqual, hashCode, toString methods for all properties of the class. If it is a final property, no setter method will be generated for that property.
Import lombok.AccessLevel;import lombok.Setter;import lombok.Data;import lombok.ToString;@Data public class DataExample {private final String name; @ Setter (AccessLevel.PACKAGE) private int age; private double score; private String [] tags; @ ToString (includeFieldNames=true) @ Data (staticConstructor= "of") public static class Exercise {private final String name; private final T value;}}
If you do not use Lombok, the implementation is as follows:
Import java.util.Arrays;public class DataExample {private final String name; private int age; private double score; private String [] tags; public DataExample (String name) {this.name = name;} public String getName () {return this.name;} void setAge (int age) {this.age = age;} public int getAge () {return this.age;} public void setScore (double score) {this.score = score } public double getScore () {return this.score;} public String [] getTags () {return this.tags;} public void setTags (String [] tags) {this.tags = tags;} @ Override public String toString () {return "DataExample (" + this.getName () + "," + this.getAge () + "," + this.getScore () + "," + Arrays.deepToString (this.getTags ()) + ")" } protected boolean canEqual (Object other) {return other instanceof DataExample;} @ Override public boolean equals (Object o) {if (o = = this) return true; if (! (o instanceof DataExample)) return false; DataExample other = (DataExample) o; if (! other.canEqual ((Object) this)) return false; if (this.getName () = = null? Other.getName ()! = null:! this.getName (). Equals (other.getName ()) return false; if (this.getAge ()! = other.getAge ()) return false; if (Double.compare (this.getScore (), other.getScore ())! = 0) return false; if (! Arrays.deepEquals (this.getTags (), other.getTags ()) return false; return true;} @ Override public int hashCode () {final int PRIME = 59 Int result = 1; final long temp1 = Double.doubleToLongBits (this.getScore ()); result = (result*PRIME) + (this.getName () = = null? 43: this.getName (). HashCode ()); result = (result*PRIME) + this.getAge (); result = (result*PRIME) + (int) (temp1 ^ (temp1 > > 32)); result = (result*PRIME) + Arrays.deepHashCode (this.getTags ()); return result } public static class Exercise {private final String name; private final T value; private Exercise (String name, T value) {this.name = name; this.value = value;} public static Exercise of (String name, T value) {return new Exercise (name, value);} public String getName () {return this.name;} public T getValue () {return this.value } @ Override public String toString () {return "Exercise (name=" + this.getName () + ", value=" + this.getValue () + ")";} protected boolean canEqual (Object other) {return other instanceof Exercise;} @ Override public boolean equals (Object o) {if (o = = this) return true; if (! (o instanceof Exercise)) return false; Exercise other = (Exercise) o If (! other.canEqual ((Object) this)) return false; if (this.getName () = = null? Other.getValue ()! = null:! this.getName (). Equals (other.getName ()) return false; if (this.getValue () = = null? Other.getValue ()! = null:! this.getValue (). Equals (other.getValue ()) return false; return true;} @ Override public int hashCode () {final int PRIME = 59; int result = 1; result = (result*PRIME) + (this.getName () = = null? 43: this.getName (). HashCode ()) Result = (result*PRIME) + (this.getValue () = = null? 43: this.getValue () .hashCode ()); return result;} @ Getter/@Setter
If you think @ data is too cruel (because @ Data collects all the features of @ ToString, @ EqualsAndHashCode, @ Getter/@Setter, @ RequiredArgsConstructor) and is not fine enough, you can use the @ Getter/@Setter annotation, which is on the property, and can automatically generate Getter/Setter methods for the corresponding properties. Examples are as follows:
Import lombok.AccessLevel;import lombok.Getter;import lombok.Setter;public class GetterSetterExample {@ Getter @ Setter private int age = 10; @ Setter (AccessLevel.PROTECTED) private String name; @ Override public String toString () {return String.format ("% s (age:% d)", name, age);}}
If you do not use Lombok:
Public class GetterSetterExample {private int age = 10; private String name; @ Override public String toString () {return String.format ("% s (age:% d)", name, age);} public int getAge () {return age;} public void setAge (int age) {this.age = age;} protected void setName (String name) {this.name = name;}} @ NonNull
This annotation is used on an attribute or constructor, and Lombok generates a non-null declaration that can be used to validate parameters and help avoid null pointers.
Import lombok.NonNull;public class NonNullExample extends Something {private String name; public NonNullExample (@ NonNull Person person) {super ("Hello"); this.name = person.getName ();}
Do not use Lombok:
Import lombok.NonNull;public class NonNullExample extends Something {private String name; public NonNullExample (@ NonNull Person person) {super ("Hello"); if (person = = null) {throw new NullPointerException ("person");} this.name = person.getName ();}} @ Cleanup
This annotation helps us to call the close () method automatically, greatly simplifying the code.
Examples are as follows:
Import lombok.Cleanup;import java.io.*;public class CleanupExample {public static void main (String [] args) throws IOException {@ Cleanup InputStream in = new FileInputStream (args [0]); @ Cleanup OutputStream out = new FileOutputStream (args [1]); byte [] b = new byte [10000]; while (true) {int r = in.read (b); if (r =-1) break; out.write (b, 0, r);}
If you do not use Lombok, you need the following:
Import java.io.*;public class CleanupExample {public static void main (String [] args) throws IOException {InputStream in = new FileInputStream (args [0]); try {OutputStream out = new FileOutputStream (args [1]); try {byte [] b = new byte [10000]; while (true) {int r = in.read (b); if (r =-1) break Out.write (b, 0, r);}} finally {if (out! = null) {out.close ();} finally {if (in! = null) {in.close ();} @ EqualsAndHashCode
By default, all non-static (non-static) and non-transient (non-transient) attributes are used to generate equals and hasCode, and some attributes can be excluded through exclude annotations.
Import lombok.EqualsAndHashCode;@EqualsAndHashCode (exclude= {"id", "shape"}) public class EqualsAndHashCodeExample {private transient int transientVar = 10; private String name; private double score; private Shape shape = new Square (5,10); private String [] tags; private int id; public String getName () {return this.name;} @ EqualsAndHashCode (callSuper=true) public static class Square extends Shape {private final int width, height Public Square (int width, int height) {this.width = width; this.height = height;}} @ ToString
The class uses the @ ToString annotation, and Lombok generates a toString () method, which by default outputs the class name and all attributes (in the order in which they are defined), separated by commas.
By setting the includeFieldNames parameter to true, you can explicitly output the toString () attribute. This point is not a little tongue-twisting, through the code will be more clear.
Import lombok.ToString;@ToString (exclude= "id") public class ToStringExample {private static final int STATIC_VAR = 10; private String name; private Shape shape = new Square (5,10); private String [] tags; private int id; public String getName () {return this.getName ();} @ ToString (callSuper=true, includeFieldNames=true) public static class Square extends Shape {private final int width, height; public Square (int width, int height) {this.width = width This.height = height;}
Examples of not using Lombok are as follows:
Import java.util.Arrays;public class ToStringExample {private static final int STATIC_VAR = 10; private String name; private Shape shape = new Square (5,10); private String [] tags; private int id; public String getName () {return this.getName ();} public static class Square extends Shape {private final int width, height; public Square (int width, int height) {this.width = width; this.height = height } @ Override public String toString () {return "Square (super=" + super.toString () + ", width=" + this.width + ", height=" + this.height + ")";} @ Override public String toString () {return "ToStringExample (" + this.getName () + "," + this.shape + "," + Arrays.deepToString (this.tags) + ")";} @ NoArgsConstructor, @ RequiredArgsConstructor and @ AllArgsConstructor
No parameter constructor, partial parameter constructor, full parameter constructor. Lombok cannot overload multiple parameter constructors.
Import lombok.AccessLevel;import lombok.RequiredArgsConstructor;import lombok.AllArgsConstructor;import lombok.NonNull;@RequiredArgsConstructor (staticName = "of") @ AllArgsConstructor (access = AccessLevel.PROTECTED) public class ConstructorExample {private int x, y; @ NonNull private T description; @ NoArgsConstructor public static class NoArgsExample {@ NonNull private String field;}}
Examples of not using Lombok are as follows:
Public class ConstructorExample {private int x, y; @ NonNull private T description; private ConstructorExample (T description) {if (description = = null) throw new NullPointerException ("description"); this.description = description;} public static ConstructorExample of (T description) {return new ConstructorExample (description) } @ java.beans.ConstructorProperties ({"x", "y", "description"}) protected ConstructorExample (int x, int y, T description) {if (description = = null) throw new NullPointerException ("description"); this.x = x; this.y = y; this.description = description;} public static class NoArgsExample {@ NonNull private String field; public NoArgsExample () {}} Lombok working principle Analysis
You'll find that in the process of using Lombok, you only need to add the appropriate comments, and you don't have to write any code for it. How exactly is the automatically generated code generated?
The core is the analysis of annotations. While JDK5 introduces annotations, it also provides two parsing methods.
Runtime parsing
For annotations that can be parsed at run time, you must set @ Retention to RUNTIME so that you can get the annotation through reflection. An interface AnnotatedElement is provided in the java.lang,reflect reflection package, which defines several methods for obtaining annotation information. Class, Constructor, Field, Method, Package and so on all implement this interface. Friends who are familiar with reflection should be familiar with this parsing method.
Compile-time parsing
There are two mechanisms for compile-time parsing, which are briefly described respectively:
1) Annotation Processing Tool
Apt is generated from JDK5. JDK7 has been marked as expired and is not recommended. It has been completely deleted in JDK8. Since JDK6, you can use Pluggable Annotation Processing API to replace it. There are two main reasons why apt is replaced:
Api are all under com.sun.mirror non-standard package.
Not integrated into javac, need to run extra
2) Pluggable Annotation Processing API
JSR 269 has been added since JDK6. As an alternative to apt, it solves two problems of apt. When javac executes, it calls the program that implements the API, so we can make some enhancements to the compiler. At this time, javac executes as follows:
Lombok is essentially a program that implements "JSR 269 API". In the process of using javac, the specific process by which it works is as follows:
Javac analyzes the source code and generates an abstract grammar tree (AST)
When running, call the Lombok program that implements "JSR 269API".
At this point, Lombok processes the AST obtained in the first step, finds the grammar tree (AST) corresponding to the class of @ Data annotation, and then modifies the grammar tree (AST) to add the corresponding tree nodes defined by getter and setter methods.
Javac uses the modified abstract grammar tree (AST) to generate bytecode files, that is, to add new nodes (code blocks) to class.
Read the Lombok source code, and the corresponding annotations are implemented in HandleXXX, such as HandleGetter.handle () when the @ Getter annotation is implemented. There are other class libraries implemented in this way, such as Google Auto, Dagger, and so on.
Advantages and disadvantages of Lombok
Advantages:
It can automatically generate constructor, getter/setter, equals, hashcode, toString and other methods in the form of annotations, which improves the development efficiency.
Make the code concise without paying too much attention to the corresponding methods
When properties are modified, it also simplifies the maintenance of getter/setter methods generated for these properties, and so on
Disadvantages:
Overloading of multiple parameter constructors is not supported
Although it saves the trouble of manually creating the getter/setter method, it greatly reduces the readability and integrity of the source code, and reduces the comfort of reading the source code.
Summary
Although Lombok has many advantages, Lombok is more similar to an IDE plug-in, and the project also needs to rely on the corresponding jar package. Lombok relies on the jar package because it is compiled with its annotations, so why is it similar to plug-ins? Because when in use, eclipse or IntelliJ IDEA need to install the corresponding plug-in, when the compiler compiles by operating AST (abstract syntax tree) to change bytecode generation, which means that it is changing the java syntax. It is not a run-time feature like spring's dependency injection or mybatis's ORM, but a compile-time feature. What I feel most uncomfortable about here is the dependence on plug-ins! Because Lombok only saves some of the trouble of manually generated code, but IDE has keyboard shortcuts to help generate getter/setter and other methods, which is also very convenient.
This is the end of the content of "the function and use of Lombok". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.