1.使用范围
- 属性:为该属性提供getter方法
- 类:为类的所有属性提供getter方法
2.介绍
生成属性的get方法。
注意:与setter不同的是,@Getter注解会生成final修饰的属性
3.属性
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package lombok;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.SOURCE)
public @interface Getter {
AccessLevel value() default AccessLevel.PUBLIC;
Getter.AnyAnnotation[] onMethod() default {};
boolean lazy() default false;
/** @deprecated */
@Deprecated
@Retention(RetentionPolicy.SOURCE)
@Target({})
public @interface AnyAnnotation {
}
}
-
boolean lazy() default false;
@Getter(lazy=true)是在Lombok v0.10中引入的。
你可以让lombok生成一个getter,它将在第一次调用这个getter时计算一个值,并从那时起缓存它。如果计算数值需要大量的CPU,或者数值需要大量的内存,这就很有用。要使用这个功能,创建一个private final变量,用运行成本高的表达式来初始化它,并用@Getter(lazy=true)来注释你的字段。这个字段将从你的代码的其余部分隐藏起来,并且表达式的评估不会超过一次,即在第一次调用getter的时候。没有神奇的标记值(也就是说,即使你昂贵的计算结果是空的,结果也会被缓存),你昂贵的计算不需要是线程安全的,因为lombok负责锁。
如果初始化表达式很复杂,或者包含泛型,我们建议将代码移到一个私有的(如果可能的话是静态的)方法中,并调用该方法。
4.实例
package org.learn.lombok;
import lombok.Getter;
import lombok.Setter;
import org.learn.lombok.annotation.Method;
import org.learn.lombok.annotation.Param;
@Getter
public class DemoGetter {
private String privateVar;
public String publicVar;
String defaultVar;
protected String protectedVar;
public final String finalVar = "";
public static String staticVar = "";
public volatile String volatileVar = "";
public static final String staticFinalVar = "";
}
编译后:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.learn.lombok;
public class DemoGetter {
private String privateVar;
public String publicVar;
String defaultVar;
protected String protectedVar;
public final String finalVar = "";
public static String staticVar = "";
public volatile String volatileVar = "";
public static final String staticFinalVar = "";
public DemoGetter() {
}
public String getPrivateVar() {
return this.privateVar;
}
public String getPublicVar() {
return this.publicVar;
}
public String getDefaultVar() {
return this.defaultVar;
}
public String getProtectedVar() {
return this.protectedVar;
}
public String getFinalVar() {
this.getClass();
return "";
}
public String getVolatileVar() {
return this.volatileVar;
}
}
package org.learn.lombok;
import lombok.Getter;
import lombok.Setter;
import org.learn.lombok.annotation.Method;
import org.learn.lombok.annotation.Param;
public class DemoGetter {
@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;
}
}
java标准写法(也就是编译后的写法)
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package org.learn.lombok;
import java.util.concurrent.atomic.AtomicReference;
public class DemoGetter {
private final AtomicReference<Object> cached = new AtomicReference();
public DemoGetter() {
}
private double[] expensive() {
double[] result = new double[1000000];
for(int i = 0; i < result.length; ++i) {
result[i] = Math.asin((double)i);
}
return result;
}
public double[] getCached() {
Object value = this.cached.get();
if (value == null) {
synchronized(this.cached) {
value = this.cached.get();
if (value == null) {
double[] actualValue = this.expensive();
value = actualValue == null ? this.cached : actualValue;
this.cached.set(value);
}
}
}
return (double[])((double[])(value == this.cached ? null : value));
}
}
附属说明:你不应该直接引用这个字段,始终使用lombok生成的getter,因为这个字段的类型会被处理成一个AtomicReference。不要试图直接访问这个AtomicReference;如果它指向自己,那么这个值已经被计算过了,是空的。如果该引用指向null,那么该值就没有被计算。这种行为在未来的版本中可能会改变。因此,始终使用生成的getter来访问你的字段!