Menu

#1131 CloseResource should complain if code betwen declaration of resource and try

PMD-5.1.0
closed
None
PMD
3-Major
Bug
2013-10-13
2013-09-12
No

If some code between the creation of the resource and try statement fails, the resource doesn't get closed.

In this testcase the exception is explicit:

<test-code>
<description><![CDATA[Code betwen declaration and try, should fail]]></description>
<rule-property name="types">Connection</rule-property>
<expected-problems>1</expected-problems>
<code><![CDATA[         
public class Foo {
    void bar() {
        String test = "";
        Connection c = pool.getConnection();
        if (test != null) {
            throw new RuntimeException("haha"); // <- RuntimeException, Connection  c is not closed
        }
        try {
        } catch (Exception e) {
        } finally {
           c.close();
        }
    }
}
]]></code>
</test-code>

Discussion

  • Alberto Fernández

    The previous code should be written:

    Option 1:

        <test-code>
        <description><![CDATA[Creation inside try, ok]]></description>
        <rule-property name="types">Connection</rule-property>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
            public class Foo {
             void bar() {
              String test = "";
              Connection c = null
              if (test != null) {
                 throw new RuntimeException("haha");
              }
              try {
                  // Creation inside try, ok
                   c = pool.getConnection();
              } catch (Exception e) {
              } finally {
                  if (c!= null) {
                    c.close();
                  }
              }
             }
            }
        ]]></code>
    </test-code>
    

    Option 2

            <test-code>
        <description><![CDATA[No sentences between creation and try, ok]]></description>
        <rule-property name="types">Connection</rule-property>
        <expected-problems>0</expected-problems>
        <code><![CDATA[
            public class Foo {
             void bar() {
              String test = "";
              if (test != null) {
                 throw new RuntimeException("haha");
              } 
              Connection c = pool.getConnection();
              // No sentences between creation and try, ok
              try {
              } catch (Exception e) {
              } finally {
                  if (c!= null) {
                    c.close();
                  }
              }
             }
            }
        ]]></code>
    </test-code>
    
     
  • Andreas Dangel

    Andreas Dangel - 2013-10-13
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -1,4 +1,3 @@
    -
     If some code between the creation of the resource and try statement fails, the resource doesn't get closed.
    
     In this testcase the exception is explicit:
    
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-5.1.0
    • Type: Feature-Request --> Bug
     

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.