Idea using the Lombok plugin

Source: Internet
Author: User
background

We usually define a lot of javabean in the development process, and then use the IDE to generate the constructor, getter, setter, equals, Hashcode, tostring method of its properties, when you want to change a property, such as name, type, etc. Need to recreate the methods mentioned above, is there any way in Java to avoid this repetitive work? The answer is yes, let's take a look at the picture below, the right side is a simple javabean, only two attributes are defined, the @data is added to the class, and as you can see from the structure diagram on the left, the method mentioned above has been automatically generated.
Lombok Introduction

Lombok is a simple annotation form to help us simplify the elimination of some of the necessary but very bloated Java code tools, by using the corresponding annotations, you can compile the source code when the corresponding method. Official address: Https://projectlombok.org/,github Address: Https://github.com/rzwitserloot/lombok. Lombok Use Idea to add Lombok plug-in, File-> Setting-> Plugins Search Lombok Plugin, click on Install, after the installation is completed restart idea

add a configuration to the pom.xml of the MAVEN project

<dependency>
        <groupId>org.projectlombok</groupId>
        <artifactid>lombok</ artifactid>
        <version>1.16.6</version>
</dependency>
Introduction to Annotations

Here are just a few commonly used annotations, see https://projectlombok.org/features/index.html for more. @Getter/@Setter

Can be on a class and on a property, placed on a class, generate a Getter/setter method on all Non-static (Non-static) properties, and put on a property to generate a Getter/setter method on the property. And you can specify the access level of the Getter/setter method. @EqualsAndHashCode

By default, all non-transient (non-transient) and Non-static (non-static) fields are used to generate the Equals and Hascode methods, and you can specify which properties are used specifically. @ToString

Generates the ToString method, which, by default, outputs the class name, all properties, and the attributes are output in sequential order, separated by commas. @NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor

The parameterless constructor, the partial parameter constructor, the full parameter constructor, when we need to overload multiple constructors, Lombok is powerless. @Data

@ToString, @EqualsAndHashCode, the @getter of all attributes, the combination of @setter and @requiredargsconstructor of all non-final attributes, usually we use this annotation to be sufficient. Lombok Principle

Once you understand the simple use, you should now be more curious about how it is implemented. The whole use of the process, only need to use annotations, do not need to do additional work, the mystery should be in the interpretation of the annotation. JDK5 introduces annotations and provides two ways of parsing. run-time parsing

Annotations that can be parsed at run time, the @retention must be set to runtime so that the annotation can be obtained by reflection. An interface annotatedelement is provided in the Java.lang.reflect reflection package, which defines several methods for obtaining annotation information, Class, constructor, Field, method, package, and so on, which are implemented by the interface. Most developers should be familiar with this parsing approach.

Boolean isannotationpresent (class< extends annotation> annotationclass);
<t extends annotation> T getannotation (class<t> annotationclass);
Annotation[] Getannotations ();
Annotation[] Getdeclaredannotations ();
1 2 3 4 1 2 3 4 compile-time parsing

There are two mechanisms for compile-time parsing, and many articles on the web are confusing them, describing them separately. Annotation processing Tool

Apt from JDK5, JDK7 has been marked as expired, not recommended, JDK8 has been completely deleted, since JDK6 start, you can use the Pluggable Annotation processing API to replace it, APT was replaced with 2 main reasons: APIs are not integrated into the Javac under the Com.sun.mirror package and require additional operation

More introduction of apt can be found here. pluggable Annotation processing API

JSR 269, since JDK6 joined, as APT's alternative, it solves APT's two problems, javac in execution will invoke the implementation of the API program, so we can do some enhancements to the compiler, then Javac execution process is as follows:

Lombok is to use this way to achieve, interested words can go to see its Lombok source code, corresponding to the implementation of annotations in the handlexxx, such as @getter annotation implementation is handlegetter.handle (). There are other libraries that are implemented in this way, such as Google Auto, Dagger, and so on. Lombok problem cannot support overloaded references for multiple parameter constructors http://blog.csdn.net/ghsau/article/details/52334762

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.