I have a java class that implements serializable and I'm assuming the variable within the class would also be serialized but SonarQube is complaining to me that it is not.
My snippet of code is shown below:
I have a java class that implements serializable and I'm assuming the variable within the class would also be serialized but SonarQube is complaining to me that it is not.
My snippet of code is shown below:
SonarQube marked this line as an error, because java.util.List doesn't implement java.io.Serializable. java.util.ArrayList is serializable, but the bondAxeMarkQuoteUpdates
is protected
so somebody can assign other non-serializable list to it (e.g. in a subclass).
To solve the problem you can:
transient
, but it will be ignored during serializationprivate
, so SonarQube can verify that nobody assigned non-serializable list to itI receive the same error and the solution was turn the class used on the variable as Serializable
.
For example, this show an error because Object
is not Serializable
:
private Map<String, Object> map = new HashMap<>();
The simplest solution in the case was turn the second parameter Serializable
. So, you could use:
private Map<String, Serializable> map = new HashMap<>();
If you are using your own class (instead of Object
), you can put the class to implements Serializable
.
As stated in the rule documentation (that you can open clicking on the ... in your screenshot) : https://sonarqube.com/coding_rules#rule_key=squid%3AS1948
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)