Findbugs OBL_UNSATISFIED_OBLIGATION扩展AutoClosable基类的类的构造函数

时间:2016-09-30 15:19:14

标签: java findbugs autocloseable

我正在尝试子类化实现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. 我做得不好吗?这是一个糟糕的模式还是什么?

0 个答案:

没有答案