1

我正在尝试子类化一个实现AutoClosable. 这是一个简短的例子:

public class AutoClosableBase implements AutoCloseable {

    private final int a;

    public AutoClosableBase(int a) {
        this.a = a;
    }

    @Override
    public void close() throws Exception {
        // nothing to do
    }
}

public class AutoClosableSub extends AutoClosableBase {

    private final int b;

    public AutoClosableSub(int a, int b) {
        super(a);
        this.b = b;
    }
}

但是,FindBugs (3.0.0) 抱怨 AutoClosableSub 的构造函数,显然是因为它调用 super 而不关闭它:

new Sample$AutoClosableSub(Sample, int, int) may fail to clean up Sample$AutoClosableBase
    Bug type OBL_UNSATISFIED_OBLIGATION (click for details)
    In class Sample$AutoClosableSub
    In method new Sample$AutoClosableSub(Sample, int, int)
    Reference type Sample$AutoClosableBase
    1 instances of obligation remaining
    Obligation to clean up resource created at Sample.java:[line 27] is not discharged
    Path continues at Sample.java:[line 28]
    Path continues at Sample.java:[line 29]
    Remaining obligations: {Sample$AutoClosableBase x 1}

我知道我可以通过以下方式抑制这一点:

@edu.umd.cs.findbugs.annotations.SuppressFBWarnings("OBL_UNSATISFIED_OBLIGATION")

但是,我想知道:

  1. 这是 Findbugs 中的误报吗?
  2. 在这种情况下,除了压制,有没有办法让 Findbugs 开心?
  3. 我在做坏事吗?这是一个糟糕的模式还是什么?
4

0 回答 0