Introduction and usage of 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. For example, we create a new class, and then write a few fields in it, and then usually we need to Manually create get and set methods, constructors, etc. The role of lombok is to save us the trouble of manually creating these codes. It can automatically generate these methods for us when we compile the source code.

Although some people may say that IDEs have built-in functions to automatically generate these methods, but 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;  
        public NonNullExample(@NonNull Person person) {
        super("Hello");
        this.name = person.getName();
    }
}

Without 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.

Use lombok:

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);
        }
    }
}

Without lombok:

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();
            }
        }
    }
}

@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;
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    protected void setName(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.


Guess you like

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