A constructor calls a method that a subclass can override before the subclass is set up
What it finds
A constructor that calls a method its subclasses can override runs the subclass's version before the subclass's own constructor has run: the override sees its fields still at their defaults, null and zero, and may fail or record wrong state.
Reported are calls, without a receiver or on this, of methods the class itself declares that are neither private, final nor static, in constructors of classes that are not final.
From the rule's source, overridable_call_in_constructor.rs.
class Widget { private final String name; Widget(String name) { this.name = name; init();tx0 reports: the constructor calls the overridable method init this.layout();tx0 reports: the constructor calls the overridable method layout setup(); } protected void init() {} void layout() {} private void setup() {}} final class Sealed { Sealed() { init(); } void init() {}} 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:overridable-call-in-constructor" = "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:overridable-call-in-constructor] reason="..."tx0 explain tx0/java:overridable-call-in-constructor shows the same in the terminal. Found a false positive? Report it.