我在 Eclipse for AutoCloseable
s 中收到的这些“资源泄漏”警告似乎是救命稻草。
但是,如何让它们为工厂创建的实例工作?
例如(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?)知道所有权的变化?