-lombok Plugin Usage guide for developing gadgets

Source: Internet
Author: User
Lombok Introduction

Lombok is summer vacation came to the company internship found a very easy to use gadgets, just see when it feels very stunning, there is a feeling of brief encounter, after a period of time feel really good, so hereby to recommend.

Lombok's official address: https://projectlombok.org/

Lombok's GitHub Address: Https://github.com/rzwitserloot/lombok

So what is Lombok, Lombok is a simple annotated form to help us simplify the removal of some of the necessary but very bloated Java code tools, simply, for example, we created a new class, and then wrote a few fields in it, And then normally we need to manually build getter and setter methods Ah, constructors ah and so on, the role of Lombok is to save us the trouble of manually creating these code, it can automatically help us generate these methods when we compile the source.

Lombok can achieve the effect is in the source code does not need to write some common methods, but in the compilation generated bytecode file will help us to generate these methods, this is the magical role of Lombok.

While some might say that the IDE has the ability to automatically generate these methods, using Lombok will make your code look more concise and easier to write. Lombok Installation

Lombok installation is not the same as the general reference jar package, you can download the latest Jar package on the official website and import it into the project.

Maven Add Dependency

<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        < artifactid>lombok</artifactid>
        <version>1.16.10</version>
    </dependency>
</dependencies>

Intellij idea development needs to install Lombok plugin, while setting up Setting, Compiler, Annotation processors, Enable Annotation process ing tick. Lombok Use

Lombok the use of the process is mainly by the annotation function, the official web document has all the annotations, here do not list, only a few of the more commonly used. @NonNull: can help us avoid null pointers.

Using Lombok:

Import Lombok. nonnull;
    public class Nonnullexample extends Something {
        private String name;  
        Public nonnullexample (@NonNull person person) {
        super ("Hello");
        THIS.name = Person.getname ();
    }
}

Do not use Lombok:

public class Nonnullexample extends Something {
    private String name;  
    Public nonnullexample (@NonNull person person) {
        super ("Hello");
        if (person = = null) {
            throw new NullPointerException ("person");
        }
        THIS.name = Person.getname ();
    }
}
@Cleanup: Automatically help us call the Close () method.

Using Lombok:

Import Lombok. Cleanup;
Import java.io.*;
public class Cleanupexample {public
    static void Main (string[] args) throws IOException {
        @Cleanup inputstream i n = new FileInputStream (args[0]);
        @Cleanup OutputStream out = new FileOutputStream (args[1]);
        Byte[] B = new byte[10000];
        while (true) {
            int r = in.read (b);
            if (r = =-1) break;
            Out.write (b, 0, R);}}}

Do not use Lombok:

Import java.io.*;
    public class Cleanupexample {public
        static void Main (string[] args) throws IOException {
            InputStream in = new Fil Einputstream (Args[0]);
            try {
                OutputStream out = new FileOutputStream (args[1]);
                try {
                    byte[] b = new byte[10000];
                    while (true) {
                    int r = in.read (b);
                    if (r = =-1) break;
                    Out.write (b, 0, R);
                    }
                } Finally {
                    if (out! = null) {
                        out.close ()}}}
            finally {
                if (in! = null) {
                in.close () ;
            }
        }
    }
}
@Getter/@Setter: Automatic generation of Getter/setter methods

Using Lombok:

    Import Lombok. AccessLevel;
    Import Lombok. Getter;
    Import Lombok. Setter;
    public class Gettersetterexample {
        @Getter @Setter private int: age = ten;
        @Setter (accesslevel.protected) private String name;
    

Do not use Lombok:

public class Gettersetterexample {
    private int: age = ten;
    private String name;
    public int getage () {
        

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.