5

Eclipse 支持 try-with-resource,有点像这样:

try(Outputstream resource = new FileOutputStream(file)){
// do something...
}

这个功能添加到 Eclipse 已经有好几年了,但是没有模板“try-with-reousource”。只有一个是“try-catch”。

我试图制作模板,例如try($type{} ${localVar} = new $type{}){ {$cursor{} },但没用。(还建议使用非 AutoClosable 类型)

是否有任何有用的 try-with-resource 模板?

4

2 回答 2

1

Eclipse 中不包含“try-with-resource”模板。

有一个未解决的问题:Bug 351864 - [1.7][templates] Add 'try-with-resource' 模板

于 2015-10-04T20:07:34.747 回答
0

Eclipse 4.22(2021 年第四季度,7 年后)可能会提供该功能:

使用 try-with-resources quickfix 包围

当有问题的资源实现 AutoCloseable 时,创建了一个新的快速修复程序来为 Surround 提供try-with-resources资源泄漏和潜在的资源泄漏。

快速修复示例:

未关闭的资源——https://www.eclipse.org/eclipse/news/4.22/images/unclosed-closeable.png

潜在未关闭的资源

于 2021-12-11T16:12:09.063 回答