rspec/rules/S6219/java/rule.adoc

55 lines
1.6 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
In Records serialization is not done the same way as for ordinary serializable or externalizable classes. Records serialization does not rely on the ``++serialVersionUID++`` field, because the requirement to have this field equal is waived for record classes. By default, all records will have this field equal to ``++0L++`` and there is no need to specify this field with ``++0L++`` value and it is possible to specify it with some custom value to support serialization/deserialization involving ordinary classes.
This rule raises an issue when there is a ``++private static final long serialVersionUID++`` field which is set to ``++0L++`` in a Record class.
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
record Person(String name, int age) implements Serializable {
@Serial
private static final long serialVersionUID = 0L; // Noncompliant
}
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
record Person(String name, int age) implements Serializable {} // Compliant
record Person(String name, int age) implements Serializable {
@Serial
private static final long serialVersionUID = 42L; // Compliant
}
----
== Resources
2021-04-28 16:49:39 +02:00
* https://docs.oracle.com/javase/specs/jls/se16/html/jls-8.html#jls-8.10[Records specification]
* https://docs.oracle.com/en/java/javase/16/docs/specs/serialization/serial-arch.html#serialization-of-records[Serialization of records]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this redundant "serialVersionUID" field
=== Highlighting
the 'serialVersionUID' field declaration
endif::env-github,rspecator-view[]