
resources.report.rules.pmd.CheckResultSet.html Maven / Gradle / Ivy
CheckResultSet
CheckResultSet
Always check the return of one of the navigation method (next,previous,first,last) of a ResultSet. Indeed,
if the value return is 'false', the developer should deal with it !
This rule is defined by the following XPath expression:
//Type/ReferenceType/ClassOrInterfaceType[
(@Image = 'ResultSet')
and
(../../../descendant::Name[ends-with(@Image,'executeQuery')])
and
(
(not (contains(
(./ancestor::Block/descendant::WhileStatement/descendant::Name/attribute::Image),
concat(../../../VariableDeclarator/VariableDeclaratorId/attribute::Image,'.next')
) ) )
and ( not ( contains(
(./ancestor::Block/descendant::IfStatement/descendant::Name/attribute::Image),
concat(../../../VariableDeclarator/VariableDeclaratorId/attribute::Image,'.next')
) ) )
and (not (contains(
(./ancestor::Block/descendant::WhileStatement/descendant::Name/attribute::Image),
concat(../../../VariableDeclarator/VariableDeclaratorId/attribute::Image,'.previous')
) ) )
and ( not ( contains(
(./ancestor::Block/descendant::IfStatement/descendant::Name/attribute::Image),
concat(../../../VariableDeclarator/VariableDeclaratorId/attribute::Image,'.previous')
) ) )
and ( not ( contains(
(./ancestor::Block/descendant::IfStatement/descendant::Name/attribute::Image),
concat(../../../VariableDeclarator/VariableDeclaratorId/attribute::Image,'.last')
) ) )
and ( not ( contains(
(./ancestor::Block/descendant::IfStatement/descendant::Name/attribute::Image),
concat(../../../VariableDeclarator/VariableDeclaratorId/attribute::Image,'.first')
) ) )
)
]
Example:
[
// This is NOT appropriate !
Statement stat = conn.createStatement();
ResultSet rst = stat.executeQuery("SELECT name FROM person");
rst.next(); // what if it returns a 'false' ?
String firstName = rst.getString(1);
// This is appropriate...
Statement stat = conn.createStatement();
ResultSet rst = stat.executeQuery("SELECT name FROM person");
if (rst.next())
{
String firstName = rst.getString(1);
}
else
{
// here you deal with the error ( at least log it)
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy