@Override public int[] getDefaultTokens() { return getRequiredTokens(); }
/** * 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; }
/** * Check whether the method call should be violated. * @param methodCall method call to check. */ private void checkMethodCall(DetailAST methodCall) { DetailAST objCalledOn = methodCall.getFirstChild().getFirstChild(); if (objCalledOn.getType() == TokenTypes.DOT) { objCalledOn = objCalledOn.getLastChild(); } final DetailAST expr = methodCall.findFirstToken(TokenTypes.ELIST).getFirstChild(); if (containsOneArgument(methodCall) && containsAllSafeTokens(expr) && isCalledOnStringFieldOrVariable(objCalledOn)) { final String methodName = methodCall.getFirstChild().getLastChild().getText(); if (EQUALS.equals(methodName)) { log(methodCall, MSG_EQUALS_AVOID_NULL); } else { log(methodCall, MSG_EQUALS_IGNORE_CASE_AVOID_NULL); } } }
/** * Whether the field or the variable from THIS instance is of String type. * @param objCalledOn the field or the variable from THIS instance to check. * @return true if the field or the variable from THIS instance is of String type. */ private boolean isStringFieldOrVariableFromThisInstance(DetailAST objCalledOn) { boolean result = false; final String name = objCalledOn.getText(); final DetailAST field = getObjectFrame(currentFrame).findField(name); if (field != null) { result = STRING.equals(getFieldType(field)); } return result; }
@Test public void testTokensNotNull() { final EqualsAvoidNullCheck check = new EqualsAvoidNullCheck(); Assert.assertNotNull("Acceptable tokens should not be null", check.getAcceptableTokens()); Assert.assertNotNull("Default tokens should not be null", check.getDefaultTokens()); Assert.assertNotNull("Required tokens should not be null", check.getRequiredTokens()); }
/** * 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; }
/** * Check whether the method call should be violated. * @param methodCall method call to check. */ private void checkMethodCall(DetailAST methodCall) { DetailAST objCalledOn = methodCall.getFirstChild().getFirstChild(); if (objCalledOn.getType() == TokenTypes.DOT) { objCalledOn = objCalledOn.getLastChild(); } final DetailAST expr = methodCall.findFirstToken(TokenTypes.ELIST).getFirstChild(); if (containsOneArgument(methodCall) && containsAllSafeTokens(expr) && isCalledOnStringFieldOrVariable(objCalledOn)) { final String methodName = methodCall.getFirstChild().getLastChild().getText(); if (EQUALS.equals(methodName)) { log(methodCall, MSG_EQUALS_AVOID_NULL); } else { log(methodCall, MSG_EQUALS_IGNORE_CASE_AVOID_NULL); } } }
/** * Whether the field or the variable from THIS instance is of String type. * @param objCalledOn the field or the variable from THIS instance to check. * @return true if the field or the variable from THIS instance is of String type. */ private boolean isStringFieldOrVariableFromThisInstance(DetailAST objCalledOn) { boolean result = false; final String name = objCalledOn.getText(); final DetailAST field = getObjectFrame(currentFrame).findField(name); if (field != null) { result = STRING.equals(getFieldType(field)); } return result; }
@Override public int[] getAcceptableTokens() { return getRequiredTokens(); }
/** * 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; }
/** * 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; }
@Override public int[] getAcceptableTokens() { return getRequiredTokens(); }
@Override public int[] getDefaultTokens() { return getRequiredTokens(); }