Menu

#1159 OBL_UNSATISFIED_OBLIGATION false positive

2.0.3
closed-fixed
5
2015-02-19
2013-01-11
Anonymous
No

private void subscribe(HttpServletRequest req) throws Exception {
Connection connection = null;
PreparedStatement stmt = null;
try {
connection = getDataSource(req).getConnection();
stmt = connection.prepareStatement(\"...\");
stmt.setString(1, ...);
stmt.executeUpdate();
} finally {
close(stmt, connection);
}
}

@WillClose
protected void close(Object... objects) {
if (objects != null) {
for (Object object : objects) {
if (object != null) {
if (object instanceof Closeable) {
try { ((Closeable)object).close(); } catch (IOException ignore) { }
} else if (object instanceof Statement) {
try { ((Statement)object).close(); } catch (SQLException ignore) { }
} else if (object instanceof Connection) {
try { ((Connection)object).close(); } catch (SQLException ignore) { }
} else if (object instanceof ResultSet) {
try { ((ResultSet)object).close(); } catch (SQLException ignore) { }
} else if (object instanceof JSONWriter) {
try { ((JSONWriter)object).close(); } catch (Exception ignore) { }
} else {
throw new IllegalArgumentException(\"Cannot handle class:\" + object.getClass().getName());
}
}
}
}
}

I tried the @javax.annotation.WillClose annotation (as suggested in a similar bug report) but it did nothing to satisfy findbugs... is there something else I can use other than @SuppressWarning )(which could mask copy/paste errors or not passing a resource to the close mehtod?

Discussion

  • William Pugh

    William Pugh - 2013-01-14
    • status: open --> open-postponed
     
  • William Pugh

    William Pugh - 2013-01-14

    The problem is that we don't support annotations on vararg arguments, and thus you can't tell findbugs that all of the arguments to the close method will be closed.

    Putting the @WillClose annotation on the method doesn't have that semantics either.

    You could write a close method that takes one, two and three arguments, to handle the common cases, Something like:

    void close(@WillClose Object object1, @WillClose Object object2) {
    close(object1); close(object2);
    }
    void close(@WillClose Object object1, @WillClose Object object2, @WillClose Object object3) {
    close(object1); close(object2); close(object3);
    }
    void close(@WillClose Object object) {
    ...
    }

     
  • William Pugh

    William Pugh - 2013-01-14

    The other problem here is that the analysis we perform doesn't handle polymorphic close methods. It really wants to be able to determine for each @WillClose parameter what kind of resource is being closed. This will require some more thinking.

     
  • William Pugh

    William Pugh - 2013-01-14

    I decided to go ahead and fix this issue. I handled some more specific cases, and added a hack to look for methods named close annotated with @WillClose

     
  • William Pugh

    William Pugh - 2013-01-14
    • status: open-postponed --> closed-fixed
     
  • William Pugh

    William Pugh - 2013-06-17
    • Group: --> 2.0.3
     

Log in to post a comment.