In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Java's Lombok". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Java's Lombok.
1) introduce the corresponding maven package:
Org.projectlombok lombok 1.16.18 provided
Lombok's scope=provided, which means that it only takes effect during the compilation phase and does not need to be entered into the package. As a matter of fact, Lombok correctly compiles the Java file with Lombok annotations into a complete Class file at compile time.
2) add IDE tool's support for Lombok:
Lombok support is introduced into IDEA as follows:
Click the File-- Settings settings interface to install the Lombok plug-in:
Click the File-- Settings settings page to open Annocation Processors:
This item is turned on to make Lombok annotations work at compile time.
Eclipse Lombok plug-in installation can be Baidu, but also relatively simple, it is worth mentioning that because Eclipse built-in compiler is not Oracle javac, but eclipse's own implementation of Eclipse Compiler for Java (ECJ). For ECJ to support Lombok, you need to add the following two items to the eclipse.ini configuration file:
-Xbootclasspath/a: [path where lombok.jar is located]
-javaagent: [path where lombok.jar is located]
3) the principle of Lombok implementation:
Since Java 6, javac has supported the "JSR 269 Pluggable Annotation Processing API" specification, and as long as the program implements the API, it can be called when the javac is running.
Lombok is a program that implements "JSR 269API". In the process of using javac, the specific process by which it works is as follows:
Javac analyzes the source code to generate an abstract grammar tree (AST)
Call the Lombok program that implements JSR 269 in the process of javac compilation
At this point, Lombok processes the AST obtained in the first step, finds the grammar tree (AST) corresponding to the class where the Lombok comments are located, and then modifies the grammar tree (AST) to add the corresponding tree nodes defined by the Lombok comments.
Javac uses the modified abstract grammar tree (AST) to generate bytecode files
4) use of Lombok annotations:
Common notes for the POJO class:
Getter/@Setter: generate the getter/setter method of all member variables on the action class and the getter/setter method of the member variable on the member variable. You can set access permissions and whether to load lazily, etc.
Package com.trace; import lombok.AccessLevel; import lombok.Getter; import lombok.Setter; / * Created by Trace on 2018-5-19. * DESC: test class * / @ SuppressWarnings ("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;}}
In the Structure view, you can see that methods such as getter/setter have been generated:
The compiled code is as follows: [this is also the boilerplate code that traditional Java programming needs to write]
/ 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;}
ToString: acts on the class and overrides the default toString () method. You can limit the display of certain fields by the of attribute and exclude some fields by the exclude attribute.
@ EqualsAndHashCode: works on classes, overriding the default equals and hashCode
@ NonNull: mainly used in member variables and parameters. The identity cannot be empty, otherwise a null pointer exception is thrown.
@ NoArgsConstructor, @ RequiredArgsConstructor, @ AllArgsConstructor: acts on the class to generate the constructor. There are staticName, access and other attributes.
Once the staticName property is set, the instance is generated statically, and the access property can limit access.
@ NoArgsConstructor: generate a parameterless constructor
RequiredArgsConstructor: generates a constructor that contains member variables of final and @ NonNull annotations
AllArgsConstructor: generates an all-parameter constructor.
Compiled result:
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;}
Data: acts on a class and is a collection of the following annotations: @ ToString @ EqualsAndHashCode @ Getter @ Setter @ RequiredArgsConstructor
@ Builder: acts on a class, transforming the class into a builder mode
Log: acts on the class to generate log variables. There are different notes for different log implementation products:
Other important notes:
@ Cleanup: automatically closes resources, which is valid for objects that implement java.io.Closeable interface, such as typical IO stream objects
The compiled result is as follows:
Is it too concise?
@ SneakyThrows: the checked exception can be caught and thrown. The above main method can be overridden as follows:
Synchronized: at the method level, you can replace the synchronize keyword or lock lock, which is of little use.
At this point, I believe you have a deeper understanding of "how to use Java's Lombok". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.