Sonar "Make transient or serializable" error
Asked Answered
S

6

21

I have the following serializable class (implements serializable):

public class Test implements Serializable{

private String id;

private Map<String,Object> otherProperties;

}

However , it seems like this property is causing some problems with serialization :

enter image description here

How can I solve this problem ?

Also , is there any downside in not making this transient or serializable ? Will I be able to serialize this class fully ?

Scrutable answered 9/12, 2019 at 16:20 Comment(1)
rules.sonarsource.com/java/RSPEC-1948Semitropical
T
27

The Map interface does not extend the Serializable interface, which is why Sonar is warning you.

When serializing an instance of Test, you must choose whether or not you want otherProperties to be serialized.

If you don't want to serialize otherProperties, then the field should be marked as transient:

private transient Map<String, Object> otherProperties;

Otherwise, you can change the type of otherProperties to an implementation of Map that implements Serializable, such as HashMap.

Trousseau answered 9/12, 2019 at 16:24 Comment(3)
If we change the otherProperties to the type HashMap or anyother implementation of Map interface, sonar will raise another issue to force us to use the Interface type in variables.Syndesmosis
if we do this then in Criteria Mata model that filed will be deleted .Insomnia
Added HashMap but this still does not work for me since Sonar does not know if Object is serializable.Lockhart
O
5

Add this 2 methods to your class:

 private void writeObject(java.io.ObjectOutputStream stream)
        throws IOException {
    stream.defaultWriteObject();
}

private void readObject(java.io.ObjectInputStream stream)
        throws IOException, ClassNotFoundException {
    stream.defaultReadObject();
}
Ombudsman answered 30/11, 2022 at 11:21 Comment(1)
This, and plus annotation on those methods: @SerialScapegrace
F
5

Change Object to Serializable and then Sonar should be happy.

private Map<String, Serializable> otherProperties;

Since your class is Serializable its fields also should be Serializable.

Faxon answered 18/7, 2023 at 20:13 Comment(1)
If you're injecting a list of types implementing an interface you can't just replace your types by Serializable thoughFemmine
D
2

If it's an option (for instance, if you are using Jackson to (de)serialize your class), remove the Serializable marker from your Test class. The serialization will still work and you will get rid of the warning in Sonar.

Declamation answered 2/4, 2021 at 8:42 Comment(0)
T
2

I was having a similar issue but in contrast to Nexussim Lements I've been using an interface that gets serialized but still SonarLint showed me a warning:

public class BasicCheck implements Serializable { ... }

public class Case implements Serializable {
   private BasicCheck basicCheck; // Sonarlint throws warning here
   ...
}

Luckily I've found FAlfeao's solution to be working. All I had to do was to add these two methods to my Case-class:

private void writeObject(java.io.ObjectOutputStream stream)
        throws IOException {
    stream.defaultWriteObject();
}

private void readObject(java.io.ObjectInputStream stream)
        throws IOException, ClassNotFoundException {
    stream.defaultReadObject();
}
Thoroughwort answered 19/12, 2022 at 12:28 Comment(0)
D
0

Map value type should be specified to extends Serialzable:

public class Test<T extends Serializable> implements Serializable{

private String id;

private Map<String,T> otherProperties;

}
Deafanddumb answered 15/9, 2022 at 2:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.