A getter or setter reads or writes a different field than the one its name says
What it finds
getWidth() { return height; } or setWidth(int w) { this.height = w; } in a class that has a width field reads or writes the wrong field, typically after copying the accessor of another field. Callers get or change the wrong value with no error.
Reported are getters and setters whose whole body returns or assigns a field of the class other than the one their name says, when the class declares an instance field of that name. A field whose name contains the property, as lastWidth for getWidth, is taken as a deliberate stand-in, and static fields are left alone.
From the rule's source, accessor_wrong_field.rs.
class Box { private int width; private int height; int getWidth() {tx0 reports: getWidth returns height, not width return height; } int getHeight() { return height; } void setWidth(int w) {tx0 reports: setWidth sets height, not width this.height = w; } void setHeight(int h) { this.height = h; }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/java:accessor-wrong-field" = "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:accessor-wrong-field] reason="..."tx0 explain tx0/java:accessor-wrong-field shows the same in the terminal. Found a false positive? Report it.