" never closes If I have the following code: public OutputStream test(...">

Eclipse inconsistencies: resource leak: "<unrecognized Closeable value>" never closes

If I have the following code:

public OutputStream test(boolean condition) throws FileNotFoundException {
    return condition ? null : new FileOutputStream("test.txt");
}

Eclipse puts the yellow squiggles under new FileOutputStream("test.txt")and shows me the following warning:

Resource leak: '<unassigned Closeable value>' is never closed

It is strange if I remove the triple operation:

public OutputStream test() throws FileNotFoundException {
    return new FileOutputStream("test.txt");
}

warning goes away.

Is this inconsistency (bug?) In Eclipse, or am I missing the fundamental difference between the two scenarios?

In general, it seems that Eclipse is smart enough to understand that when I return Closeablefrom a method, it’s normal whether the method should close the stream (after all, what is the return point of the closed stream?). It even does it right when I return the result indirectly:

public OutputStream test() throws FileNotFoundException {
    FileOutputStream result = new FileOutputStream("test.txt");
    return result;
}

(no warnings here)

, Eclipse ? , ?


:

FileOutputStream ByteArrayOutputStream, :

public OutputStream test(boolean condition) {
    return condition ? null : new ByteArrayOutputStream();
}

-? OutputStream (Closeable, Flushable, AutoCloseable). - , ByteArrayOutputStream.close() - -op? , Eclipse , ?

+4

Source: https://habr.com/ru/post/1539182/


All Articles