Suppressing Java Findbugs error (EI_EXPOSE_REP)

感情迁移 提交于 2019-12-01 16:49:40

问题


I have a Java gettor method that looks like the following:

import java.util.Date;
//...
public Date getSomeDate() {
  return someDate;
}

and Findbugs reports that this exposes a mutable object: "May expose internal representation by returning reference to mutable object". I changed the code to this:

import java.util.Date;
//...
public Date getSomeDate() {
  return new Date(someDate.getTime());
}

but Findbug still reports the same vulnerability. What more can I do to suppress/fix this problem? I am running Findbugs 1.3.9 in the IntellJ 10 Findbugs plugin.


回答1:


I just realized that Findbugs analyzes compiled code (.class files), not source code. After rebuilding and re-running Findbugs, the problem went away.




回答2:


No,we need to clone that object using below code :

public Date getSomeDate() {
  return new Date(someDate.getTime()).clone();
}


来源:https://stackoverflow.com/questions/5472299/suppressing-java-findbugs-error-ei-expose-rep

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!