Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Sonar want a LocalDateTime marked transient when it's already serializable?

Tags:

java

sonarqube

I've got a sonar plugin to Eclipse, and it's giving me a

Make this value-based field transient so it is not included in the serialization of this class

on a LocalDateTime object. What I do not get is, LocalDateTime is definitely serializable. Here's the class

 public final class LocalDateTime
        implements Temporal, TemporalAdjuster, ChronoLocalDateTime<LocalDate>, Serializable {

Anyone have any ideas? Do I just not understand what transient means? Normally I wouldn't pay much attention, but I'm weirdly able to serialize it in a Get request, but not deserialize it in a post request and I'm wondering if it's related to this.

like image 619
Steve Avatar asked May 11 '18 15:05

Steve


People also ask

How do you make a list transient or serializable?

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 .

What happens if your serializable class contains a member which is not serializable how do you fix it?

It'll throw a NotSerializableException when you try to Serialize it. To avoid that, make that field a transient field.

How do you make a transient variable serializable in Java?

At the time of serialization, if you don't want to save the value of a particular variable in a file, then use the transient keyword. transient private <member variable>; In case you define any data member as transient, it will not be serialized. This is because every field marked as transient will not be serialized.

Can a serializable class contains a non serializable field in Java?

the non-serializable field's Class must have an API to allow getting it's state (for writing to the object stream) and then instantiating a new instance with that state (when later reading from the object stream.)

What is the use of transient in Java?

At the time of serialization, if we don’t want to save value of a particular variable in a file, then we use transient keyword. When JVM comes across transient keyword, it ignores original value of the variable and save default value of that variable data type.

Is there any compile-time error when declaring final variable as transient?

However there is no compilation error. transient and final : final variables are directly serialized by their values, so there is no use/impact of declaring final variable as transient. There is no compile-time error though.

What is the use of localdatetime in it?

It implements the ChronoLocalDateTime interface and inherits the object class. Wherever we need to represent time without a timezone reference, we can use the LocalDateTime instances.

What is the use of transient keyword with static variables?

It is good habit to use transient keyword with private confidential fields of a class during serialization. transient and static : Since static fields are not part of state of the object, there is no use/impact of using transient keyword with static variables. However there is no compilation error.


1 Answers

According to Sonarsource rules explorer :

Fields in a Serializable class must themselves be either Serializable or transient even if the class is never explicitly serialized or deserialized. For instance, under load, most J2EE application frameworks flush objects to disk, and an allegedly Serializable object with non-transient, non-serializable data members could cause program crashes, and open the door to attackers. In general a Serializable class is expected to fulfil its contract and not have an unexpected behaviour when an instance is serialized.

However as you've said the LocalDateTime is serializable. This means that sometimes Sonar rules are not good (when using Hibernate with Objects that contains LocalDateTime which is Serializable).

One solution would be to use your own rules instead of Sonar default ones. Another one (this is a workaround more than a solution) would be to annotate your field with : @SuppressWarnings("squid:S3437")

like image 109
Abdelghani Roussi Avatar answered Sep 18 '22 17:09

Abdelghani Roussi