Override Drops Parent Synchronization
ID |
java.override_drops_synchronized |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
reliability |
Description
Reports a method that overrides a synchronized method declared in its direct parent class
without itself being synchronized, and without an equivalent synchronized block wrapping
its whole body.
Only the direct parent is examined; a grandparent’s synchronized method is out of scope. A
parent method that is private, or that differs from the override by the static modifier, is
not a real override relationship and is not compared. The reverse case — an override that adds
synchronized over a parent that does not have it — is not reported.
Rationale
Callers of the parent type rely on the thread-safety its synchronized contract promised. An
override that drops the modifier still compiles, still overrides correctly, and still satisfies
every functional test — but silently removes that guarantee for every caller who reaches the
method through the parent’s type.
public class Cache {
public synchronized void invalidate() {
// ...
}
}
public class LoggingCache extends Cache {
@Override
public void invalidate() { // no longer thread-safe
// ...
}
}