use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class EqualsAvoidNullCheck method isObjectValid.
/**
* Check whether the object equals method is called on is not a String literal
* and not too complex.
* @param objCalledOn the object equals method is called on ast.
* @return true if the object is valid.
*/
private static boolean isObjectValid(DetailAST objCalledOn) {
boolean result = true;
final DetailAST previousSibling = objCalledOn.getPreviousSibling();
if (previousSibling != null && previousSibling.getType() == TokenTypes.DOT) {
result = false;
}
if (isStringLiteral(objCalledOn)) {
result = false;
}
return result;
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class EqualsAvoidNullCheck method isStringFieldOrVariableFromClass.
/**
* Whether the field or the variable from the specified class is of String type.
* @param objCalledOn the field or the variable from the specified class to check.
* @param className the name of the class to check in.
* @return true if the field or the variable from the specified class is of String type.
*/
private boolean isStringFieldOrVariableFromClass(DetailAST objCalledOn, final String className) {
boolean result = false;
final String name = objCalledOn.getText();
FieldFrame frame = getObjectFrame(currentFrame);
while (frame != null) {
if (className.equals(frame.getFrameName())) {
final DetailAST field = frame.findField(name);
if (field != null) {
result = STRING.equals(getFieldType(field));
}
break;
}
frame = getObjectFrame(frame.getParent());
}
return result;
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class EqualsAvoidNullCheck method isStringFieldOrVariable.
/**
* Whether the field or the variable is of String type.
* @param objCalledOn the field or the variable to check.
* @return true if the field or the variable is of String type.
*/
private boolean isStringFieldOrVariable(DetailAST objCalledOn) {
boolean result = false;
final String name = objCalledOn.getText();
FieldFrame frame = currentFrame;
while (frame != null) {
final DetailAST field = frame.findField(name);
if (field != null && (frame.isClassOrEnumOrEnumConstDef() || checkLineNo(field, objCalledOn))) {
result = STRING.equals(getFieldType(field));
break;
}
frame = frame.getParent();
}
return result;
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class EqualsHashCodeCheck method finishTree.
@Override
public void finishTree(DetailAST rootAST) {
objBlockWithEquals.entrySet().stream().filter(detailASTDetailASTEntry -> {
return objBlockWithHashCode.remove(detailASTDetailASTEntry.getKey()) == null;
}).forEach(detailASTDetailASTEntry -> {
final DetailAST equalsAST = detailASTDetailASTEntry.getValue();
log(equalsAST.getLineNo(), equalsAST.getColumnNo(), MSG_KEY_HASHCODE);
});
objBlockWithHashCode.entrySet().forEach(detailASTDetailASTEntry -> {
final DetailAST equalsAST = detailASTDetailASTEntry.getValue();
log(equalsAST.getLineNo(), equalsAST.getColumnNo(), MSG_KEY_EQUALS);
});
objBlockWithEquals.clear();
objBlockWithHashCode.clear();
}
use of com.puppycrawl.tools.checkstyle.api.DetailAST in project checkstyle by checkstyle.
the class EqualsHashCodeCheck method isEqualsMethod.
/**
* Determines if an AST is a valid Equals method implementation.
*
* @param ast the AST to check
* @return true if the {code ast} is a Equals method.
*/
private static boolean isEqualsMethod(DetailAST ast) {
final DetailAST modifiers = ast.getFirstChild();
final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS);
return CheckUtils.isEqualsMethod(ast) && modifiers.branchContains(TokenTypes.LITERAL_PUBLIC) && isObjectParam(parameters.getFirstChild()) && (ast.branchContains(TokenTypes.SLIST) || modifiers.branchContains(TokenTypes.LITERAL_NATIVE));
}
Aggregations