Lombok注解之@Getter

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来访问你的字段!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值