[Java] Add another exception for “Resources should be closed” (S2095)

S2095 finds resources that have not been closed, but also has exceptions for classes where close() has no effect (like java.io.StringReader and java.io.ByteArrayOutputStream).

I would like to request that org.apache.commons.io.output.NullOutputStream is added to the exceptions list. It does not override the no-op close() method in java.io.OutputStream.

And, if possible, it would be cool if it could auto-ignore any class that has a parent with a no-op close() method and doesn’t override it. This would have caught Apache’s NullOutputStream without needing to add an explicit exclusion. Just an idea…

Hello @tobinibot, thanks for the feedback.

The exceptions for S2095 is not meant to be exhaustive, it would not be possible to keep such a list up-to-date, with all possible dependencies. This is exactly why you can provide a list of excluded ressources to the rule, preventing it to report an issue for them.

And, if possible, it would be cool if it could auto-ignore any class that has a parent with a no-op close() method and doesn’t override it.

It would be a way to automatically detect some of them, but I’m afraid it’s not possible to do this yet.
I don’t expect this case to be so frequent, I think the current situation is good enough.

In any case, if you have a good example where the exceptions list is not enough, or is unmanageable, feel free to continue the discussion.

Best,
Quentin

Thanks Quentin, I didn’t realize we could add exceptions. I just figured out how to do that, and this is a perfect solution for us, thanks!

1 Like