javaeclipsecompiler-warningssuppress-warningsbonecp

How to make a custom compiler warning in Eclipse?


I am experimenting with BoneCP as a connection pooling alternative to DBCP. I added the bonecp-0.8.0rc-1.jar to my runtime classpath, and copy-n-pasted the code from their example page into my test driver:

Connection connection = null;
BoneCP connectionPool;
try {
    Class.forName(config.getDatabaseLogger().getJDBCDriver());

    BoneCPConfig boneConfig = configureBoneCP(config);

    connectionPool = new BoneCP(boneConfig);

    connection = connectionPool.getConnection();    // fetch a connection
} catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

On the line that reads:

connectionPool = new BoneCP(boneConfig);

Eclipse (Juno) is giving me a compiler warning:

Resource leak: 'connectionPool' is never closed

It then gives me an option to "Add @SuppressWarnings 'resource' to newConnectionManager" (newConnectionManager() references the class this is contained in).

How is this possible? How does Eclipse know anything about a BoneCP object, and how does it know that it must be closed? Is this some kind of "custom warning" that ships with the BoneCP library? If so, how could I add/annotate code to my own projects to help warn downstream developers that they aren't using my libraries correctly?


Solution

  • The warning is caused by this statement:

    connection = connectionPool.getConnection();
    

    The method getConnection() returns a object of type java.sql.Connection which implements java.lang.AutoCloseable since Java 7. Eclipse generates this warning when it can not determine whether a resource which implements either java.io.Closeable or java.lang.AutoCloseable gets closed by all possible code paths.