【java findbugs集锦】【转】May expose internal representation by incorporating reference to mutable object
2016-12-26 09:26
489 查看
[hyddd的FindBugs分析记录][M V EI2] May expose internal representation by incorporating reference to mutable object
[M V EI2] May expose internal representation by incorporating reference to mutable object [EI_EXPOSE_REP2]This code stores a reference to an externally mutable object into 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. Storing a copy of the object is better approach in many situations.
这个问题和Inconsistent synchronization描述的问题很类似,解决方案也很类似,可以参考看看:http://www.cnblogs.com/hyddd/articles/1391098.html
先看一段代码:
public class Test extends Thread{
public static void main(String args[]) throws Exception{
Test3 obj = new Test3();
Date now = new Date();
obj.setRegDate(now);
now.setYear(4000); //问题所在!
System.out.println(obj.getRegDate());
}
}
public class Test3 {
private Date regDate ;
public void setRegDate(Date regDate) {
this.regDate = regDate;
}
public Date getRegDate() {
return regDate;
}
}
这段代码的输出是:Thu Feb 15 21:47:13 CST 5900
如果main里面不加now.setYear(4000);这句代码呢,结果是:Sun Feb 15 21:47:31 CST 2009
从这里我们发现了,修改一个对象,可能会引起其他对象的修改,因为JAVA里,对象是引用传递的......所以这里我的建议是:setObj的时候,对象不要直接赋值(this.regDate = regDate),而是赋值传入对象的拷贝(this.regDate = (Date)regDate.clone();)。
OK~现在我们把代码this.regDate = regDate替换成this.regDate = (Date)regDate.clone();,运行一下看看结果,噢,输出是:Sun Feb 15 21:47:31 CST 2009。
相关文章推荐
- findbugs提示:May expose internal representation by incorporating reference to mutable object 的理解.
- FindBugs分析记录May expose internal representation by returning reference to mutable ob
- findbugs:may expose internal representation by ret
- FindBugs 恶意代码(may expose internal representation),序列化(defines non-transient non-serializable )错误解决方法
- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
- java.lang.NullPointerException:Attempt to invoke virtual method xxx on a null object reference
- java.sql.SQLException: Fail to convert to internal representation
- Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference(Fb)
- Linq To xRM Issue(CRM 4.0 Online): The 'select' and 'orderBy' calls may only reference a single common entity type.
- java中的 by value or by reference
- “Object reference not set to an instance of an object. ”错误
- VB.NET Error Object reference not set to an instance of an object
- warning LNK4089: all references to "USER32.dll" discarded by /OPT:REF
- Java: pass by value or reference?
- An introduction to the Java 2 Platform, Enterprise Edition specification by way of BEA's WebLogic Server
- 我遇到的Object reference not set to an instance of an object.错误的解决办法
- Object reference not set to an instance of an object
- Cannot display this layer. The data source referenced by this layer may be missing or corrupted.
- 奇怪的 Object reference not set to an instance of an object
- (原創) 如何在C++/CLI實做pass by reference to const? (.NET) (C++/CLI) (C/C++)