如何在 Eclipse 中防止流畅的资源泄漏警告?

How to prevent fluent resource leak warnings in Eclipse?

提问人:boot-and-bonnet 提问时间:4/14/2020 更新时间:4/14/2020 访问量:208

问:

有没有办法避免对类型上的流畅样式方法链接出现“潜在资源泄漏”警告?下面的示例在每次方法调用时都有一个警告。AutoCloseable

我宁愿不使用 ,因为无论在使用该类的任何地方都需要它。而且我不想关闭警告,因为我过去发现过许多真正的错误。@SuppressWarnings

随着最新的2020-03日食,误报的数量似乎大大增加,以至于警告几乎无法使用。

public class NoLeak implements AutoCloseable {

    public NoLeak a() {
        return this;
    }

    public NoLeak b() {
        return this;
    }

    @Override
    public void close() {}

    public static void main(String[] args) {
        try (NoLeak noLeak = new NoLeak()) {
            noLeak
                .a()  // warning here,
                .b()  // here,
                .a(); // and here
        }
    }
}

Eclipse 2020-03 (4.15.0),Java 12

Java Eclipse 警告 资源泄漏

评论

0赞 boot-and-bonnet 4/15/2020
对于返回已管理的可关闭资源的任何方法来说,也是一个问题,例如:java.nio.file.Path.getFileSystem()
0赞 boot-and-bonnet 4/16/2020
也许对返回已托管资源的每个方法进行注释,以指示调用方没有责任关闭它?

答: 暂无答案