Menu

#1263 PMD reports CheckResultSet violation in completely unrelated source files.

PMD-5.2.1
closed
PMD
3-Major
Bug
5.1.x
2014-11-03
2014-09-17
No

If the below two source files are analyzed by PMD (5.1.0 - 5.1.3). In the bug report for the 'UnrelatedClass.java' file, there is a violation
<violation beginline="55" endline="55" begincolumn="9" endcolumn="32" rule="CheckResultSet" ruleset="Basic" class="MysqlExample" method="select" variable="results" externalInfoUrl="http://pmd.sourceforge.net/pmd-5.1.3/rules/java/basic.html#CheckResultSet" priority="3">

In the above violation, the line numbers, class name, method etc, correspond to MysqlExample.java file. Though there is no dependency among the two files, only similarity they have is that they both have a local variable named 'results' that has call to its 'next' method.

This violation is reported only if PMD first analysis MysqlExample.java, and then UnrelatedClass.java. If you are trying to reproduce this you may have to run it multiple times.

Command that I ran is:
~/pmd-src-5.1.3/bin/run.sh pmd -R rulesets/java/basic.xml -format xml -dir MysqlExample.java,UnrelatedClass.java

I have Attached an archive of these files.

//UnrelatedClass.java
class UnrelatedClass {

boolean next() {
    return false;
}

public static void main(String[] argv) {
    UnrelatedClass results = new UnrelatedClass();
    results.next();
}

}

//MysqlExample.java
public class MysqlExample {

private final DataSource dataSource;
private final String table;

private String select(String key) throws Exception {
    Connection conn = dataSource.getConnection();
    String upsert = "select val_ from " + table + " where key_ = ?";
    PreparedStatement stmt = conn.prepareStatement(upsert);
    ResultSet results = null;
    try {
        stmt.setString(1, key);
        results = stmt.executeQuery();
        if(results.next())
            return results.getString(1);
        else
            return null;
    } finally {
        try {
            stmt.close();
        } catch(Exception e) {}
        try {
            conn.close();
        } catch(Exception e) {}
    }
}

public MysqlExample(String table, String username, String password) {
    this.table = table;
    BasicDataSource ds = new BasicDataSource();
    ds.setDriverClassName("com.mysql.jdbc.Driver");
    ds.setUsername(username);
    ds.setPassword(password);
    ds.setUrl(connectionString);
    this.dataSource = ds;
}

}
```

1 Attachments

Discussion

  • Vamshi Basupalli

    In the source code of PMD 5.1.3 CheckResultSetRule. The member variable 'resultSetVariables' is not purged every time a new method declaration node in the AST is visited. This results in local variable names and AST node information that have 'java.sql.ResultSet' declarations being stored in 'resultSetVariables' across multiple source files.

     

    Last edit: Vamshi Basupalli 2014-09-17
  • Andreas Dangel

    Andreas Dangel - 2014-10-18
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-next
     
  • Andreas Dangel

    Andreas Dangel - 2014-11-03
    • status: open --> closed
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.