当成员为 java.util.List 类型时序列化 Sonarqube 问题

Serializing Sonarqube issue when mebers are java.util.List type

我定义了可序列化 class 并且它有类型 java.util.List 的成员。在声纳中它显示错误为 "Fields in a "Serializable" class should either be transient or serializable"

但这些成员的实际实现是 ArrayLists,它们是可序列化的。

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

确保根据 RSPEC-1948:

Collection 字段标记为 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.

并指定类型而不是使用原始类型 ArrayList,例如:

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