writeObject is the only synchronized code of a class, which guards serialization against changes that nothing else is guarded against

tx0/java:lone-synchronized-write-objectrecommendedwarningmaintainability/designdepth fastsmell

What it finds

Making writeObject synchronized only helps if the code that changes the object holds the same lock. When nothing else in the class is synchronized, the lock serializes a consistent copy of nothing in particular and costs a monitor on each write.

Reported is a synchronized writeObject in a class with no other synchronized method or block.

From the rule's source, lone_synchronized_write_object.rs.

Example from the rule's tests
import java.io.ObjectOutputStream;import java.io.Serializable; class Saved implements Serializable {  int value;  private synchronized void writeObject(ObjectOutputStream out) throws java.io.IOException {tx0 reports: nothing else    out.defaultWriteObject();  }}class Guarded implements Serializable {  int value;  synchronized void set(int v) { value = v; }  private synchronized void writeObject(ObjectOutputStream out) throws java.io.IOException {    out.defaultWriteObject();  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:lone-synchronized-write-object" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:lone-synchronized-write-object] reason="..."

tx0 explain tx0/java:lone-synchronized-write-object shows the same in the terminal. Found a false positive? Report it.