I'm getting the following violation reported by Sonar: May expose internal representation by returning reference to mutable object.
It's because I'm returning a String[] from a getter.
I know what the problem is and how to solve it but going through several thread on stackoverflow I noticed that seems to be happen for String[] and Dates for example:
But given the reason why that happens which is returning a reference to an object whose internal state could be changed by the caller. Shouldn't that violation be raised for every getter returning a mutable object?
For example:
public List<String> getList() { return list; }
public Foo getFoo() { return foo; } //where foo is just a random object with getters and setters...
The caller could change the state of the returned objects. Shouldn't sonar report the same for those?
Many thanks, Francisco.