Join GitHub today
[all] Replacing IOUtils.closeQuietly(foo) with try-with-resources statements #1401
There were two remaining issues that I was unsure of how to address:
Please let me know if I should handle the remaining issues in any way, and if there are any other issues with the code I have changed.
referenced this pull request
Oct 22, 2018
changed the title from
Replacing IOUtils.closeQuietly(foo) with try-with-resources statements
[all] Replacing IOUtils.closeQuietly(foo) with try-with-resources statements
Oct 22, 2018
@hooperbloob There is CloseResource that detects if you don't call
Looks like we now have #1405 to request a new rule that will detect
Without a deeper look: Maybe we should make RuleSetWriter itself implement AutoClosable? Then we can use a try-with-resources when RuleSetWriter is used. We'd still need to either use closeQuietly here or implement it manually ourselves (if we want to get rid of the deprecated method call).
Theoretically an error would only occur, if the File is not found, which could happen when creating the FileOutputStream. So I'm not sure, whether we need to close anything at all in the error case. Since the method already throws IOException, I guess, we should just let it throw. The method returns a Writer, and the caller is then responsible for closing it.
Hm... closing in the flush method sounds wrong. We probably should provide an explicit close method or document, that a renderer is not responsible for closing the underlying writer...
I guess, there is not much to do about it. We could make the method more specific for PMD's use case: We use this method twice and check before, whether the classloader is ours, so we could move the check inside the method (instanceof ClasspathClassLoader). But we still need to close the classloader explicitly (also because ClassLoader is not Closable by default, so we can't use it in a try-with-resources...)
I'll look at your updated PR later today - thanks for your work!