spotbugs~lombok生成的Date属性引起的EI_EXPOSE_REP问题

EI_EXPOSE_REP是spotbugs,findbugs里通过代码分析抛出来的代码安全性问题,主要表示在一个Date类型的字段在进行@Getter注解时,没有检查它是否为空,这块我们有两种解决方案,第一种是手写Date类型的字段的Getter方法;第二种是安装com.google.code.findbugs:findbugs包,然后使用它的@SuppressFBWarnings注释,把这种问题忽略,我们介绍一下这两种方法。

第一种

重写它的setter方法

public void setBillDate(Date billDate) {
    this.billDate = billDate != null ? new Date(billDate.getTime()) : null;
}

第二种

使用引用findbug包

   <dependency>
            <groupId>com.google.code.findbugs</groupId>
            <artifactId>findbugs</artifactId>
            <version>3.0.1</version>
        </dependency>

在实体上添加SuppressFBWarnings注解即可

@Data
@SuppressFBWarnings(value = {"EI_EXPOSE_REP", "EI_EXPOSE_REP2"}, justification = "I prefer to suppress these FindBugs warnings")
public abstract class BaseEntity<T extends Model<?>> extends Model<T> {
    private Date createTime;
    private String createUser;
    private Date updateTime;
    private String updateUser;

}

再进行spotbugs:spotbugs时,这个错误就没有了。

posted @ 2020-09-02 13:44  张占岭  阅读(3153)  评论(0编辑  收藏  举报