6

My team has quite a huge amount of code. Recently I have found some objects that weren't closed properly.
How can i find all the instances that are not closed or not inside a try-with-resources block?
Some objects like Statement, ResultSet are not showing warning messages for it, even.

Is there an extension tool for showing all this occurrences?
I'm using Eclipse.

walen
  • 7,103
  • 2
  • 37
  • 58
Hard Worker
  • 995
  • 11
  • 33
  • AFAIK try with resources promises to close resources, so this should not be happening there. But what about the old school `try`-`catch` blocks, do you have any of those as well? – Tim Biegeleisen Feb 14 '17 at 08:18
  • 1
    @Tim TWR only guarantees to close the "named" resources. For example, `try (BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream("..."))) {}`, only the `BufferedReader` is guaranteed to be closed. The FIS and ISR might not be, if construction of the BR fails. – Andy Turner Feb 14 '17 at 08:22
  • So then any resource block could have a problem, if I understand you correctly. – Tim Biegeleisen Feb 14 '17 at 08:23
  • 1
    Possible duplicate of http://stackoverflow.com/questions/7343158/how-to-find-unclosed-i-o-resources-in-java. – Andy Turner Feb 14 '17 at 08:25
  • As one of possible solutions in IDEA: show hierarchy of `Closeable` interface (default - ctrl+H) and find usages (alt+F7) of each class from hierarchy in project – cybersoft Feb 14 '17 at 08:49

1 Answers1

5

Static code analysis tools like Sonar should find all those occurrences and warn you about them.
IDEA IntelliJ's inspection profiles also includes some rules for that (Settings > Editor > Inspections > Java > Resource management issues, or just filter by "close" inside Inspections).

I see that you're using Eclipse. You can probably install the FindBugs Eclipse plugin, which includes a couple of rules to check if resources are properly closed.

walen
  • 7,103
  • 2
  • 37
  • 58