Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discussing rule AEM-20 "ResourceResolver can be closed using try-with-resources" #191

Open
joerghoh opened this issue May 15, 2020 · 1 comment
Labels
bug rule New rule or modification of existing one.

Comments

@joerghoh
Copy link
Contributor

joerghoh commented May 15, 2020

I think that the current implementation displays quite a lot of false positives, which diminish the value of this rule right now.

Look at this example (https://github.com/Adobe-Consulting-Services/acs-aem-commons/blob/4b3da3f5fdf0b59049b966bea9306d24c87d0293/bundle/src/main/java/com/adobe/acs/commons/audit_log_search/impl/AuditLogSearchServlet.java#L64)

protected final void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
            throws ServletException, IOException {
[...]
ResourceResolver resolver = request.getResourceResolver();
[...]

This line is pointed out with this rule.
But actually it does not apply here, as this is a simple assignment, and no ResourecResolver has been opened here. When you close the RR as suggested (by whatever means) you are doing wrong. Because you did not open the RR here, but you just did an assignment to a local variable.

This rule as it is today is not helpful, because blindly following the rules will cause developers to introduce functional issues in their code.

Instead only methods, which clearly open a new RR should be considered. Given the number of patterns out there, which are used for it, the rules are likely to get much more complex. Consider these patterns:

ResourceResolver rr = resourceResolverFactory.login[...];

and

ResourceResolver rr = HelperService.getNewResourceResolver();

and

ResourceResolver getNewResourceResolver() {
  [...]
  ResourceResolver rr = resourceResolverFactory.login[...]
  return rr;
}

I would be great if you could point any issue clearly; even if that means, that we cannot detect all possible situations where a RR will remains unclosed.

@toniedzwiedz toniedzwiedz added bug rule New rule or modification of existing one. labels May 22, 2020
@toniedzwiedz
Copy link
Collaborator

Hi @joerghoh, I agree that the rule is somewhat troublesome for a number of reasons, some of which I've described in #188

I'm of a mind to get rid of it. If other rules pertaining to Autocloseable resources fail to pick up Sling-specific examples, we can revisit bringing the rule back and make sure to use the examples you provided as unit tests to avoid false positives arising.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug rule New rule or modification of existing one.
Projects
None yet
Development

No branches or pull requests

2 participants