Reputation: 378
I have define serializable class and it has members of type 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.
public class TestDataClass implements Serializable {
List<String> listMember = new ArrayList();
}
Upvotes: 4
Views: 1834
Reputation: 31968
Do make sure to mark the Collection
fields as private
according to RSPEC-1948:
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.
and specify the type instead of using a raw type ArrayList
, such as :
private List<String> listMember = new ArrayList<>(); // notice '<>' for 'ArrayList<String>'
Upvotes: 1