java:S3305 don't report field if bean creation method is used i multiple places

Thanks for your great work.

This is about java:S3305:

Noncompliant Code Example
  public class ​FooConfiguration {
      @Autowired private ​DataSource dataSource​; // Noncompliant

      @Bean    public ​MyService myService() {
      return new ​MyService(this​.dataSource​);

It makes total sense to move the Autowired field into the bean method. However, there is one exception I’d like to see added: If myService() is used in one or more other bean method:

      @Bean    public ​MyOtherService myOtherService() {
      return new ​MyOtherService(myService()​); // don't report "field only used in myService" because then I'd have to declare the Autowired in myOtherService as well.

Hope the idea is clear,



Hey @Jan47

Please read this thread regarding reporting false-positives:

(We’re missing what product you’re using and what version if applicable)

SonarList for IntelliJ

Hi @Jan47,

Thank you for reporting.

If I understand well, in your full example you don’t want rule S3305 to report an issue. I.e., the autowired field should be left untouched. Right?

Yes, in case the method is used in another method I would recommend to not report the field.

Thanks again for your effort and let me know if you have further questions.

Ticket created: [SONARJAVA-4312] - Jira

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