【发布时间】:2014-06-09 03:20:48
【问题描述】:
我在 Eclipse 中为AutoCloseables 收到的这些“资源泄漏”警告似乎是救命稻草。
但是,如何让它们为工厂创建的实例工作?
例如(a 有效,但 b 无效):
public static void main(String[] args) {
// a) This emits a warning
new AutoCloseable() {
@Override
public void close() throws Exception {}
};
// b) But this doesn't!
newResource();
}
public static AutoCloseable newResource() {
return new AutoCloseable() {
@Override
public void close() throws Exception {}
};
}
有没有我可以在newResource() 上粘贴的注释,或者我可以做些什么来让编译器(或者是 Eclipse?)知道所有权的变化?
【问题讨论】:
-
如果忽略某些返回值,Intellij 通常会发出警告。这可能会有所帮助,因为您不必在本地分配值。
标签: java eclipse autocloseable