电商网站流量统计,百度手机卫士下载安装,儿童故事网站建设,做营销策划的上哪个网站好背景#xff1a;
Lombok 这个插件大家日常工作中几乎是必备的#xff0c;几个简单的注解就可以帮助我们减少一大坨get/set方法等#xff1b;其中Builder注解使用的也很广泛#xff0c;使用了建造者模式帮助我们构建出个性化的对象#xff0c;本次踩坑点就在这个地方。
先…
背景
Lombok 这个插件大家日常工作中几乎是必备的几个简单的注解就可以帮助我们减少一大坨get/set方法等其中Builder注解使用的也很广泛使用了建造者模式帮助我们构建出个性化的对象本次踩坑点就在这个地方。
先讲一下踩坑的大致流程在一个需求中需要对接口内部的一个上下文对象 增加一个属性Map而这个上下文对象在别的接口中也有使用那就需要兼容其他接口所以我给这个新增的Map属性增加一个默认值 MapObject,Object map Maps.newHashMap() 然而还是获取这个属性的时候发生了异常原因就在当前类上面的Builder注解下文会举一个例子具体说明一下。
举例
package com.shizhuang.duapp.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 MapString,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.shizhuang.duapp.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 MapString, 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, MapString, Long extraInfoMap) {this.title title;this.brandId brandId;this.channelCode channelCode;this.extraInfoMap extraInfoMap;}//方法1public static CommodityModelBuilder builder() {return new CommodityModelBuilder();}--- 省略Get方法 ---public static class CommodityModelBuilder {private String title;private Long brandId;private Integer channelCode;private MapString, Long extraInfoMap;CommodityModelBuilder() {}//方法2public CommodityModelBuilder title(String title) {this.title title;return this;}//方法3public CommodityModelBuilder brandId(Long brandId) {this.brandId brandId;return this;}public CommodityModelBuilder channelCode(Integer channelCode) {this.channelCode channelCode;return this;}public CommodityModelBuilder extraInfoMap(MapString, Long extraInfoMap) {this.extraInfoMap extraInfoMap;return this;}//方法4public 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.shizhuang.duapp.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 MapString, Long extraInfoMap;// 方法1private static MapString, Long $default$extraInfoMap() {return Maps.newHashMap();}--- 省略部分代码 ---public MapString, Long getExtraInfoMap() {return this.extraInfoMap;}public static class CommodityModelBuilder {private String title;private Long brandId;private Integer channelCode;private boolean extraInfoMap$set;private MapString, Long extraInfoMap$value;CommodityModelBuilder() {}public CommodityModelBuilder title(String title) {this.title title;return this;}--- 省略部分代码 ---public CommodityModelBuilder extraInfoMap(MapString, Long extraInfoMap) {this.extraInfoMap$value extraInfoMap;// 标记用户已对目标属性赋值处理了this.extraInfoMap$set true;return this;}public CommodityModel build() {// this.extraInfoMap$value 是内部类的属性MapString, 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方式创建对象