- Language: Java
- Rule: java:S1258 “Classes and enums with private fields should have a constructor”
- Running SonarLint 10.4.0.82051 under Eclipse 2024-06 (4.32) together with lombok v1.18.32
Consider the following snippet:
@lombok.experimental.UtilityClass
public class Foo {
private final String bar; // Implicitly static because of @UtilityClass
static {
bar = "bar";
}
}
The rule wrongly marks the class declaration as needing a constructor in order to initialize the field bar
, which is static because of @UtilityClass
and is already initialized in the static initializer.
The warning disappears if the field has visibility other than private (including package-private), or if the field is explicitly marked as static
(which is redundant with the annotation’s purpose), which means that the rule is disregarding @UtilityClass
and thinking that the field bar
is not static
.
This false positive may be related to this one about @Builder
and @SuperBuilder
and this one also about those annotations.