FindBugs分析记录May expose internal representation by returning reference to mutable ob
2017-08-03 10:43
621 查看
使用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
相关文章推荐
- 【java findbugs集锦】【转】May expose internal representation by incorporating reference to mutable object
- findbugs提示:May expose internal representation by incorporating reference to mutable object 的理解.
- findbugs:may expose internal representation by ret
- FindBugs 恶意代码(may expose internal representation),序列化(defines non-transient non-serializable )错误解决方法
- FindBugs分析记录[H STCAL]Call to static DateFormat
- Inline hook ObReferenceObjectByHandle,附加问题笔记做记录
- ObReferenceObjectByHandle() 函数简略分析
- Effective C#之23:Avoid Returning References to Internal Class Objects
- Linq To xRM Issue(CRM 4.0 Online): The 'select' and 'orderBy' calls may only reference a single common entity type.
- Effective C# Item 23: Avoid Returning References to Internal Class Objects
- Item 26: Avoid Returning References to Internal Class Objects(Effective C#)
- FindBugs分析记录][H STCAL]Call to static DateFormat
- [C++] Returning values by reference in C++
- InlineHookObReferenceObjectByHandle(0环)
- 条款20:宁以pass-by-reference-to-const替换pass-by-value
- struts拦截器配置问题Unable to find interceptor class referenced by ref-name defaultStack
- “undefined reference to JNI_GetCreatedJavaVM”和“File format not recognized”错误原因分析
- 【20】宁以pass-by-reference-to-const替换pass-by-value
- undefined reference to "XX "的原因分析
- Effective C++读书笔记:TIP21~宁以pass-by-reference-to-const替换pass-by-valuue