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 identity
How to use org.immutables
How to use java.util.stream.Collector
How to use VisualVM
How to use Map
[Creating] How to use JUnit
[Rails] How to use Scope
[Introduction to Rails] How to use render
[Processing × Java] How to use arrays
How to use Ruby inject method
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
Studying how to use the constructor (java)
How to use environment variables in RubyOnRails
Ruby: CSV :: How to use Table Note
[Rails] How to use helper method, confimartion
How to use credentials.yml.enc introduced in Rails 5.2
[Java] Learn how to use Optional correctly
How to use @Builder and @NoArgsConstructor together
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
How to use ExpandableListView in Android Studio
[Rails] How to use PostgreSQL in Vagrant environment
[Ruby basics] How to use the slice method
[Java] How to use Calendar class and Date class
How to type backslash \
How to concatenate strings
A memo to check when you try to use Lombok
How to use and apply Java's JFrame / Canvas class
Notes on how to use regular expressions in Java
How to use GitHub for super beginners (team development)
Java --How to make JTable
[JavaFX] [Java8] How to use GridPane
JavaFx memorandum
Use AutosizingTextView
How to install JDK8-10 (Mac)
How to initialize Java array
How to insert a video
How to create a method
How to rollback migration files
A story about misunderstanding how to use java scanner (memo)
How to authorize using graphql-ruby
[For those who create portfolios] How to use binding.pry with Docker
How to master programming in 3 months
[How to install Spring Data Jpa]
How to connect Heroku and Sequel
How to pass Oracle Java Silver
[Swift] How to replace multiple strings
Java 8 ~ Stream API ~ to start now
I tried to summarize Java 8 now
How to make a Java array
[Android] How to make Dialog Fragment
How to build CloudStack using Docker
How to find Java prime numbers
Util to use for some reason
How to concatenate strings in java
How to install Swiper in Rails
Uppercase only the specified range with substring. (How to use substring)