4

I am using SonarQube 6.7.3 with Sonar java plugin 5.3

I have a Serializable java class with a map as an instance variable. 1. Map is showing S1948 rule violation when initialized with null explicitly. 2. Map is not showing any violation when that explicit null initialization is removed.

Same can be seen below screenshot. Can you help me understand the difference between the two.

Screen shot: enter image description here

arkay
  • 149
  • 1
  • 12

1 Answers1

2

Some implementations of Map are serializable, others are not. So if you initialize it as a null, it will raise the rule as Sonar doesn't have a way to know what implementation you're going to use.

This post has it all explained: Java why a Map of Map (ex: Map<String,Map<String,String>>) not serializeable

brunags
  • 292
  • 2
  • 7
  • The link was very clear in its context but how does it applies to my question. How an explicit initialization of null matters – arkay Jun 19 '18 at 13:33
  • 1
    It totally matters. At compile time sonar can't guarantee serialization as you're not using a serializable implementation of Map (null is not a Serializable implementation of Map). So as long as SONAR knows, that is not serializable. But JB's answer has another very good point: "Remember that Sonar is only a tool, which can sometimes help, and sometimes get in the way. You should be in control, and decide if a warning should make you change things, or not" – brunags Jun 19 '18 at 16:34