I am using Eclipse 4.2 with resource leak warnings enabled.
This code creates, in my opinion, a false warning about a resource leak.
public static void test(){ InputStream in = null; try { in = new FileInputStream("A"); } catch (IOException e) { return; }finally{ close(in); } } public static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } } }
If I refactor the code and pull the closure method into a finally block, everything will be fine.
public static void test2(){ InputStream in = null; try { in = new FileInputStream("A"); } catch (IOException e) { return; }finally{ if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace() } } } }
Is it possible to somehow remove these warnings without duplicating the close method code and disabling resource leak warnings?
I found an error report here for something similar in loops, but there are no loops in my code.
source share