Expand my Community achievements bar.

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.

Avatar

Level 2
 
1 Accepted Solution

Avatar

Correct answer by
Employee

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");
    }
}

 

 

 

 

View solution in original post

4 Replies

Avatar

Community Advisor

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?

 


Esteban Bustamante

Avatar

Community Advisor

hello @Nesan 

 

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


Aanchal Sikka

Avatar

Community Advisor

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.

Avatar

Correct answer by
Employee

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");
    }
}