EqualsAvoidNullCheck updated to follow Cyclomatic Complexity rule. #954

This commit is contained in:
Roman Ivanov 2015-06-13 21:07:30 -07:00
parent 3eca04c940
commit 57f395fa2b
1 changed files with 20 additions and 9 deletions

View File

@ -121,18 +121,11 @@ public class EqualsAvoidNullCheck extends Check {
}
final DetailAST objCalledOn = dot.getFirstChild();
//checks for calling equals on String literal and
//anon object which cannot be null
//Also, checks if calling using strange inner class
//syntax outter.inner.equals(otherObj) by looking
//for the dot operator which cannot be improved
if (objCalledOn.getType() == TokenTypes.STRING_LITERAL
|| objCalledOn.getType() == TokenTypes.LITERAL_NEW
|| objCalledOn.getType() == TokenTypes.DOT) {
if (isStringLiteral(objCalledOn)) {
return;
}
final DetailAST method = objCalledOn.getNextSibling();
final DetailAST expr = dot.getNextSibling().getFirstChild();
@ -150,6 +143,24 @@ public class EqualsAvoidNullCheck extends Check {
}
}
/**
* checks for calling equals on String literal and
* anon object which cannot be null
* Also, checks if calling using strange inner class
* syntax outter.inner.equals(otherObj) by looking
* for the dot operator which cannot be improved
* @param objCalledOn object AST
* @return if it is string literal
*/
private boolean isStringLiteral(DetailAST objCalledOn) {
if (objCalledOn.getType() == TokenTypes.STRING_LITERAL
|| objCalledOn.getType() == TokenTypes.LITERAL_NEW
|| objCalledOn.getType() == TokenTypes.DOT) {
return true;
}
return false;
}
/**
* Checks if a method contains no arguments
* starting at with the argument expression.