Class java.beans.Statement triggered in CloseResource rule
A source code analyzer
Brought to you by:
adangel,
juansotuyo
Using PMD 4.2.5
Having the following code below and including the ruleset/design.xml
The CloseResource rule incorrectly reports the Statement variable as a resource that should be closed.
:::java
import java.beans.Statement;
...
Statement stmt = new Statement(vo, "set" + prop, new Object[] { vector });
try {
stmt.execute();
} catch (Exception e) {
throw new RuntimeException("Could not set property prop: " + prop + "of type:" + pd.getPropertyType(), e);
}
Hi,
Thanks for the report. Would you mind checking if this is still happening with PMD 4.3 ? I think some tweaking on CloseResource was done.
Same result using PMD 4.3.
<pmd version="4.3" timestamp="2012-02-02T14:43:55.013">
...
<violation beginline="66" endline="66" begincolumn="27" endcolumn="30" rule="CloseResource" ruleset="Design Rules" package="common.utils" class="" method="setBoolean" variable="stmt" externalinfourl="http://pmd.sourceforge.net/rules/design.html#CloseResource" priority="3">Ensure that resources like this Statement object are closed after use</violation>
..</pmd>
A quick and good fix, will probably be to use type resolution to assert that Statement is coming form an SQL related package. I'll try to look at that, but if you want, you can probably do this by yourself. Look into existing rulesets how type resolution is used, and modify PMD rule accordingly.
Diff: