我们正在JDK 1.7中使用HashMap,在使用 SonarQube 进行代码审查时,我遇到了一些问题.
We are using HashMap in JDK 1.7 and I face some issue during the code review with SonarQube.
请考虑以下示例:
public class SerializationTest implements Serializable { private Map<String,String> test1=new HashMap<>(); //Serializeable private Map<ANEnum,String> test2=new HashMap<>(); //Serializeable private Map<String,ASerializeableObject> test3=new HashMap<>(); //Serializeable private Map<String,Map<String,String>> test4=new HashMap<>(); //Not Serializeable private Map<ANEnum,Map<String,String>> test5=new HashMap<>(); //Not Serializeable private Map<String,Map<String, ASerializeableObject>> test6=new HashMap<>(); //Not Serializeable声纳标记的最后三个HashMap标记为不是serializeable.声纳错误是(Make "test4" transient or serializable)
The Sonar mark last three HashMaps as not serializeable. The Sonar error is (Make "test4" transient or serializable)
据我猜想HashMap是serializeable,如果它的键和值是serializeable.但是似乎如果我将HashMap值设置为另一个HashMap,则原来的HashMap根本就不会是serializeable.
As far as I guessed the HashMap is serializeable if its key and value are serializeable. But it seems that if I set a HashMap value as another HashMap, the original HashMap will not be serializeable at all.
此声纳问题正确吗?!如果可以的话,我该如何解决?!
Is this Sonar Issue correct ?! If it is how can I fix it ?!
推荐答案让我们逐行查看每一行:
Let's see each line, one by one:
private Map<String,String> test1=new HashMap<>();键类型String是可序列化的.值类型String是可序列化的.具体的Map类型HashMap可序列化.所以一切都是可序列化的.
The key type, String, is serializable. The value type, String, is serializable. The concrete Map type, HashMap, is serializable. So everything is serializable.
private Map<ANEnum,String> test2=new HashMap<>();密钥类型ANEnum是可序列化的.值类型String是可序列化的.具体的Map类型HashMap可序列化.所以一切都是可序列化的.
The key type, ANEnum, is serializable. The value type, String, is serializable. The concrete Map type, HashMap, is serializable. So everything is serializable.
private Map<String,ASerializeableObject> test3=new HashMap<>();键类型String是可序列化的.值类型ASerializeableObject是可序列化的.具体的Map类型HashMap可序列化.所以一切都是可序列化的.
The key type, String, is serializable. The value type, ASerializeableObject, is serializable. The concrete Map type, HashMap, is serializable. So everything is serializable.
private Map<String,Map<String,String>> test4=new HashMap<>();键类型String是可序列化的.具体的Map类型HashMap可序列化. 但是值类型Map不能必需可序列化. Map的某些具体实现(例如HashMap)是可序列化的.其他一些则不是.因此,Sonar无法保证此字段可序列化,并发出警告.如果您确定只将可序列化的映射存储为值,则没问题.如果存储不可序列化的映射,则序列化将在运行时失败.
The key type, String, is serializable. The concrete Map type, HashMap, is serializable. But the value type, Map, is not necessarily serializable. Some concrete implementations of Map (like HashMap), are serializable. Some others are not. So Sonar can't guarantee that this field is serializable, and issues a warning. If you're sure that you will only store serializable maps as values, no problem. If you store non-serializable maps, then the serialization will fail at runtime.
private Map<ANEnum,Map<String,String>> test5=new HashMap<>(); //Not Serializeable与以前相同的解释
private Map<String,Map<String, ASerializeableObject>> test6=new HashMap<>();与以前相同的解释
请记住,Sonar只是一个工具,有时可以提供帮助,有时会造成阻碍.您应该控制住一切,并决定是否通过警告来改变自己.
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.
更多推荐
Java为什么无法对Map的地图(例如:Map< String,Map< String,String>>
发布评论