Hi!
I get this warning using, when I override the clone-method and have there following implementation:
@Override
public User clone() {
User clone = new User();
clone.setFirstName(this.getFirstName());
clone.setAgeGroup(this.getAgeGroup());
// Do something with clone-object
}
I get why I receive this warning (trigger is whenever a clone() is being overriden) since overriding the clone-method can cause a lot of issues, in general.
In my opinion the in this example the clone() is misused and should just be renamed to for instance, let’s say createInstanceOfOriginal()
.
But am I correct in assuming the the “clone”-Object in this example shares not common state with the this-object, since it is completely independent of the this-object.
Reason being that the “clone-object” is being created by using a constructor. So when I change the value of an attribute in the “clone-object” , it has no effect on the “this-object”, correct?
If so, then I at least find the description provided by SonarQube in the “Why is this issue” somewhat misleading?
It states there:
“The clone generally shares state with the object being cloned. If that state is mutable, you don’t have two independent objects. If you modify one, the other changes as well…”
This is clearly not the case given the implementation above, right?
So it seems that SonarQube does not consider the whole code context here by also taking the implementation into account, but right away goes on whenever a clone-method is being overriden and providing this general message, which might be not applicable in the particular situation due to the concrete implementation -am I being right?
Best wishes
Abegail
Blockquote
Blockquote