rspec/rules/S2675/java/rule.adoc
2023-07-03 13:46:55 +02:00

52 lines
1.3 KiB
Plaintext

== Why is this an issue?
The `readObject` method is implemented when a `Serializable` object requires special handling to be reconstructed from a file.
The object created by `readObject` is accessed only by the thread that called the method,
thus using the `synchronized` keyword in this context is unnecessary and causes confusion.
=== Noncompliant code example
[source,java,diff-id=1,diff-type=noncompliant]
----
private synchronized void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException { // Noncompliant
//...
}
----
=== Compliant solution
[source,java,diff-id=1,diff-type=compliant]
----
private void readObject(java.io.ObjectInputStream in)
throws IOException, ClassNotFoundException { // Compliant
//...
}
----
== Resources
=== Documentation
* https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/io/Serializable.html[Oracle SDK 20 - Serializable]
* https://docs.oracle.com/en/java/javase/20/docs/api/java.base/java/io/ObjectInputStream.html[Oracle SDK 20 - ObjectInputStream]
=== Articles & blog posts
* https://www.baeldung.com/java-serialization[Serialization in Java]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove the "synchronized" keyword from this method
endif::env-github,rspecator-view[]