Automatically generate Java code with Lombok code to save development time

Source: Internet
Author: User
Tags exception handling int size modifier log4j

First, what is Lombok?

Lombok is a small code generation tool. Official website: http://projectlombok.org/

The main features of Lombok are: automatic generation of default Getter/setter methods, automated resource management (through @cleanup annotations) and annotation-driven exception handling. Currently widely used in foreign countries.

Lombok It's like jquery, the goal is to get programmers to write less code, and to improve some of the original syntax. Lombok can do that. Neither with annotations process, nor with reflection. But directly black to the compilation process. So there is no effect on operational efficiency, we can validate it by decompile the class file.

Second, why the project to introduce Lombok

I think this is mainly the following three points:

1. Improve development efficiency

2. Make the code intuitive, concise, clear, reduce a large number of redundant code (generally can save 60%-70% more code)

3. Significantly reduce the cost of late maintenance

Third, how to use the Lombok

Currently we are mainly using eclipse development, so take eclipse as an example to demonstrate:

1. Installation of Lombok on Eclipse

Java-jar Lombok.jar

To install the Eclipse plug-in

2. Use of Lombok

The Lombok feature is to create some code based on annotation to reduce the number of duplicate code, which provides the following annotation:

@Getter and @setter: Creating getter and Setter for attributes

@EqualsAndHashCode: Implementing the Equals () method and the Hashcode () method

@ToString: Implementing the ToString () method

@Data: Above 3 annotation and, will create Getter Setters equals Hashcode and ToString

@Cleanup: Close the stream

@Synchronized: Object synchronization

@SneakyThrows: Throwing an exception

@Log4j: log4j Log Declaration

Now, let's look at some examples:

@Getter @Setter

The original wording:

The writing in Lombok

When generating getter/setter methods, Lombok comply with traditional standards. All of these method names begin with a GET or set and the property names are uppercase. Of course, if the attribute is a Boolean, the exception is the case. In this case, the getter starts with IS, not get. This is a standard practice for Java beans;

Hashcode when JavaBean output number;

ToString output class and a printed list of each property;

Access permissions for the Modifier getter method

@Getter (accesslevel.protected) to modify the access rights of the Getter method

@Setter (accesslevel.protected) modifier Setter Method access rights

@Getter (accesslevel.protected) private int size;

@ToString

To exclude a particular field from overriding the ToString method:

@ToString (exclude= "Color")

Public @Data class Lure {
private String name;
private int size;
private String color;
private String style;
}

@EqualsAndHashCode

Wait a minute...... It is very convenient to use.

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.