Introduction and usage of Lombok (transfer)

Original link

Introduction to lombok

lombok is a very useful gadget that I found when I came to the company for an internship during the summer vacation. When I first saw it, I was very surprised. to recommend.

Official address of lombok: https://projectlombok.org/

Github address of lombok: https://github.com/rzwitserloot/lombok

So what exactly is lombok? Lombok is a tool that can help us simplify and eliminate some must-have but bloated Java code in the form of simple annotations. In simple terms, for example, we create a new class, and then write in it A few fields, and then usually we need to manually create getter and setter methods, constructors, etc. The role of lombok is to save us the trouble of manually creating these codes, it can be used when we compile the source code. Automatically generate these methods for us.

The effect that lombok can achieve is that there is no need to write some general methods in the source code, but it will help us generate these methods in the compiled bytecode file, which is the magical effect of lombok.

Although some people may say that IDEs have built-in functions to automatically generate these methods, using lombok will make your code look more concise and easier to write.

lombok installation

The installation of lombok is no different from the general reference jar package. You can download the latest jar package from the official website and import it into the project.

Maven adds dependencies

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

For Intellij idea development, you need to install the Lombok plugin, and set the Setting -> Compiler -> Annotation Processors -> Enable annotation processing check.

lombok use

The use of lombok mainly relies on annotations. There are all annotations in the documents on the official website. They are not listed here, but only a few of them are more commonly used.

@NonNull: can help us avoid null pointers.

Use lombok:

import lombok.NonNull;
    public class NonNullExample extends Something {
        private String name;  
        publicNonNullExample(@NonNull Person person) {
        super("Hello");
        this.name = person.getName();
    }
}

Without lombok:

public class NonNullExample extends Something {
    private String name;  
    publicNonNullExample(@NonNull Person person) {
        super("Hello");
        if (person == null) {
            throw new NullPointerException("person");
        }
        this.name = person.getName();
    }
}

@Cleanup: automatically help us call the close()method.

Use lombok:

import lombok.Cleanup;
import java.io.*;
public class CleanupExample {
    publicstaticvoidmain(String[] args) throws IOException {
        @Cleanup InputStream in = 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);
        }
    }
}

Without lombok:

import java.io.*;
    public class CleanupExample {
        publicstaticvoidmain(String[] args) throws IOException {
            InputStream in = new FileInputStream(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: Automatically generate Getter/Setter methods

Use lombok:

    import lombok.AccessLevel;
    import lombok.Getter;
    import lombok.Setter;
    public class GetterSetterExample {
        @Getter @Setter private int age = 10;
        @Setter(AccessLevel.PROTECTED) private String name;
    }

Without lombok:

public class GetterSetterExample {
    private int age = 10;
    private String name;
    publicintgetAge() {
        return age;
    }
    publicvoidsetAge(int age) {
        this.age = age;
    }
    protectedvoidsetName(String name) {
        this.name = name;
    }
}

@NoArgsConstructor: Automatically generate a parameterless constructor.

@AllArgsConstructor: Automatically generate a full-parameter constructor.

@Data:  Automatically add @ToString , @EqualsAndHashCode , @Getter methods for all fields , @Setter for non-final fields , and @RequiredArgsConstructor !

There are other annotations such as automatically generating log objects, etc., which can be found on the official website, so I will not list them one by one.

Official documentation https://projectlombok.org/features/index.html

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326766634&siteId=291194637