Running Sonar over this code:
@Configuration
public class SomethingConfiguration {
@Autowired(required = false)
@SuppressWarnings("java:S3305")
private @NonNull List<Thing> things = Collections.emptyList();
@Bean
public Widget widget() {
return new Widget(things);
}
Gives this false positive violation:
[rank 3] [java:S3305] /home/candrews/projects/Dashboard/src/main/java/mil/af/kesselrun/appops/mattermost/MattermostConfiguration.java:29:47
Factory method injection should be used in "@Configuration" classes
See https://rules.sonarsource.com/java/RSPEC-3305
This violation should not occur when there’s an initializer on the @Autowired
field (= Collections.emptyList();
in this example) because that’s how non-null defaults are accomplished. For reference, see https://stackoverflow.com/questions/19299114/is-there-an-easy-way-to-autowire-empty-collection-if-no-beans-present-in-spring/31327710
I’m using sonarlint version 4.13.0.19712.