Lombok的踩坑系列之@Builder

背景:

Lombok 这个插件大家日常工作中几乎是必备的,几个简单的注解就可以帮助我们减少一大坨get/set方法等;其中@Builder注解使用的也很广泛,使用了建造者模式帮助我们构建出个性化的对象,本次踩坑点就在这个地方。

先讲一下踩坑的大致流程,在一个需求中需要对接口内部的一个上下文对象 增加一个属性Map,而这个上下文对象在别的接口中也有使用,那就需要兼容其他接口,所以我给这个新增的Map属性增加一个默认值 Map<Object,Object> map = Maps.newHashMap() ,然而还是获取这个属性的时候发生了异常,原因就在当前类上面的@Builder注解,下文会举一个例子具体说明一下。

举例:

package com.nbinterface.interfaces.facade;

import com.google.common.collect.Maps;
import lombok.Builder;
import lombok.Getter;

import java.util.Map;

@Getter
@Builder
public class CommodityModel {

    private String title;

    private Long brandId;

    private Integer channelCode;

    private Map<String,Long> extraInfoMap = Maps.newHashMap();

    public static void main(String[] args) {

        CommodityModel model = CommodityModel.builder()
                .brandId(100L)
                .title("NB 新百伦")
                .build();
        Object price = model.getExtraInfoMap().getOrDefault("price", 100L);
        System.out.println("price:" + price);
    }
}

问题来了,如上代码直接执行main方法,是否会打印出 "price: 100" ?

答案分割图

嗯哼,答案是大家贼熟悉的 NPE

看到这NPE,肯定是 extraInfoMap 这个属性是Null ,但是我们明明给了一个默认值嘛,为啥子会是Null 呢?答案就在编译后的代码中,如下:(着重关注标记的代码)

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.nbinterface.interfaces.facade;

import com.google.common.collect.Maps;
import java.util.Map;

public class CommodityModel {
    private String title;
    private Long brandId;
    private Integer channelCode;
    private Map<String, Long> extraInfoMap = Maps.newHashMap();

    public static void main(String[] args) {
        CommodityModel model = builder().brandId(100L).title("NB 新百伦").build();
        Object price = model.getExtraInfoMap().getOrDefault("price", 100L);
        System.out.println("price:" + price);
    }

    CommodityModel(String title, Long brandId, Integer channelCode, Map<String, Long> extraInfoMap) {
        this.title = title;
        this.brandId = brandId;
        this.channelCode = channelCode;
        this.extraInfoMap = extraInfoMap;
    }
    
    //方法1
    public static CommodityModelBuilder builder() {
        return new CommodityModelBuilder();
    }

  --- 省略Get方法 ---

    public static class CommodityModelBuilder {
        private String title;
        private Long brandId;
        private Integer channelCode;
        private Map<String, Long> extraInfoMap;

        CommodityModelBuilder() {
        }
    //方法2
        public CommodityModelBuilder title(String title) {
            this.title = title;
            return this;
        }
    //方法3
        public CommodityModelBuilder brandId(Long brandId) {
            this.brandId = brandId;
            return this;
        }

        public CommodityModelBuilder channelCode(Integer channelCode) {
            this.channelCode = channelCode;
            return this;
        }

        public CommodityModelBuilder extraInfoMap(Map<String, Long> extraInfoMap) {
            this.extraInfoMap = extraInfoMap;
            return this;
        }
    //方法4
        public CommodityModel build() {
            return new CommodityModel(this.title, this.brandId, this.channelCode, this.extraInfoMap);
        }

        public String toString() {
            return "CommodityModel.CommodityModelBuilder(title=" + this.title + ", brandId=" + this.brandId + ", channelCode=" + this.channelCode + ", extraInfoMap=" + this.extraInfoMap + ")";
        }
    }
}

Lombok的@Builder 注解在编译期间会帮我们生成一个内部的Builder类,并生成一个创建这个内部builder对象的静态方法(方法1),然后我们的代码是调用了方法1,方法2,方法3和方法4,其中方法4中的this.extraInfoMap 是内部类中的属性并没有默认值,所以build()方法返回的对象extraInfoMap就是一个null;

解决:

在需要默认值的属性上面增加 @Builder.Default 注解

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.nbinterface.interfaces.facade;

import com.google.common.collect.Maps;
import java.util.Map;

public class CommodityModel {
    private String title;
    private Long brandId;
    private Integer channelCode;
    private Map<String, Long> extraInfoMap;
  
    // 方法1
    private static Map<String, Long> $default$extraInfoMap() {
        return Maps.newHashMap();
    }
     --- 省略部分代码 ---

    public Map<String, Long> getExtraInfoMap() {
        return this.extraInfoMap;
    }

    public static class CommodityModelBuilder {
        private String title;
        private Long brandId;
        private Integer channelCode;
        private boolean extraInfoMap$set;
        private Map<String, Long> extraInfoMap$value;

        CommodityModelBuilder() {
        }

        public CommodityModelBuilder title(String title) {
            this.title = title;
            return this;
        }
        
        --- 省略部分代码 ---

        public CommodityModelBuilder extraInfoMap(Map<String, Long> extraInfoMap) {
            this.extraInfoMap$value = extraInfoMap;
            // 标记用户已对目标属性赋值处理了
            this.extraInfoMap$set = true;
            return this;
        }

        public CommodityModel build() {
            // this.extraInfoMap$value 是内部类的属性
            Map<String, Long> extraInfoMap$value = this.extraInfoMap$value;
            // 用户如果没有操作,则使用方法1为内部类赋值
            if (!this.extraInfoMap$set) {
                extraInfoMap$value = CommodityModel.$default$extraInfoMap();
            }
            // 使用内部类的属性创建对象
            return new CommodityModel(this.title, this.brandId, this.channelCode, extraInfoMap$value);
        }
    }
}

此时再看编译后的代码,会发现内部类中有一个属性extraInfoMap$set 会标记用户是否对extraInfoMap属性处理过,没有操作的话就会赋值我们加的默认值 = Maps.newHashMap();

总结:

日常我们业务开发中有很多小的需求,只需要增加一个属性就可以解决,此时就要注意历史逻辑中是否用 Lombok 的 Builder方式创建对象,

  • 11
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值