SonarQube Server v10.8 (100206)
SonarQube for IDE 10.14.1.80220
If I were to do this:
@Builder
public static class Example {
private Long id = 1L;
}
Then SonarQube would behave as expected and mark the id field as used. But a common pattern in my codebase is to provide a builder with defaults for a different class. This works like this:
@UtilityClass
public class ExampleFactory {
public static class ExampleBuilder {
private Long id = 1L;
}
@Builder
private Example build(Long id) {
var example = new Example();
example.setId(id);
return example;
}
}
Where @Builder
Delomoks to
@UtilityClass
public final class ExampleFactory {
@org.springframework.lang.NonNull
public static ExampleBuilder builder() {
return new ExampleBuilder();
}
public static class ExampleBuilder {
private Long id = 1L;
ExampleBuilder() {
}
@org.springframework.lang.NonNull
public ExampleBuilder id(Long id) {
this.id = id;
return this;
}
@org.springframework.lang.NonNull
public Example build() {
return ExampleFactory.build(this.id);
}
@org.springframework.lang.NonNull
public String toString() {
return "ExampleFactory.ExampleBuilder(id=" + this.id + ")";
}
}
private static Example build(Long id) {
var example = new Example();
example.setId(id);
return Example;
}
}
But Sonar does not seem to realize this and incorrectly flags the id field as not used (java:S1068). To be fair, IntelliJ itself does not seem to understand this either, so my full suppression that I’ve been using on the Builder classes looks like this:
@SuppressWarnings({"java:S1068", "unused", "FieldMayBeFinal", "MismatchedQueryAndUpdateOfCollection"})