#992 Class java.beans.Statement triggered in CloseResource rule

PMD-5.0.3
closed
pmd (542)
PMD
3-Major
Bug
2013-04-05
2012-01-30
No

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.

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);
           }

Discussion

  • Romain PELISSE
    Romain PELISSE
    2012-01-30

    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="TestUtil" 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>
    ..

     
  • Romain PELISSE
    Romain PELISSE
    2012-02-24

    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.

     
  • Andreas Dangel
    Andreas Dangel
    2013-03-16

    • status: open --> in-progress
    • assigned_to: Romain PELISSE --> Andreas Dangel
    • module: --> PMD
    • milestone: --> PMD-5.0.3
    • priority: 5 --> 3-Major
    • type: --> Bug
    • affects_version: -->
     
  • Andreas Dangel
    Andreas Dangel
    2013-03-16

    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -3,6 +3,8 @@
     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 });
    @@ -12,3 +14,4 @@
                } catch (Exception e) {
                  throw new RuntimeException("Could not set property prop: " + prop + "of type:" + pd.getPropertyType(), e);
                }      
    +~~~~~
    
     
  • Andreas Dangel
    Andreas Dangel
    2013-03-17

    • status: in-progress --> closed