--A library that automatically generates frequently used code such as getter / setter and constructor in Java
--For example, if you annotate a class with @Getter
, it will automatically generate getters for the fields in the class.
--The official page is here
--Install the lombok plugin --Preference-> Plugins-> Search for lombok in MarketPlace and install --Enable annotation processing -Check Enable annotation processing in Preferences-> Build, Execution, Deployment-> Compiler-> Annotation Processors
Add the following to dependencies
in build.gradle
build.gradle
compileOnly 'org.projectlombok:lombok:1.18.8'
annotationProcessor 'org.projectlombok:lombok:1.18.8'
--You can see the code generated by lombok by running delombok
--When checking from the command line,
java -jar <lombok.jar path> delombok -p <Annotated class path>
@Getter
/ @Setter
--Automatically generate getter / setter for the fields included in the given class
Grantor
import lombok.Getter;
import lombok.Setter;
import java.util.List;
@Getter
@Setter
public class Human {
private int age;
private String name;
private List<String> hobby;
}
Generated class
import java.util.List;
public class Human {
private int age;
private String name;
private List<String> hobby;
@java.lang.SuppressWarnings("all")
public int getAge() {
return this.age;
}
@java.lang.SuppressWarnings("all")
public String getName() {
return this.name;
}
@java.lang.SuppressWarnings("all")
public List<String> getHobby() {
return this.hobby;
}
@java.lang.SuppressWarnings("all")
public void setAge(final int age) {
this.age = age;
}
@java.lang.SuppressWarnings("all")
public void setName(final String name) {
this.name = name;
}
@java.lang.SuppressWarnings("all")
public void setHobby(final List<String> hobby) {
this.hobby = hobby;
}
}
@ToString
--Automatically generate toString ()
method
Grantor
import lombok.ToString;
import java.util.List;
@ToString
public class Human {
private int age;
private String name;
private List<String> hobby;
}
Generated class
import java.util.List;
public class Human {
private int age;
private String name;
private List<String> hobby;
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "Human(age=" + this.age + ", name=" + this.name + ", hobby=" + this.hobby + ")";
}
}
@NoArgsConstructor
/ @RequiredArgsConstructor
/ @AllArgsConstructor
--@NoArgsConstructor
: Automatically generate a no-argument constructor
--@RequiredArgsConstructor
: Automatically generate a constructor that takes a field with final as an argument
--@AllArgsConstructor
: Automatically generate a constructor with all fields as arguments
Grantor(@AllArgsConstructor)
import lombok.AllArgsConstructor;
import java.util.List;
@AllArgsConstructor
public class Human {
private int age;
private final String name;
private List<String> hobby;
}
Generated class
import java.util.List;
public class Human {
private int age;
private final String name;
private List<String> hobby;
@java.lang.SuppressWarnings("all")
public Human(final int age, final String name, final List<String> hobby) {
this.age = age;
this.name = name;
this.hobby = hobby;
}
}
@Data
--Same effect as adding the following annotation
@Getter
/ @Setter
@ToString
@EqualsAndHashCode
@RequiredArgsConstructor
@Value
--Immutable version of @Data
--Make all fields private and final
--setter is not generated
@Builder
--Automatically generate Builder class
Grantor
import lombok.Builder;
import java.util.List;
@Builder
public class Human {
private int age;
private final String name;
private List<String> hobby;
}
Side to use
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> hobby = Arrays.asList("Futsal", "diet", "reading");
Human human = new Human.HumanBuilder().age(20).name("Name Taro").hobby(hobby).build();
System.out.println(human);
}
}
Generated class
import java.util.List;
public class Human {
private int age;
private final String name;
private List<String> hobby;
@java.lang.SuppressWarnings("all")
Human(final int age, final String name, final List<String> hobby) {
this.age = age;
this.name = name;
this.hobby = hobby;
}
@java.lang.SuppressWarnings("all")
public static class HumanBuilder {
@java.lang.SuppressWarnings("all")
private int age;
@java.lang.SuppressWarnings("all")
private String name;
@java.lang.SuppressWarnings("all")
private List<String> hobby;
@java.lang.SuppressWarnings("all")
HumanBuilder() {
}
@java.lang.SuppressWarnings("all")
public HumanBuilder age(final int age) {
this.age = age;
return this;
}
@java.lang.SuppressWarnings("all")
public HumanBuilder name(final String name) {
this.name = name;
return this;
}
@java.lang.SuppressWarnings("all")
public HumanBuilder hobby(final List<String> hobby) {
this.hobby = hobby;
return this;
}
@java.lang.SuppressWarnings("all")
public Human build() {
return new Human(age, name, hobby);
}
@java.lang.Override
@java.lang.SuppressWarnings("all")
public java.lang.String toString() {
return "Human.HumanBuilder(age=" + this.age + ", name=" + this.name + ", hobby=" + this.hobby + ")";
}
}
@java.lang.SuppressWarnings("all")
public static HumanBuilder builder() {
return new HumanBuilder();
}
}
@NonNull
--If you give it to a class field or method argument, it will automatically check for null.
@EqualsAndHashCode
--ʻEquals () ,
hashCode () `method is automatically generated
@Log
--Automatically generate Logger with the name log
@Code generated when Slf4j is added
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.class);
--Supports various libraries such as Slf4j
, Log4j
--Plan to study Logger separately
var
/ val
--The type can be inferred automatically when a variable is declared. It seems that it will not be used because it was implemented as standard in JDK10 or later
Recommended Posts