How can we fix the SonarQube issue in AEM where constructor injection should be used instead of field injection? We are encountering the mentioned issue when using the @Injection annotation. | Community
Skip to main content
Level 2
Jul 5, 2023 4:57 PM UTC
Solved

How can we fix the SonarQube issue in AEM where constructor injection should be used instead of field injection? We are encountering the mentioned issue when using the @Injection annotation.

No text available
This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Nishant-Singh
Nishant-SinghAccepted solution
Employee
Jul 6, 2023 5:58 AM UTC

It seems you are using Spring J2EE rules also in SonarQube. 

if Spring is not used then you can remove those rules.

if its really required to fix then you can write as below -

 

 

 

import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.ScriptVariable; import org.apache.sling.models.annotations.injectorspecific.SlingObject; import com.day.cq.wcm.api.Page; import javax.inject.Inject; import javax.inject.Named; @Model(adaptables = {SlingHttpServletRequest.class, Resource.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class TestModelConstructor { private String currentResourcePath; private String currentPagePagePath; private String requestParam; public TestModelConstructor( @ScriptVariable @Named("currentPage") final Page currentPage, @ScriptVariable @Named("resource") final Resource resource, @SlingObject @Named("slingHttpServletRequest") final SlingHttpServletRequest slingHttpServletRequest ) { currentResourcePath = resource.getPath(); currentPagePagePath = currentPage.getPath(); requestParam = slingHttpServletRequest.getParameter("myParam"); } }

 

 

 

 

4 replies

Nishant-Singh
Nishant-SinghAccepted solution
Employee
Jul 6, 2023 5:58 AM UTC

It seems you are using Spring J2EE rules also in SonarQube. 

if Spring is not used then you can remove those rules.

if its really required to fix then you can write as below -

 

 

 

import org.apache.sling.api.SlingHttpServletRequest; import org.apache.sling.api.resource.Resource; import org.apache.sling.models.annotations.DefaultInjectionStrategy; import org.apache.sling.models.annotations.Model; import org.apache.sling.models.annotations.injectorspecific.ScriptVariable; import org.apache.sling.models.annotations.injectorspecific.SlingObject; import com.day.cq.wcm.api.Page; import javax.inject.Inject; import javax.inject.Named; @Model(adaptables = {SlingHttpServletRequest.class, Resource.class}, defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL) public class TestModelConstructor { private String currentResourcePath; private String currentPagePagePath; private String requestParam; public TestModelConstructor( @ScriptVariable @Named("currentPage") final Page currentPage, @ScriptVariable @Named("resource") final Resource resource, @SlingObject @Named("slingHttpServletRequest") final SlingHttpServletRequest slingHttpServletRequest ) { currentResourcePath = resource.getPath(); currentPagePagePath = currentPage.getPath(); requestParam = slingHttpServletRequest.getParameter("myParam"); } }

 

 

 

 

Translate
Tanika02
Level 7
Jul 6, 2023 5:51 AM UTC

Hello @nesan - 

 

  1. Do you have the AEM RulesSet Enabled/Configured in SonarQube? As I have not seen this error in recent times while using SonarQube!
  2. Additionally, there is definitely a way how you can handle this via instead of using field injection, refactor the classes to use constructor injection. But it might not work pretty well if we do such refactoring at the Sling Model level or that would require higher efforts to handle.

Now we can definitely add exception to sonarQube to handle this but I would recommend raising an Adobe support case.

Translate
aanchal-sikka
New Participant
Jul 6, 2023 3:46 AM UTC

hello @nesan 

 

Requesting you to please share the code (atleast the snippets) and the exact error that Sonarqube is reporting.

Translate
Aanchal Sikka
EstebanBustamante
New Participant
Jul 5, 2023 9:04 PM UTC

A couple of things to check:
1) Are you using "javax.inject.Inject" annotation?

2) Are you using the specific annotation whenever possible instead of "Inject" all over the place?

 

I am not sure if you really want to use constructor injection instead, it maybe depends on the scenario, Can you post a sample code?

 
Translate
Esteban Bustamante