6

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.

Gabe
  • 84,912
  • 12
  • 139
  • 238
Ralph
  • 31,584
  • 38
  • 145
  • 282

2 Answers2

11

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

Ralph
  • 31,584
  • 38
  • 145
  • 282
  • I've been the victim of the same situation :) - spending 15 minutes wondering why my change didn't fix the error, and then realizing that I didn't rebuild. – Rob Hruska Mar 29 '11 at 14:21
  • @Rob Hruska: Took me about the same amount of time :-). – Ralph Mar 29 '11 at 15:00
0

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

public Date getSomeDate() {
  return new Date(someDate.getTime()).clone();
}
ngCoder
  • 2,095
  • 1
  • 13
  • 22