Closed
Description
Regression in 4.9.0.
For the code below compiled with Java 21 Eclipse compiler, SpotBugs reports false positive:
Shared primitive variable "field" in one thread may not yield the value of the most recent write from another thread
with the AT_STALE_THREAD_WRITE_OF_PRIMITIVE
warning.
public class MyClass {
private boolean field;
public static enum MyEnum {
}
void method(MyEnum myEnum) {
field = false;
switch (myEnum) {
default:
}
}
public void print() {
System.out.println(field);
}
}
The analysis is wrong.
For the switch on enum value, ecj generates this field as volatile:
private static volatile synthetic int[] $SWITCH_TABLE$MyClass$MyEnum
Here, volatile
modifier that is put by compiler on synthetic SWITCH_TABLE
field (generated by compiler) should never qualify the class as potentially multi-threaded.
Fix is similar to #3306.
Synthetic fields shouldn't be considered for analysis.
I have a trivial patch that fixes the problem.