Support for defaults in "Factory method injection should be used in "@Configuration" classes" rule

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.

Hello @candrews,

Thanks for the feedback and sorry for the delay.

It makes sense to me, ticket created: SONARJAVA-3605.

Best,
Quentin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.