簡介
lombok是一個編譯級別的外掛,它可以在專案編譯的時候生成一些程式碼。通俗的說,lombok可以通過註解來標示生成getter
settter
等程式碼。
引入
建立gradle專案
compile group: 'org.projectlombok', name: 'lombok', version: '1.16.20'
複製程式碼
註解
@NonNull
標記欄位不可為null
@Setter
public class Person {
@NonNull
private String name;
@NonNull
private Integer age;
}
複製程式碼
對應的位元組碼檔案:
public class Person {
@NonNull
private String name;
@NonNull
private Integer age;
public Person() {
}
public void setName(@NonNull String name) {
if (name == null) {
throw new NullPointerException("name");
} else {
this.name = name;
}
}
public void setAge(@NonNull Integer age) {
if (age == null) {
throw new NullPointerException("age");
} else {
this.age = age;
}
}
}
複製程式碼
@Getter/@Setter
自動生成getter和setter方法
public class Person {
@Getter
private String name;
@Setter
private Integer age;
}
複製程式碼
對應的位元組碼檔案:
public class Person {
private String name;
private Integer age;
public Person() {
}
public String getName() {
return this.name;
}
public void setAge(Integer age) {
this.age = age;
}
}
複製程式碼
@Cleanup
自動關閉流程式碼
@Cleanup
InputStream in = new FileInputStream(args[0]);
複製程式碼
對應的位元組碼檔案:
InputStream in = new FileInputStream(args[0]);
if (Collections.singletonList(in).get(0) != null) {
in.close();
}
複製程式碼
@AllArgsConstructor/@NoArgsConstructor/@RequiredArgsConstructor
自動生成全參建構函式和無參建構函式
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private String name;
private Integer age;
}
複製程式碼
對應的位元組碼檔案
public class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public Person() {
}
}
複製程式碼
@Builder
自動生成建造者模式的bean
@Builder
public class Person {
private String name;
private Integer age;
}
複製程式碼
對應的位元組碼檔案
public class Person {
private String name;
private Integer age;
Person(String name, Integer age) {
this.name = name;
this.age = age;
}
public static Person.PersonBuilder builder() {
return new Person.PersonBuilder();
}
public static class PersonBuilder {
private String name;
private Integer age;
PersonBuilder() {
}
public Person.PersonBuilder name(String name) {
this.name = name;
return this;
}
public Person.PersonBuilder age(Integer age) {
this.age = age;
return this;
}
public Person build() {
return new Person(this.name, this.age);
}
public String toString() {
return "Person.PersonBuilder(name=" + this.name + ", age=" + this.age + ")";
}
}
}
複製程式碼
@EqualsAndHashCode
自動生成equals和hashcode方法
@EqualsAndHashCode
public class Person {
private String name;
private Integer age;
}
複製程式碼
對應的位元組碼檔案
public class Person {
private String name;
private Integer age;
public Person() {
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof Person)) {
return false;
} else {
Person other = (Person)o;
if (!other.canEqual(this)) {
return false;
} else {
Object this$name = this.name;
Object other$name = other.name;
if (this$name == null) {
if (other$name != null) {
return false;
}
} else if (!this$name.equals(other$name)) {
return false;
}
Object this$age = this.age;
Object other$age = other.age;
if (this$age == null) {
if (other$age != null) {
return false;
}
} else if (!this$age.equals(other$age)) {
return false;
}
return true;
}
}
}
protected boolean canEqual(Object other) {
return other instanceof Person;
}
public int hashCode() {
int PRIME = true;
int result = 1;
Object $name = this.name;
int result = result * 59 + ($name == null ? 43 : $name.hashCode());
Object $age = this.age;
result = result * 59 + ($age == null ? 43 : $age.hashCode());
return result;
}
}
複製程式碼
@ToString
自動生成toString()方法
@ToString
public class Person {
private String name;
private Integer age;
}
複製程式碼
對應的位元組碼檔案
public class Person {
private String name;
private Integer age;
public Person() {
}
public String toString() {
return "Person(name=" + this.name + ", age=" + this.age + ")";
}
}
複製程式碼
@Value
自動生成全參建構函式、Getter方法、equals方法、hashCode法、toString方法
@Value
public class Person {
private String name;
private Integer age;
}
複製程式碼
注意:@Value不會生成Setter方法
@Synchronized
自動為被標記的方法新增synchronized鎖
public class SynchronizedExample {
private final Object readLock = new Object();
@Synchronized
public static void hello() {
System.out.println("world");
}
@Synchronized
public int answerToLife() {
return 42;
}
@Synchronized("readLock")
public void foo() {
System.out.println("bar");
}
}
複製程式碼
對應的位元組碼檔案
public class SynchronizedExample {
private static final Object $LOCK = new Object[0];
private final Object $lock = new Object[0];
private final Object readLock = new Object();
public static void hello() {
synchronized($LOCK) {
System.out.println("world");
}
}
public int answerToLife() {
synchronized($lock) {
return 42;
}
}
public void foo() {
synchronized(readLock) {
System.out.println("bar");
}
}
}
複製程式碼
@Delegate
為標記屬性生成委託方法
public class DelegateExample {
public void show() {
System.out.println("show...");
}
}
@AllArgsConstructor
public class Demo {
@Delegate
private final DelegateExample delegateExample;
}
複製程式碼
對應的位元組碼檔案
public class DelegateExample {
public DelegateExample() {
}
public void show() {
System.out.println("show...");
}
}
public class Demo {
private final DelegateExample delegateExample;
public Demo(DelegateExample delegateExample) {
this.delegateExample = delegateExample;
}
// 委託方法
public void show() {
this.delegateExample.show();
}
}
複製程式碼