Recognize custom close method #1130

This commit is contained in:
alberto committed 2013-09-21 17:53:06 +02:00
1 parent 989672f8fb
commit a24cbf034b
2 files changed
+35 -2

No files matched your search

@@ -160,6 +160,19 @@ public class CloseResourceRule extends AbstractJavaRule {
closed = true;
break;
}
if (name.contains(".")) {
String[] parts = name.split("\\.");
if (parts.length == 2) {
String methodName = parts[1];
String varName = parts[0];
if (varName.equals(variableToClose)
&& closeTargets.contains(methodName)) {
closed = true;
break;
}
}
}
}
if (closed) {
break;
@@ -153,7 +153,7 @@ New use case
]]></description>
<rule-property name="closeTargets">commit,close,close,close</rule-property>
<rule-property name="types">DAOTransaction,Connection,Statement,ResulSet</rule-property>
<expected-problems>2</expected-problems>
<expected-problems>1</expected-problems>
<code><![CDATA[
import java.sql.*;
public class Foo {
@@ -425,4 +425,24 @@ public class Test {
}
]]></code>
</test-code>
</test-data>
<test-code>
<description><![CDATA[Custom close method, should be ok]]></description>
<rule-property name="closeTargets">cleanup</rule-property>
<rule-property name="types">MyClass</rule-property>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
void bar() {
MyClass myClass = null;
try {
myClass = new MyClass();
} catch (Exception e) {
} finally {
myClass.cleanup(); // should be ok, it's closed with cleanup
}
}
}
]]></code>
</test-code>
</test-data>