Suppressing Java Findbugs error (EI_EXPOSE_REP)
I have a Java gettor method that looks like the foll开发者_开发问答owing:
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.
I just realized that Findbugs analyzes compiled code (.class
files), not source code. After rebuilding and re-running Findbugs, the problem went away.
No,we need to clone that object using below code :
public Date getSomeDate() {
return new Date(someDate.getTime()).clone();
}
精彩评论