How to use Lombok now

Introduction

When I imported the source for a project, I got a lot of build errors. There are a lot of errors that make XXXBuilder not exist. If you look at build.gradle, you can see that it uses lombok's library, but in fact, you 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 the STS folder

image.png

Press the Select button

image.png

Click Install / update

image.png

Installation is complete.

Idea

File ⇒ Settings ⇒ Plugins ⇒ lombok Enter and search

image.png

If it already exists in Installed, you do not need to install it.

Select on the MarketPlace tab and enter lombok to search

image.png

Press the Install button

image.png

Press the Restart IDE button to complete.

VSCode You can avoid build errors with an 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 installing, restart VS Code just in case

How to use Lombok

Lombok features https://projectlombok.org/features/all

Added 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's easy to understand, but getter and setter methods are automatically generated.

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

Only the excluded age field is unused.

@EqualsAndHashCode image.png

Three methods, hashCode, equals and canEqual, are generated.

@NoArgsConstructor, @RequiredArgsConstructor, @AllArgsConstructor image.png

@Value image.png

@Builder image.png

@Data image.png

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

@Log image.png

@Cleanup Annotation that automatically releases resources. This is convenient because you can omit the resource close process 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 and so on.

Using these annotations can reduce the amount of bean classes and improve maintenance.

that's all

Recommended Posts

How to use Lombok now
How to use @Builder (Lombok)
How to use Lombok in Spring
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
How to use Twitter4J
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to use identity
How to use hashes
How to use JUnit 5
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Java] How to use Map
How to use Chain API
[Java] How to use Map
How to use Priority Queuing
[Rails] How to use enum
How to use java Optional
How to use Ruby return
[Rails] How to use enum
How to use java class
How to use Swift UIScrollView
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use rails join
Ruby: How to use cookies
How to use Eclipse Debug_Shell
How to use Apache POI
[Rails] How to use validation
How to use Java variables
[Rails] How to use "kaminari"
How to use GC Viewer
[Java] How to use Optional ①
[Creating] How to use JUnit
[Rails] How to use Scope
How to use the link_to method
[Rails] How to use gem "devise"
How to use StringBurrer and Arrays.toString.
How to use arrays (personal memorandum)
How to use Java HttpClient (Get)
How to use scope (JSP & Servlet)
How to use the include? method
[Rails] How to use devise (Note)
How to use the form_with method
[Rails] How to use flash messages
How to use EventBus3 and ThreadMode
How to use binding.pry [53 days left]
[Java] How to use join method