The Lombok of simplifying and simplifying

Source: Internet
Author: User

As we all know, the Java language is verbose, write long will find many places of code look very redundant. lombokprovides a solution for simplifying Java code that is annotated to generate templated code. The most typical is the constructor that generates the class, the Getter/setter,tostring method, and so on.

Annotations are a feature that accompanies the JDK5 release, which is divided into source,class (compile stage) and runtime, depending on the life cycle. Lombok provides annotations that have a lifetime of class, meaning that annotations that are Lombok are processed by the compiler (JAVAC) and translated into corresponding bytecode. To put it bluntly, Lombok is like a professional writer who helps you write code by definition. We write the code through the annotations provided by Lombok, define the outline of the article, the rest to Lombok, he will help us to write the article (code) of the Flesh and blood (constructor, Getter,setter, etc.).

Here are some of the annotations and effects provided by Lombok:

    • @Getter/ @Setter : Create getters and setters based on fields
    • @EqualsAndHashCode: Achieve equals() andhashcode()
    • @ToString: ImplementtoString()
    • @Data: Features with four annotations above
    • @Cleanup: Closes an open stream
    • @Synchronized: Lock on the object
    • @SneakyThrows: Catching unhandled exceptions

Here's a code that uses Lombok annotations:

import lombok.Data;import java.util.Date;@Datapublic class Person {    private String name;    private Date birthday;    private int gender;}

Compile into bytecode first:

javac -cp lombok-1.18.2.jar Person.java

Look at the contents of the generated bytecode by JAVAP:

~ lombok git:(master) ✗ javap Person.classCompiled from "Person.java"public class cc.databus.lombok.Person {  // lombok生成的构造函数  public cc.databus.lombok.Person();  // 生成的getter和setter  public java.lang.String getName();  public java.util.Date getBirthday();  public int getGender();  public void setName(java.lang.String);  public void setBirthday(java.util.Date);  public void setGender(int);  // 生成的equals和hashcode  public boolean equals(java.lang.Object);  protected boolean canEqual(java.lang.Object);  public int hashCode();  // 生成的toString  public java.lang.String toString();}

This is a simple example, and the more detailed use can refer to the list of annotations provided by Lombok.

If you are developing under Intelij, you will need to install a plugin for Lombok, otherwise you will not be able to run and debug under Intelij:

    1. Open File > Settings > Plugins
    2. Choose Browse repositories ...
    3. Search Lombok Plugin
    4. Installing plugins
    5. Restart IntelliJ idea
References
    1. List of annotations provided by Lombok

The article is posted on my personal blog, welcome to shoot bricks. Portal: The Lombok of Simplicity

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.