
Inline adoc files when they are included exactly once. Also fix language tags because this inlining gives us better information on what language the code is written in.
60 lines
2.3 KiB
Plaintext
60 lines
2.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Providing a ``++serialVersionUID++`` field on ``++Serializable++`` classes is strongly recommended by the ``++Serializable++`` documentation but blindly following that recommendation can be harmful.
|
|
|
|
|
|
``++serialVersionUID++`` value is stored with the serialized data and this field is verified when deserializing the data to ensure that the code reading the data is compatible with the serialized data. In case of failure, it means the serialized data and the code are not in sync and this fine because you know what's wrong.
|
|
|
|
When the ``++serialVersionUID++`` is generated by an IDE or blindly hard-coded, there is a high probability that one will forget to update the ``++serialVersionUID++`` value when the ``++Serializable++`` class is later enriched with additional fields. As a consequence, old serialized data will incorrectly be considered compatible with the newer version of the code creating situations which are hard to debug.
|
|
|
|
|
|
Therefore, defining ``++serialVersionUID++`` should be done with care. This rule raises an issue on each ``++serialVersionUID++`` field declared on classes implementing ``++Serializable++`` to be sure the presence and the value of the ``++serialVersionUID++`` field is challenged and validated by the team.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,java]
|
|
----
|
|
public class Foo implements Serializable {
|
|
private static final long serialVersionUID = 1;
|
|
}
|
|
|
|
public class BarException extends RuntimeException {
|
|
private static final long serialVersionUID = 8582433437601788991L;
|
|
}
|
|
----
|
|
|
|
|
|
== Resources
|
|
|
|
* Vojtech Ruzicka's Programming Blog: https://www.vojtechruzicka.com/explicitly-declare-serialversionuid/[Should I explicitly declare serialVersionUID?]
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Remove this "serialVersionUID"
|
|
|
|
|
|
=== Highlighting
|
|
|
|
"serialVersionUID"
|
|
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== on 1 Dec 2018, 10:37:49 Jens Bannmann wrote:
|
|
The "serialization" tag https://jira.sonarsource.com/issues/?jql=project%20%3D%20RSPEC%20AND%20status%20%3D%20Active%20AND%20labels%20%3D%20serialization%20ORDER%20BY%20key[currently includes 14 rules], but this one is surprisingly missing. I suggest adding it.
|
|
|
|
=== on 3 Dec 2018, 15:19:54 Ann Campbell wrote:
|
|
Thanks [~bannmann]. Oversight fixed.
|
|
|
|
endif::env-github,rspecator-view[]
|