Findbugs中的BUG:May expose internal representation by returning reference to mutable object 引发问题说明

本文探讨了在使用FindBugs插件检测IDEA中model层类时遇到的“Mayexposeinternalrepresentationbyreturningreferencetomutableobject”警告。通过修改实体类的get和set方法,采用克隆技术来避免内部对象状态的外部修改,从而解决了该问题。然而,这种方法可能与JSON格式化注解冲突,需谨慎使用。

在使用IDEA的findbugs的插件检测model层类的时候发生如下错误:

May expose internal representation by returning reference to mutable object
Returning a reference to a mutable object value stored in one of the object's fields exposes the internal representation of the object.  If instances are accessed by untrusted code, and unchecked changes to the mutable object would compromise security or other important properties, you will need to do something different. Returning a new copy of the object is better approach in many situations.

这里提示的BUG的测试结果如下:

实体类BusGeologicalDrilling部分代码:

public class BusGeologicalDrilling{
 private Date enddt;

// 此处省略构造函数


//下面为IDE自动为我们生成的get和set方法

 public Date getEnddt() {
        return   enddt  ;
    }

 public void setEnddt(Date enddt) {
        this.enddt =   enddt  ;
    }
}

部分测试代码如下:

 BusGeologicalDrilling busGeologicalDrilling=new BusGeologicalDrilling();
 Date date=new Date();
 busGeologicalDrilling.setEnddt(date);
 System.out.println(busGeologicalDrilling.getEnddt().toString());
 date.setYear(5);
 System.out.println(busGeologicalDrilling.getEnddt().toString());

测试结果如下:

通过结果对比发现:

日期的对象在经过改变后,原本之前赋予实体的enddt属性的值也跟着被修改掉了,这样显然是不对的,这里涉及到引用传递值的问题。此处不再深究。

解决方案:

一:修改实体的属性的get和set方法如下:

public class BusGeologicalDrilling{
 private Date enddt;

// 此处省略构造函数


//下面为IDE自动为我们生成的get和set方法

 public Date getEnddt() {
        return (Date)enddt.clone();
    }

 public void setEnddt(Date enddt) {
        this.enddt = (Date)enddt.clone();
    }
}

备注:这种方式虽然可以解决引用值传递的问题,但是当在属性上使用【com.fasterxml.jackson.annotation包下面的JsonFormat】如:注解控制Date的格式或者时区,如果当前属性绑定的是非格式或者空值会发生绑定错误,所以此种方法并不完美,如下:

 @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
 private Date enddt;

二、使用官方推荐的Date获取方式为实体进行赋值,测试如下:

上面测试我们看到是没有任何问题的,原因在于Calendar类和Date的实体获取机制不同导致。

 

 

 

FindBugs实践 1、 Bug级别 根据Bug可能导致的后果,FindBugs定义了若干Bug级别,主要的级别如下所示: Bad Practice: 不好的实践 Correctness: 正确性 Experimental Internationalization: Malicious code vulnerability: 存在漏洞的有害代码 Multithreaded correctness: 多线程正确性 Performance:性能 Security:安全 Dodgy: 欺骗性代码 2、 常见Bug以及处理办法 a) 不需要处理 May expose internal representation by incorporating reference to mutable object 描述:调用set方法,修改对象属性,被修改的对象属性是一个可变的对象; May expose internal representation by returning reference to mutable object 描述:调用get方法,获得对象属性,获得的对象属性是一个可变的对象; b) 建议处理 Dead store to local variable 描述:对一个局部变量赋值,但是这个局部变量可能不会被用到; 处理方式:确认此局部变量是否会被使用,如果确实不会被用到,请去掉; Exception is caught when Exception is not thrown 描述:调用的方法中不会抛出异常,但是调用方法的时候尝试使用try catch 捕获异常; 处理方式:确认此方法的调用会不会导致异常的发生,如果不会抛出异常请去 掉try catch,确认方法调用会不会抛出异常关键是对方法调用参数的合法 性进行检查,排除调用这个方法时可能抛出异常的参数; Unread field 描述:某个对象的属性不会被读取 处理方式:确认此局部变量是否会被使用,如果确实不会被用到,请去掉,和 Dead store to local variable一样处理,只是这里针对的是不是局部变量; Call to static DateFormat 描述:调用静态的DateFormat对象 处理方式:DateFormat对象是线程不安全的,建议不要使用静态的DateFormat,但是DateFormat不会被修改的话,也可以不用修改; Class is Serializable, but doesn't define serialVersionUID 描述:类是可序列化的,但是没有定义serialVersionUID; 处理方式:自动生成serialVersionUID; Field only ever set to null 描述:Field一直被设置为null; 处理方式:检查相关filed的调用情况,看所有对Field的操作是否都是将Field设置为null; Usage of GetResource may be unsafe if class is extended 描述:如果一个类被继承了,使用getResource可能会不安全 处理方式: Unsynchronized get method, synchronized set method 描述:非同步的get方法,同步的set方法 处理方式:修改为一致的情况 Unconditional wait 描述:无条件的wait 处理方式: Switch statement found where one case falls through to the next case 描述:switch语句中有case没有使用break; 处理方式: 增加break; Should be a static inner class 描述:应该是静态内部类; 处理方式: 增加static修饰符使其成为静态内部类; Private method is never called 描述:私有方法没有被调用; 处理方式:考虑将其注释掉;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值