You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@sling.apache.org by "Carsten Ziegeler (Jira)" <ji...@apache.org> on 2022/11/02 07:22:00 UTC

[jira] [Comment Edited] (SLING-11507) Field injection should not inject static fields

    [ https://issues.apache.org/jira/browse/SLING-11507?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17627496#comment-17627496 ] 

Carsten Ziegeler edited comment on SLING-11507 at 11/2/22 7:21 AM:
-------------------------------------------------------------------

[~rombert] True, a static method could be written in a thread-safe way, however - unless I'm overlooking something - the methods never get the instance passed in as an argument. So the only thing a static method could do is tell the world that it got invoked or potentially setting up a shared resource for the instances when invoked for the first time. However, these look like anti-patterns to me as well.

For annotation scanning, I think we need to scan for both "inject annotations". There might even be more annotations to look at (see https://sling.apache.org/documentation/bundles/models.htm) 
It seems all Model classes need to have the class level "Model" annotation, so we can probably restrict scanning to those classes


was (Author: cziegeler):
[~rombert] True, a static method could be written in a thread-safe way, however - unless I'm overlooking something - the methods never get the instance passed in as an argument. So the only thing a static method could do is tell the world that it got invoked or potentially setting up a shared resource for the instances when invoked for the first time. However, these look like anti-patterns to me as well.

For annotation scanning, I think we need to scan for both "inject annotations". Not sure, if the class needs to be annotated with "Model" or whether you can pass in any class into the machinery?

> Field injection should not inject static fields
> -----------------------------------------------
>
>                 Key: SLING-11507
>                 URL: https://issues.apache.org/jira/browse/SLING-11507
>             Project: Sling
>          Issue Type: Task
>          Components: Sling Models
>    Affects Versions: Models Impl 1.4.14
>            Reporter: Joerg Hoh
>            Priority: Major
>             Fix For: Sling Models Implementation 1.5
>
>          Time Spent: 0.5h
>  Remaining Estimate: 0h
>
> Currently this injection works:
> {noformat}
>         @SlingObject
>         private static ResourceResolver resourceResolver;
> {noformat}
> but it should not. Sling Models are Pojos and injection must never inject into static fields. Instead it should throw an exception and an error message indicating the problem (another option would be to refuse the injection with a proper error message).



--
This message was sent by Atlassian Jira
(v8.20.10#820010)