Eclipse不一致:资源泄漏:'< unssigned Closeable value>'永远不会关闭 [英] Eclipse inconsistencies: Resource leak: '<unassigned Closeable value>' is never closed
问题描述
如果我有以下代码:
public OutputStream test(boolean condition) throws FileNotFoundException {
return condition ? null : new FileOutputStream("test.txt");
}
Eclipse将黄色字形置于 new FileOutputStream( test .txt)
并向我显示以下警告:
Eclipse puts yellow squiggles under new FileOutputStream("test.txt")
and shows me the following warning:
资源泄漏:< unssigned Closeable value>永远不会关闭
奇怪的是,如果我删除三元运算,则:
The strange thing is, if I remove the ternary operation:
public OutputStream test() throws FileNotFoundException {
return new FileOutputStream("test.txt");
}
警告消失了。
这是Eclipse中的不一致(错误吗?)还是缺少两种情况之间的根本区别?
Is this an inconsistency (bug?) in Eclipse or am I missing some fundamental difference between the two scenarios?
通常,Eclipse似乎很聪明足以理解当我返回 Closeable
,不让该方法关闭流是可以的(毕竟,返回关闭的流有什么意义?)。当我间接返回结果时,它甚至可以正确执行此操作:
In general, it seems like Eclipse is smart enough to understand that when I return a Closeable
from a method, it is ok to not have the method close the stream (after-all, what's the point of returning a closed stream?). It even does this correctly when I return the result indirectly:
public OutputStream test() throws FileNotFoundException {
FileOutputStream result = new FileOutputStream("test.txt");
return result;
}
(此处无警告)
那么,Eclipse是否只是被三元操作所迷惑?如果是这样,我应该将其报告为错误吗?
So, is Eclipse just getting confused by the ternary operation? If so, should I report this as a bug?
另一件奇怪的事情:
如果我替换 FileOutputStream
和 ByteArrayOutputStream
,警告也消失了:
If I replace FileOutputStream
with ByteArrayOutputStream
, the warning goes away also:
public OutputStream test(boolean condition) {
return condition ? null : new ByteArrayOutputStream();
}
如何区别对待这两个流?两者都是 OutputStream
并实现完全相同的接口( 可关闭
, Flushable
, AutoCloseable
)。是否以某种方式知道 ByteArrayOutputStream.close()
是没有操作的吗?如果是这样,是将它硬编码到Eclipse中,还是实际上是解析源代码或字节码来解决这个问题?
How come it's treating these two streams differently? Both are direct descendents of OutputStream
and implement the exact same interfaces (Closeable
, Flushable
, AutoCloseable
). Does it somehow know that ByteArrayOutputStream.close()
is a no-op? If so, is that hard-coded into Eclipse or does it actually parse the source or byte-code to figure this out?
推荐答案
这显然是一个错误。错误报告 https://bugs.eclipse.org/bugs/show_bug.cgi ?id = 434065 已被确认,但没有得到解决。
It is clearly a bug. The bug report https://bugs.eclipse.org/bugs/show_bug.cgi?id=434065 has been acknowledged, but not fixed.
该错误截至2019年7月仍未解决。
The bug is still open as of July 2019.
这篇关于Eclipse不一致:资源泄漏:'< unssigned Closeable value>'永远不会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!