Hopefully someone can help clarifying this.
Consider having the following exception type:
public class SomeException extends RuntimeException {
private List<String> messages;
public SomeException(List<String> messages) {
this.messages = Collections.unmodifiableList(messages);
}
public List<String> getMessages() {
return this.messages;
}
}
For the getMessages method Sonar says that the immutability rule is violated and I should return a copy of the internal list.
However, there is no need for that because Sonar should figure out that the internal List could've been set only via the constructor, thus through calling Collections.unmodifiableList which is by nature cannot be modified.
Has anyone encountered such an issue?
Personally I don't really want to copy the list again.