1

I am trying to subclass a class that implements AutoClosable. Here is a brief example:

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;
    }
}

However, FindBugs (3.0.0) complains about the constructor of AutoClosableSub, apparently because it calls super without closing it:

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}

I know I can suppress this with:

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

However, I want to know:

  1. Is this a false positive in Findbugs?
  2. Is there a way to keep Findbugs happy in this case, except the suppression?
  3. Am I doing something bad? Is this a bad pattern or something?
Pieter Müller
  • 4,573
  • 6
  • 38
  • 54

0 Answers0