Cloud Manager code quality · Java code rules (SonarQube) · updated
InjectAnnotationWithOptionalInjectionCheck: Do not use @Inject annotation with @Optional in Sling Models
This rule flags Sling Models that use the generic @Inject annotation with an OPTIONAL injection strategy. Replace @Inject with the specific injector annotation, such as @ValueMapValue, @ChildResource or @OSGiService, so Sling does not try every injector for every field.
| Rule key | InjectAnnotationWithOptionalInjectionCheck |
|---|---|
| Adobe's rule name | Do not use @Inject annotation with @Optional in Sling Models |
| Type | Software quality |
| Severity | Minor |
| Since | 2023.11 |
| Checked by | SonarQube analysis of your Java code |
Why Cloud Manager flags it
A generic @Inject makes Sling Models ask each registered injector in turn (value map, child resources, request attributes, OSGi services, script bindings and so on) until one returns a value. With a required field that search stops at the first hit. With an optional field and no value, every injector is tried and all fail, on every adaptation. On a page with dozens of components that adds measurable render time, which is why Adobe calls it a performance problem.
Adobe's page names @ValueMapValue and @OsgiInjector as examples of more specific annotations (the Sling Models annotation for services is @OSGiService). The page gives no code sample; the table maps common fields.
Which annotation to use
| Field reads | Annotation |
|---|---|
| A property of the component node | @ValueMapValue |
| A child node | @ChildResource |
| An OSGi service | @OSGiService |
| The current resource or request | @Self or @SlingObject |
| A script binding such as currentPage | @ScriptVariable |
| A request attribute | @RequestAttribute |
Example
Non-compliant
@Model(adaptables = Resource.class)
public class Teaser {
@Inject @Optional
private String title;
}Compliant
@Model(adaptables = Resource.class,
defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL)
public class Teaser {
@ValueMapValue
private String title;
}How to fix it
- Search for @Inject in model classes, including those with defaultInjectionStrategy = OPTIONAL on @Model.
- Replace each with the injector-specific annotation for where the value really comes from.
- Run the component's unit tests (AEM Mocks) to confirm the values are still injected.
- Measure a heavy page before and after; the gain is largest on list and navigation components.
Does it stop the pipeline?
Code smells feed the Maintainability Rating, which is based on the total estimated fix time as a share of development time, not on any single finding. One finding rarely moves it, but hundreds can push the rating below A, which is an important failure: the pipeline pauses until someone overrides or fixes it.
If a finding is a genuine false positive, Adobe's code quality page lets you annotate the code with @SuppressWarnings and the rule ID. In a full-stack pipeline, important failures can be overridden by a deployment manager, project manager or business lead; in a code-quality-only pipeline they cannot.
Questions
Is @Inject on a required field also flagged?
Adobe's rule targets @Inject with the optional strategy. Required @Inject is still slower than a specific annotation, so it is worth converting while you are there.
Does @Default still work with @ValueMapValue?
Yes. @Default(values = "...") works with the injector-specific annotations.
Can this be automated?
Largely. When the field type and name make the source obvious (a String from the value map), the change is mechanical; services and request attributes need a quick check.
See the Cloud Service blockers in your code before Cloud Manager shows them. The free Surge Migrate readiness scan reads a repository zip or a Best Practices Analyzer CSV in your browser and lists them with file, fix and effort. Nothing is uploaded.
Sources
- Adobe: Custom code quality rules (checked 10 Nov 2026)
- Adobe: Code quality testing in Cloud Manager (checked 10 Nov 2026)
Rule key, type, severity and version are as listed on Adobe's page, checked 10 Nov 2026. Explanations and examples are ours unless marked as Adobe's.