简体   繁体   English

当meber是java.util.List类型时序列化Sonarqube问题

[英]Serializing Sonarqube issue when mebers are java.util.List type

I have define serializable class and it has members of type java.util.List. 我已经定义了可序列化的类,它有java.util.List类型的成员。 In sonar it shows error as "Fields in a "Serializable" class should either be transient or serializable" 在声纳中,它显示错误为“可序列化”类中的字段应该是瞬态的或可序列化的“

But actual implementation of those members are ArrayLists, which are serializable. 但这些成员的实际实现是ArrayLists,它们是可序列化的。

public class TestDataClass implements Serializable {
    List<String> listMember = new ArrayList();
}

Do make sure to mark the Collection fields as private according to RSPEC-1948 : 确保根据RSPEC-1948Collection字段标记为private

This rule raises an issue on non-Serializable fields, and on collection fields when they are not private (because they could be assigned non-Serializable values externally), and when they are assigned non-Serializable types within the class. 此规则在非Serializable字段和集合字段上引发问题, 当它们不是私有时 (因为它们可以在外部分配非Serializable值),并且在类中分配非Serializable类型时。

and specify the type instead of using a raw type ArrayList , such as : 并指定类型而不是使用原始类型ArrayList ,例如:

private List<String> listMember = new ArrayList<>(); // notice '<>' for 'ArrayList<String>'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM