Menu

#357 new Rule: Date field set without clone

open
nobody
None
5
2013-02-16
2011-09-25
No

If you take a java.util.Date on the constructor or setter, and you assign it to a field... then it *needs* to be cloned or some other thread/consumer could easily modify it.

Example:
class Foo {
Date x
Foo(Date x) {
this.x = x // violation
this.x = x.clone() // better
}

void setX(Date x) {
this.x = x // violation
this.x = x.clone() // better
}
}

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.