最全的 lombok 注解详情(随着版本不定时更新)
一、注解介绍
作用于类,生成该类所有的实例变量的 getter 方法。作用于变量,生成该变量的 getter 方法
二、属性介绍
- value:设置 getter 方法的访问修饰符,默认为 public
共有 PUBLIC、MODULE、PROTECTED、PACKAGE、PRIVATE、NONE
MODULE 是 Java 9 的新特性,NONE 表示不生成 getter 方法,即停用注解功能 - lazy:懒加载,默认为 false。如果设置为 true,参数必须是 final
- onMethod:将列出的所有注解都放在生成的方法上
JDK 7 之前的写法是 onMethod = @__({@Deprecated}),而 JDK 8 之后的写法是 onMethod_ = {@Deprecated}
三、实战演练
@Getter(value = AccessLevel.PROTECTED, lazy = false, onMethod_ = {
@Deprecated})
public class 徐艺洋 {
private String name;
@Getter(lazy = true)
private final Integer age = 1;
}
编译后
public class 徐艺洋 {
private String name;
private final AtomicReference<Object> age;
public 徐艺洋() {
this.age = new AtomicReference();
}
@Deprecated
protected String getName() {
return this.name;
}
/**
* 单例懒汉模式,双检查锁机制
*/
public Integer getAge() {
Object value = this.age.get();
if (value == null)
synchronized (this.age) {
value = this.age.get();
if (value == null) {
Integer actualValue = Integer.valueOf(1);
value = (actualValue == null) ? this.age : actualValue;
this.age.set(value);
}
}
return ((Integer) (Integer)((value == this.age) ? null : value));
}
}