Asymmetric Getter/Setter Synchronization
ID |
java.getters_setters_sync_pairs |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
reliability |
Description
Within one type, reports a getter/setter pair for the same property where exactly one side is
synchronized. Synchronized means either the synchronized modifier on the method, or a
synchronized block anywhere in the method’s body — a block that wraps only part of the body
still counts, matching how the two sides are meant to be equivalent.
Both sides synchronized, or neither, is compliant. A static accessor is never paired with an instance accessor of the same name, and a pair split across a class hierarchy is not matched — only accessors declared in the same type are compared.
Rationale
Callers rely on a getter/setter pair being atomic together: if the setter is synchronized but the getter is not, a reader can observe a value mid-update, and the inconsistency only shows up under concurrent load.
public synchronized void setBalance(double balance) {
this.balance = balance;
}
public double getBalance() { // reads without the lock the setter uses
return this.balance;
}