FindBugs分析记录May expose internal representation by returning reference to mutable ob
来源:互联网 发布:德国开山刀网站淘宝 编辑:程序博客网 时间:2024/05/18 14:46
使用IDE的findbugs找到这么一个bug:May expose internal representation by incorporating reference to mutable object
抛出潜藏bug的代码
public class AlertCreateTaskRuleDTO extends BaseDTO {
public String[] getAlertNameArray(){return this.alertNameArray;}
public void setAlertNameArray(String[] names){this.alertNameArray = names;}
}
public class AlertCreateTaskRuleDTO extends BaseDTO {
public String[] getAlertNameArray(){return this.alertNameArray.clone();}
public void setAlertNameArray(String[] names){this.alertNameArray = names.clone();}
}
测试
public class test { public static void main(String []args){ AlertCreateTaskRuleDTO b = new AlertCreateTaskRuleDTO(); String[] values ={"11","22","333"}; b.setAlertNameArray(values); values[0]="GAI"; System.out.print(b.getAlertNameArray()[0]); }}
输出结果为GAI
原因:setter赋值是设置的数组的引用,一旦数组发生改变引用这个数组的地方都会改变
getter获取的是数组的引用,自然获得的是数组更改后的值。
防止这种问题的方法是:设置、获得一个数组的拷贝,而不直接设置或者获得一个数组引用
修改后:
public class AlertCreateTaskRuleDTO extends BaseDTO {
public String[] getAlertNameArray(){return this.alertNameArray.clone();}
public void setAlertNameArray(String[] names){this.alertNameArray = names.clone();}
}
测试输出的结果为11
- FindBugs分析记录May expose internal representation by returning reference to mutable ob
- findbugs提示:May expose internal representation by incorporating reference to mutable object 的理解.
- FindBugs 恶意代码(may expose internal representation),序列化(defines non-transient non-serializable )错误解决方法
- Fail to convert to internal representation
- findbugs分析记录
- FindBugs分析记录[H STCAL]Call to static DateFormat
- Effective C#之23:Avoid Returning References to Internal Class Objects
- 关于pass-by-reference-to-const和pass-by-value
- 关于 pass-by-reference-to-const 和 pass-by-value
- 读书笔记-C++ Primer 7.3 Returning a Reference
- php_lang_ref:Language Reference >> References Explained >> Returning References
- Gait Identification by Sparse Representation
- undefined reference to "XX "的原因分析
- undefined reference to `av_close_input_file' 原因分析
- 指针分析/Point-to Analysis/Reference Analysis
- undefined reference to 之原因分析
- Ob
- ob
- Python copy()与deepcopy()区别
- Java序列化与反序列化及serialVersionUID
- java基础部分总结1
- laravel5.4 excel导出
- mac 环境下搭建redis
- FindBugs分析记录May expose internal representation by returning reference to mutable ob
- Java的注释和变量
- Hibernate笔记
- iOS中的 + initialize 与 +load
- 腾讯笔试题
- Spark存储体系
- 转化json
- 常见的Web实时消息交互方式和SignalR
- TextView太长设置省略号