A collection or buffer that locks on every call is created
What it finds
Vector, Hashtable, Stack and StringBuffer lock on every call. Code that uses them from one thread pays for that each time, and code that shares them between threads still needs its own locking for anything that takes more than one call. ArrayList, HashMap, ArrayDeque and StringBuilder do the same work without the locks.
From the rule's source, legacy_synchronized_class.rs.
import java.util.Hashtable;import java.util.Vector; class Old { Vector<String> names = new Vector<>();tx0 reports: Vector locks on every call Hashtable<String, Integer> ids = new Hashtable<>();tx0 reports: Hashtable locks on every call StringBuffer text = new StringBuffer();tx0 reports: StringBuffer locks on every call StringBuilder fine = new StringBuilder();} Turn it on, off or suppress it
The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:legacy-synchronized-class" = "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:legacy-synchronized-class] reason="..."tx0 explain tx0/java:legacy-synchronized-class shows the same in the terminal. Found a false positive? Report it.