An instance method or constructor assigns a static field, which all instances share
What it finds
A static field is shared by every instance. When an instance method or constructor assigns it, the last object to run wins, and instances on different threads race on it, as the Java memory model allows. It is usually an instance field declared static by mistake, or a cache that needs synchronization.
Reported is an assignment, increment or decrement of a static, non-final field of the class inside an instance method or a constructor.
From the rule's source, static_written_from_instance.rs.
class Counter { private static int created; private static final int LIMIT = 10; Counter() { created++;tx0 reports: the static field created is assigned from instance code } void reset() { created = 0;tx0 reports: assigned from instance code } static void clear() { created = 0; }} 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:static-written-from-instance" = "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:static-written-from-instance] reason="..."tx0 explain tx0/java:static-written-from-instance shows the same in the terminal. Found a false positive? Report it.