pmd: fix #1135 CheckResultSet ignores results set declared outside of try/catch
This commit is contained in:
1 parent
64ca9b6f01
commit
32bf09ffd7
3 files changed
+70
-2
No files matched your search
@@ -8,6 +8,7 @@ Fixed bug 1121: NullPointerException when invoking XPathCLI
|
||||
Fixed bug 1130: CloseResource doesn't recognize custom close method
|
||||
Fixed bug 1131: CloseResource should complain if code betwen declaration of resource and try
|
||||
Fixed bug 1134: UseStringBufferLength: false positives
|
||||
Fixed bug 1135: CheckResultSet ignores results set declared outside of try/catch
|
||||
Fixed bug 1136: ECMAScript: NullPointerException in getLeft() and getRight()
|
||||
Fixed bug 1141: ECMAScript: getFinallyBlock() is buggy.
|
||||
Fixed bug 1142: ECMAScript: getCatchClause() is buggy.
|
||||
|
||||
@@ -45,8 +45,8 @@ public class CheckResultSetRule extends AbstractJavaRule {
|
||||
ASTVariableDeclarator declarator = node.getFirstChildOfType(ASTVariableDeclarator.class);
|
||||
if (declarator != null) {
|
||||
ASTName name = declarator.getFirstDescendantOfType(ASTName.class);
|
||||
if (name != null && name.getImage().endsWith("executeQuery")) {
|
||||
|
||||
if (type.getType() != null
|
||||
|| (type.getType() == null && name != null && name.getImage().endsWith("executeQuery"))) {
|
||||
ASTVariableDeclaratorId id = declarator.getFirstChildOfType(ASTVariableDeclaratorId.class);
|
||||
resultSetVariables.put(id.getImage(), node);
|
||||
}
|
||||
|
||||
+67
@@ -158,6 +158,73 @@ public class Test {
|
||||
|
||||
return _count;
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>#1135 CheckResultSet ignores results set declared outside of try/catch (good case)</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
try {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
ps = getCurrentSession().connection().prepareStatement(query);
|
||||
ps.setInt(1, fiscalYear);
|
||||
rs = ps.executeQuery();
|
||||
if (rs.next()) {
|
||||
result = rs.getInt("value");
|
||||
}
|
||||
} catch (SQLException se) {
|
||||
throw new DataAccessException(se);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>#1135 CheckResultSet ignores results set declared outside of try/catch</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<code><![CDATA[
|
||||
import java.sql.ResultSet;
|
||||
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
PreparedStatement ps = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
ps = getCurrentSession().connection().prepareStatement(query);
|
||||
ps.setInt(1, fiscalYear);
|
||||
rs = ps.executeQuery();
|
||||
rs.next(); //this should be a PMD warning, but it is not
|
||||
result = rs.getInt("value");
|
||||
} catch (SQLException se) {
|
||||
throw new DataAccessException(se);
|
||||
} finally {
|
||||
//call method to close the ResultSet and PreparedStatment
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>#1135 CheckResultSet ignores results set declared outside of try/catch - prevent false positive</description>
|
||||
<expected-problems>0</expected-problems>
|
||||
<code><![CDATA[
|
||||
import com.special.ResultSet;
|
||||
|
||||
public class Foo {
|
||||
public void bar() {
|
||||
ResultSet rs = doExecuteQuery();
|
||||
rs.next(); // no warning here because it's not a java.sql.ResultSet
|
||||
result = rs.getInt("baz");
|
||||
}
|
||||
}
|
||||
]]></code>
|
||||
</test-code>
|
||||
|
||||
Reference in new issue
Block a user