Lombok installation, getting started-eliminating lengthy Java code (rpm)

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 on classes; provides getting and setting methods for all properties of a class, plus equals, canequal, hashcode, toString methods
@Setter: Annotations are on attributes; provides setting method for attributes
@Getter: Annotations are on attributes; provides getting method for attributes
@Log4j: Annotations on a class; provides a log4j log object with a property named log for the class
@NoArgsConstructor: Annotations on a class; provides an Argument-free construction method for a class
@AllArgsConstructor : Annotations are on a class; provides a method for constructing a full parameter for a class

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-eliminating lengthy Java code (rpm)

Related Article

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.