Lombok installation, getting started-eliminate lengthy Java code

Source: Internet
Author: User
Tags log4j

Objective:
Visit the open source community when the unintentional discovery, with a period of time, think can also, hereby recommended.
LombokProvides a simple form of annotations to help us simplify the removal of some of the necessary but bloated Java code. In particular, compared to POJO, Light said not to do is not my style, first to see it.

Lombok's official website: http://projectlombok.org/

Lombok actually here I introduced, open a joke, in fact, the official online Lombok 3 minutes 49 seconds of video explanation, Inside said is also very clear, and there are documents to reference.
I don't talk too much here, let's take a look.installation of Lombok, in fact, this official website video has also talked about

Lombok Installation
Using Lombok is required, and if not installed, the IDE cannot parse the Lombok annotations. First download the latest version of the JAR package on the official website, it is now 0.11.2 version, I use 0.11.0
The first time I used the download is the latest version, that is, I now use 0.11.0, up to now has updated two versions, updated very fast ah ...

1. Double-click the downloaded JAR package to install Lombok
I chose to install this way when prompted not to find any IDE, so I did not install successfully, I was installed manually. If you want to install this way, please refer to the website's video.

2.eclipse/myeclipse Manual Installation Lombok
1. Copy the Lombok.jar to the folder directory where the Myeclipse.ini/eclipse.ini resides
2. Open Eclipse.ini/myeclipse.ini, insert the following two lines on the last side and save:
-xbootclasspath/a:lombok.jar
-javaagent:lombok.jar
3. Restart Eclipse/myeclipse

Lombok notes:
Lombok offers a few annotations, and can refer to the official video commentary and official documentation.
Lombok Annotations Online Help documentation: Http://projectlombok.org/features/index.
Here are a few of the Lombok annotations I commonly use:
@Data: Annotations are on classes, provide getting and setting methods for all properties of a class, and also provide equals, canequal, Hashcode, toString methods
@Setter: Annotations on attributes; providing setting methods for attributes
@Getter: Annotations on attributes; providing getting methods for attributes
@Log4j: Annotations are on a class, and a log4j log object with a property named log is provided for the class
@NoArgsConstructor: Annotations on a class, providing an argument-free construction method for a class
@AllArgsConstructor: Annotations are on a class, and a method for constructing a class with full parameters

Here is a simple example
1. Scenarios that do not use Lombok

1
2public class Person{
3
4 private String ID;
5 private String name;
6 Private String identity;
7 private Logger log = Logger.getlogger (Person.class);
8
9 Public person (){
10
11}
12
Public person (string ID, string name, String identity){
This.id = ID;
THIS.name = name;
This.identity = identity;
17}
18
Public String getId (){
return ID;
21}
22
All public String getName (){
return name;
25}
26
Public String getidentity (){
return identity;
29}
30
public void SetId (String ID){
This.id = ID;
33}
34
public void SetName (String name) {
THIS.name = name;
37}
38
setidentity public void (String identity) {
This.identity = identity;
41}
42}
+


2. Scenarios for using Lombok

1
2@data
3@log4j
4@noargsconstructor
5@allargsconstructor
6public class Person{
7
8 Private String ID;
9 private String name;
Ten private String identity;
11
12}
-


The above two Java classes, from the point of view, their effect is the same, compared with, it is clear that the use of Lombok is much more concise, especially in the case of the class of the property more,
It also avoids the low-level errors in modifying the name of the field when you forget to modify the method name. Finally, it is important to remember to import the Lombok.jar package to the project when using Lombok annotations


Ii. Methods of Use
The method of using the Lombok project is simple and is divided into four steps:
1) Add @data annotations on classes that need to automatically generate getter and setter methods
2) Add the Lombok.jar package to the compiled Classpath
3) Compile the source code with the Lombok-enabled compilation tool (for Lombok support, see "Iv. Compiler tools supporting Lombok")
4) The getter and setter methods are automatically generated in the compiled bytecode file


Three, the principle analysis
The following is an analysis of the principles that Lombok can work with, as an example of Oracle's Javac compiler tool.
Since Java 6, JAVAC has supported the "JSR 269 pluggable Annotation Processing API" specification, which can be invoked as soon as the program implements the API, while the JAVAC is running.
For example, there is now a "JSR 269 API" program A, then use Javac compile the source code when the specific process is as follows:
1) Javac The source code to generate an abstract syntax tree (AST)
2) A program that implements the "JSR 269 API" is called during operation
3) At this point a program can complete its own logic, including modifying the first step to get the abstract syntax tree (AST)
4) Javac using the modified abstract syntax tree (AST) to generate bytecode files
The detailed flowchart is as follows:



Lombok is essentially a program that implements the "JSR 269 API". In the process of using JAVAC, it has the following specific processes:
1) Javac The source code to generate an abstract syntax tree (AST)
2) The Lombok program that implements the "JSR 269 API" is called during operation
3) At this point Lombok processes the AST obtained from the first step, finds the syntax tree (AST) corresponding to the class where the @data annotation is located, and modifies the syntax tree (AST) to add the corresponding tree nodes defined by the getter and setter methods
4) Javac using the modified abstract syntax tree (AST) to generate bytecode files


Iv. support for Lombok compiler tools
1) from the "Three, the principle of analysis," the Oracle Javac directly support Lombok
2) Common project management tools MAVEN uses Java compilation tools from configured third-party tools, and if we configure this third-party tool for Oracle Javac, then MAVEN will support Lombok directly.
3) Intellij idea configuration of the compiler tool for Oracle Javac, but also directly support the Lombok.
4) instead of using Oracle Javac, this compiler tool uses Eclipse Compiler for Java (ECJ). To make ECJ support Lombok, you have to set the Add the following two lines of settings to the Eclipse.ini file in the Eclipse program directory:
-javaagent:[lombok.jar location of the path]
-xbootclasspath/a:[lombok.jar location of the path]


V. OTHER ISSUES
Now use IntelliJ idea as the IDE for the Java project, configuring Oracle Javac as the compilation tool.
There is now a Class A, with some fields, no setter and getter methods for creating them, using Lombok's @data annotations, and a class B, which invokes the setter and getter methods for the corresponding fields of Class A instances
Compiling projects of Class A and Class B without error, because the setter and getter methods for the corresponding fields exist in the resulting class A bytecode file
However, the IDE finds that the setter and getter methods for Class A instances used in Class B source code cannot be defined in Class A source code, which the IDE considers to be an error
To resolve this error that is not a real error, you can download and install the "Lombok plugin" in IntelliJ idea.


Vi. the sins of the Lombok
Using Lombok can eliminate the hassle of manually creating setter and getter methods, but it greatly reduces the readability and integrity of source code files and reduces the comfort of reading source code.

Lombok installation, getting started-eliminate lengthy Java code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.