Solved! Go to Solution.
Views
Replies
Total Likes
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");
}
}
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?
Hello @Nesan -
Now we can definitely add exception to sonarQube to handle this but I would recommend raising an Adobe support case.
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");
}
}
Views
Likes
Replies