[Java] How to use Lombok now

1 minute read

Introduction

There were lots of build errors when I imported the source for a project. There are many errors where XXXBuilder does not exist. Looking at build.gradle, I use the library of lombok, but in fact, I need to install the Lombok plugin in the IDE as well.

Lombok

https://projectlombok.org/

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.

Spring Tool Suite

lombok download

https://projectlombok.org/download

Double click on ##lombok JAR image.png

Click ##Specify location to select STS folder image.png

Press the Select button

image.png

Click ##Install / update image.png

Installation is complete.

Idea

image.png

You do not need to install it if it already exists in Installed.

image.png

Press the Install button

image.png

Press the Restart IDE button to complete.

VSCode

You can avoid build errors by using the extension called Lombok Annotations Support for VS Code. https://marketplace.visualstudio.com/items?itemName=GabrielBB.vscode-lombok

Install “Lombok Annotations Support for VS Code”

image.png

Press the Install button

After installation, restart VS Code just in case

How to use #Lombok

Lombok features

https://projectlombok.org/features/all

Add lombok library to build.gradle

build.gradle


    // https://mvnrepository.com/artifact/org.projectlombok/lombok
    compile group:'org.projectlombok', name:'lombok', version: '1.18.12'

##@Getter/@Setter image.png

It is easy to understand, but getter and setter methods are automatically generated.

##@ToString The toString method is defined and controlled like @ToString(exclude = "age", callSuper = false) image.png

Only the excluded age field is unused.

##@EqualsAndHashCode image.png

hashCode,equals,canEqual Three methods are generated.

##@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor image.png

##@Value image.png

##@Builder image.png

##@Data image.png

As in the above example, it is a summary of the following annotations.

  • @ToString
  • @EqualsAndHashCode
  • @Getter for all non-final fields
  • @Setter for all non-final fields
  • @RequiredArgsConstructor

##@Log image.png

##@Cleanup An annotation that automatically releases resources. This is convenient because you can omit the close processing of resources yourself.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import lombok.Cleanup;

public class TestMain {

public static void main(String[] args) throws Exception {
@Cleanup
InputStream in = new FileInputStream(args[0]);
@Cleanup
OutputStream out = new FileOutputStream(args[1]);

byte[] b = new byte[1024];
while (true) {
int r = in.read(b);
if (r == -1) {
break;
}
out.write(b, 0, r);
}
}

}

Hakamo val, var, @NonNull, @Getter(lazy=true), @Helper, @Slf4j etc.

Using these annotations reduces the amount of bean classes and improves maintenance.

that’s all