False positive of S3242 on Spring @Autowired setters?

160 views
Skip to first unread message

Tobias Gruetzmacher

unread,
Mar 2, 2018, 11:17:21 AM3/2/18
to SonarQube
Hi,

The following code:

  @Autowired (required = false)
 
public void setQueues (Collection<SomeBeanType> queues) {
   
for (SomeBeanType queue: queues) {
     
this.queues.add (queue);
   
}
 
}

triggers https://jira.sonarsource.com/browse/RSPEC-3242 - "Method parameters should be declared with base types" and suggests replacing "Collection" with "Iterable". Unfortunatly, Spring can only autowire Collection or Map (see https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Autowired.html) - so would a developer follow the recommendation of this rule, they would break autowiring. I would suggest restricting the rule on methods annotated with @Autowired to only suggest "Collection" or "Map" (I think warning on List and Set is still okay).

Regards, Tobias

Nicolas Peru

unread,
Mar 16, 2018, 4:57:08 AM3/16/18
to Tobias Gruetzmacher, SonarQube
Hi Tobias, 

Thanks for the feedback ! 

Cheers, 

--
You received this message because you are subscribed to the Google Groups "SonarQube" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sonarqube+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/sonarqube/5767bc5a-8b58-40d9-8d00-cbf10f177efa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
Nicolas Peru | SonarSource
Reply all
Reply to author
Forward
0 new messages