Priority
Normal
Type
Bug 
State
Fixed 
Assignee
Bas Leijdekkers 
Subsystem
Code Analysis. Inspection 
Affected versions
Fixed in
Fixed in build
Next build 
Build
7590
Fixed in build
8238
Severity
0
  • Submitted by   Björn Kautler
    2 years ago (18 Jan 2008 14:39)
  • Updated by   root
    3 weeks ago (17 Jan 2010 19:57)
  • Jira: IDEA-42530
    (history, comments)

IDEA-42530

Faulty "'Connection' should be opened in a try block, and closed in a finally block" inspection in InspectionGadgets plugin

0
com.siyeh.ig.j2me.ConnectionResourceInspection.CloseVisitor uses "closeRecordStore" instead of "close", this way the inspection always reports false positives . Additionally even if "close" is checked, the following code:

javax.microedition.io.Connection connection = null;
try {
    connection = (javax.microedition.io.file.FileConnection) Connector.open("");
} catch (IOException ioe) {
    // ignore
} finally {
    try {
        if (null != connection) {
            connection.close();
        }
    } catch (IOException ioe) {
        // ignore
    }
}


will also report wrongly, because of the type cast. I know that this cast is unneccessary here, but it is just for demonstration. The real code is more like:

javax.microedition.io.file.FileConnection fileConnection = null;
try {
    fileConnection = (javax.microedition.io.file.FileConnection) Connector.open("");
    if (!fileConnection.exists()) {
        fileConnection.mkdir();
    }
} catch (IOException ioe) {
    // ignore
} finally {
    try {
        if (null != fileConnection) {
            fileConnection.close();
        }
    } catch (IOException ioe) {
        // ignore
    }
}



Environment: Windows XP SP2

Issue was resolved
Comments (1)
 
History (1)
 
Links
 
Björn Kautler
  Björn Kautler
18 Jan 2008 14:45
(2 years ago)
#permalink
In case others have that problem and want to workaround this:

Connection connection = null;
try {
    connection = Connector.open("");
    final FileConnection fileConnection = (FileConnection) connection;
    if (!fileConnection.exists()) {
        fileConnection.mkdir();
    }
} catch (IOException ioe) {
    // ignore
} finally {
    try {
        if (null != connection) {
            connection.close();
        }
    } catch (IOException ioe) {
        // ignore
    }
}