Lombok学习笔记
Lombok是我们在工作中常用的工具,写一篇文章整理一下用法。
使用Maven导入lombok
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>provided</scope>
</dependency>
具体用法
val
相当于final var
var
用于声明局部变量
public class Main {
public static void main(String[] args) {
var name = "123";
final var age = 1;
System.out.println(age);
System.out.println(name);
}
}
@NonNull
用于方法的参数空指针校验。当为null时默认抛出throw new NullPointerException("param is marked non-null but is null");
public class Main {
public static void main(String[] args) {
test(null);
}
static void test(@NonNull People people) {
System.out.println(people.getName());
}
}
@Cleanup
用于一个资源自动释放。
默认在方法执行的最后执行close()
释放资源,对于没有该方法的对象,可在@Cleanup()
中指定相应的无参方法。释放资源方法需要参数的,不可用该注解。
public class Main {
public static void main(String[] args) throws IOException {
@Cleanup InputStream in = new FileInputStream("src/main/java/org/example/Main.java");
@Cleanup OutputStream out = new FileOutputStream("src/main/java/org/example/test.java");
byte[] b = new byte[10000];
in.read(b);
out.write(b);
}
}
public class Main {
public static void main(String[] args) throws IOException {
@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);
}
}
注意:cleanup方法只在资源不为null的时候执行。如果代码抛出异常,且清理方法也抛出异常,原始的异常会被cleanup隐藏。这种情况不建议使用该注解。
@Getter and @Setter
生成成员变量的get和set方法,且默认都为public
,可使用AccessLevel
指定。
public class People {
@Getter
@Setter
private String name;
@Getter
@Setter(AccessLevel.PRIVATE)
private Boolean isFoo;
}
也可以在类上使用,相当于对所有的非静态成员变量使用。
@Getter
@Setter
public class People {
private String name;
private Boolean isFoo;
}
@ToString
让lombok实现toString()
@ToString()
public class People {
@Setter
private String name;
@Setter
@ToString.Exclude
private Boolean isFoo;
}
People(name=123)
@EqualsAndHashCode
生成equals
和hashcode
方法
@EqualsAndHashCode
public class People {
private String name;
@EqualsAndHashCode.Exclude
private Boolean isFoo;
}
@NoArgsConstructor, @RequiredArgsConstructor and @AllArgsConstructor
@NoArgsConstructor
:生成非空构造函数
@RequiredArgsConstructor
: 为所有@NonNull
生成构造函数
@AllArgsConstructor
:生成全部参数的构造函数
@AllArgsConstructor
@NoArgsConstructor
@RequiredArgsConstructor
public class People {
private String name;
private Boolean isFoo;
@NonNull
private Integer age;
@NonNull
private String address;
}
@Data
相当于@ToString
+@EqualsAndHashCode
+@Getter
+@Setter
+@RequiredArgsConstructor
@Value
所有的filed都是private final
相当于final @ToString @EqualsAndHashCode @AllArgsConstructor @FieldDefaults(makeFinal = true, level = AccessLevel.PRIVATE) @Getter
@Builder
建造者模式
@Builder
public class PeopleBuilder {
private String name;
private Boolean isFoo;
private Integer age;
private String address;
}
public class Main {
public static void main(String[] args) throws IOException {
PeopleBuilder.builder().isFoo(true).build();
}
}
@SneakyThrows
偷偷抛出受检查的异常,无需用throws写,需要谨慎使用
public class Main implements Runnable {
@SneakyThrows(FileNotFoundException.class)
public static void main(String[] args) {
FileInputStream fileInputStream = new FileInputStream("ss");
}
@SneakyThrows
@Override
public void run() {
throw new Throwable();
}
}
@Synchronized
用于静态或者实例方法。类似synchronized
@With
生成除关联对象之外的克隆。用于对象final变量的修改。
@AllArgsConstructor
@ToString
public class People {
private final String name;
@With
private final Integer age;
}
public class Main {
public static void main(String[] args) {
People people = new People("11", 2);
System.out.println(people.withAge(4));
System.out.println(people);
}
}
People(name=11, age=4)
People(name=11, age=2)
@Getter(lazy=true)
将某一个计算需要耗费大量CPU或者内存的变量缓存起来
public class GetterLazyExample {
@Getter(lazy=true) private final double[] cached = expensive();
private double[] expensive() {
double[] result = new double[1000000];
for (int i = 0; i < result.length; i++) {
result[i] = Math.asin(i);
}
return result;
}
}
@Log
生成日志变量