Resource Allocation Inspection. Is it right?

Hello,

I always used this pattern for resource allocation/deallocation:

InputStream is = new FileInputStream("abc");
try {
// do something with statement
} finally {
is.close();
}

However Selena (and Demetra I believe) insists on resource allocation being
inside of try/finally block.
What pattern does it expect me to use?

I hope it is not expecting me to use this:

InputStream is = null;
try {
is = new FileInputStream("abc");
} finally {
if (is != null) {
is.close();
}
}


What do you use for similar cases?

I just checked my first snippet into Selena it does not complain any more
about "is = new FileInputStream()" not being in try/finally. May be it was
in Demetra only. But following does complain:

try {
PreparedStatement stmt = conn.prepareStatement(sql);
try {
//........
} finally {
stmt.close();
}

same with

InitialContext ctx = new InitialContext();
try {
oldDb = (DataSource)ctx.lookup("java:comp/env/jdbc/gctrackOld");
} finally {
ctx.close();
}


1 comment
Comment actions Permalink

For Inputstreams both Selena and Demetra should already work as you want, for other resources some work needs to be done. I will make sure that these inspections receive some further polishing soon.

Bas

0

Please sign in to leave a comment.