This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021

Lombok is a controversial plug-in, and there’s no denying its magic. It’s a lot of lazy work, but in addition to reducing the amount of code, when a module uses Lombok, the rest of the code that relies on that module needs to introduce Lombok dependencies. Use it in practice

Introduction to the

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java. Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

Lombok is a Java utility that helps developers eliminate Java verbosity, especially for simple Java objects (POJOs). It does this through annotations. By implementing Lombok in a development environment, developers can save a lot of time building methods like hashCode() and equals() and sorting various accessors and mutators.


The installation

IDEA Install Lombok plug-in

1, Setting → Plugin, search Lombok installation

Lombok is already built into IDEA 2020.3.

2. Import Lombok dependencies

 <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.1812.</version>
    <scope>provided</scope>
 </dependency>
Copy the code

Eclipse installs the Lombok plug-in

1, the official website to download the jar package: projectlombok.org/download

2. Double-click the lombak.jar package

3. Click Install/Update

4. Click Quit Installer to complete the installation

5. After the installation is complete, check whether there is a lombok.jar package in the Eclipse installation path and whether the following content has been added to the eclipse.ini configuration file: -javaagent:D:\eclipse-jee-2021-03-R-win32-x86_64\eclipse\lombok.jar


Lombok often uses annotations

Lombok annotations document

@NonNull

@Cleanup

@Getter and @Setter

@AllArgsConstructor

@RequiredArgsConstructor

@NoArgsConstructor

@ToString

@EqualsAndHashCode

@Data

@NonNull

When used before an argument to a member method or constructor, a non-null check is automatically generated for the argument, and a null-pointer exception is thrown if the argument is null


import lombok.NonNull;

public class NonNullExample extends Something {
    private String name;

    public NonNullExample(@NonNull Person person) {
        this.name = person.getName(); }}Copy the code

The equivalent of


import lombok.NonNull;

public class NonNullExample extends Something {
    private String name;

    public NonNullExample(@NonNull Person person) {
        if (person == null) {
            throw new NullPointerException("person is marked @NonNull but is null");
        }
        this.name = person.getName(); }}Copy the code

@Cleanup

Used in front of a variable to ensure that the resource it represents is automatically closed, by default calling the close() method of the resource

import lombok.Cleanup;
import java.io.*;

public class CleanupExample {
    public static void main(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); }}}Copy the code

The equivalent of


import java.io.*;

public class CleanupExample {
    public static void main(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(); }}}}Copy the code

@Getter and @Setter

In front of a member variable, the corresponding get and set methods are generated for the member variable. You can also specify access modifiers for the generated methods, which default to public

On a class, you can generate corresponding GET and set methods for all non-static member variables in the class.

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

public class GetterSetterExample {

    @Getter 
    @Setter 
    private int age;

    @Setter(AccessLevel.PROTECTED)
    private String name;

}
Copy the code

The equivalent of

public class GetterSetterExample {

    private int age;

    private String name;

    public int getAge(a) {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    protected void setName(String name) {
        this.name = name; }}Copy the code

@AllArgsConstructor / @NoArgsConstructor

Used on a class that generates a full-parameter constructor and a no-parameter constructor for that class

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private String password;
}
Copy the code

The equivalent of

public class User {
    private int id;
    private String name;
    private String password;

    public User(a) {}public User(int id, String name, String password) {
        this.id = id;
        this.name = name;
        this.password = password; }}Copy the code

@ToString

Used on a class to generate the toString() method

  • @toString. Exclude Excludes specified fields
  • @toString. Include contains the specified fields
import lombok.ToString;

@ToString
public class User {

    @ToString.Exclude
    private int id;
    private String name;

}
Copy the code

The equivalent of

public class User {
    private int id;
    private String name;

    public User(a) {}public String toString(a) {
        return "User(name=" + this.name + ")"; }}Copy the code

@EqualsAndHashCode

Generate equals() and hashCode() methods, as well as canEqual() methods to determine whether an object is an instance of the current class, using only non-static and non-transient member variables of the class

  • @ EqualsAndHashCode. Exclude excluding specific fields
  • @ EqualsAndHashCode. Include contains the specified field
import lombok.EqualsAndHashCode;

@EqualsAndHashCode
public class User {

    private  int id;
    private String name;

}
Copy the code

The equivalent of

public class User {
    private int id;
    private String name;

    public User(a) {}public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if(! (oinstanceof User)) {
            return false;
        } else {
            User other = (User)o;
            if(! other.canEqual(this)) {
                return false;
            } else if (this.id ! = other.id) {return false;
            } else {
                Object this$name = this.name;
                Object other$name = other.name;
                if (this$name == null) {
                    if(other$name ! =null) {
                        return false; }}else if (!this$name.equals(other$name)) {
                    return false;
                }

                return true; }}}protected boolean canEqual(Object other) {
        return other instanceof User;
    }

    public int hashCode(a) {
        int PRIME = true;
        int result = 1;
        int result = result * 59 + this.id;
        Object $name = this.name;
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        returnresult; }}Copy the code

@Data

Contains @getter and @setter, @toString, @equalSandHashCode, @noargsConstructor

import lombok.Data;

@Data
public class User {

    private int id;
    private String name;

}
Copy the code

The equivalent of

public class User {
    private int id;
    private String name;

    public User(a) {}public int getId(a) {
        return this.id;
    }

    public String getName(a) {
        return this.name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean equals(Object o) {
        if (o == this) {
            return true;
        } else if(! (oinstanceof User)) {
            return false;
        } else {
            User other = (User)o;
            if(! other.canEqual(this)) {
                return false;
            } else if (this.getId() ! = other.getId()) {return false;
            } else {
                Object this$name = this.getName();
                Object other$name = other.getName();
                if (this$name == null) {
                    if(other$name ! =null) {
                        return false; }}else if (!this$name.equals(other$name)) {
                    return false;
                }

                return true; }}}protected boolean canEqual(Object other) {
        return other instanceof User;
    }

    public int hashCode(a) {
        int PRIME = true;
        int result = 1;
        int result = result * 59 + this.getId();
        Object $name = this.getName();
        result = result * 59 + ($name == null ? 43 : $name.hashCode());
        return result;
    }

    public String toString(a) {
        return "User(id=" + this.getId() + ", name=" + this.getName() + ")"; }}Copy the code