@Override protected AbstractFrame getIfContains(DetailAST nameToFind, boolean lookForMethod) { AbstractFrame frame = null; if (lookForMethod && containsMethod(nameToFind) || containsFieldOrVariable(nameToFind)) { frame = this; } else if (getParent() != null) { frame = getParent().getIfContains(nameToFind, lookForMethod); } return frame; }
/** * Check for instance field. * @param ast token * @param name identifier of token * @return true if instance field */ private boolean isInstanceField(DetailAST ast, String name) { return !isInStatic(ast) && frame.containsInstanceField(name); }
/** * Checks return type of a given method. * @param methodDef method for check. */ private void visitMethodDef(DetailAST methodDef) { if (isVerifiable(methodDef) && isCheckedMethod(methodDef)) { checkClassName(methodDef); } }
@Override public void visitToken(DetailAST ast) { if (!isInContext(ast, ALLOWED_ASSIGNMENT_CONTEXT) && !isInNoBraceControlStatement(ast) && !isInWhileIdiom(ast)) { log(ast, MSG_KEY); } }
/** * Checks whether method or constructor parameter is ignored. * @param ast the parameter token. * @param name the parameter name. * @return true if parameter is ignored. */ private boolean isIgnoredParam(DetailAST ast, String name) { return isIgnoredSetterParam(ast, name) || isIgnoredConstructorParam(ast) || isIgnoredParamOfAbstractMethod(ast); }
/** * Checks whether a value can be assigned to a field. * A value can be assigned to a final field only in constructor block. If there is a method * block, value assignment can be performed only to non final field. * @param ast an identifier token. * @return true if a value can be assigned to a field. */ private boolean canAssignValueToClassField(DetailAST ast) { final AbstractFrame fieldUsageFrame = findFrame(ast, false); final boolean fieldUsageInConstructor = isInsideConstructorFrame(fieldUsageFrame); final AbstractFrame declarationFrame = findFrame(ast, true); final boolean finalField = ((ClassFrame) declarationFrame).hasFinalField(ast); return fieldUsageInConstructor || !finalField; }
@Override public void visitToken(DetailAST ast) { log( ast, MSG_KEY, convertToString(ast) ); }
/** * If token is LITERAL_IF and there is an {@code else} following or token is CASE_GROUP and * there is another {@code case} following, then update the uninitialized variables. * @param ast token to be checked * @return true if should be updated, else false */ private static boolean shouldUpdateUninitializedVariables(DetailAST ast) { return isIfTokenWithAnElseFollowing(ast) || isCaseTokenWithAnotherCaseFollowing(ast); }
/** Creates new instance of the check. */ public IllegalTypeCheck() { setIllegalClassNames(DEFAULT_ILLEGAL_TYPES); setIgnoredMethodNames(DEFAULT_IGNORED_METHOD_NAMES); }
/** * Checks type of given variable. * @param variableDef variable to check. */ private void visitVariableDef(DetailAST variableDef) { if (isVerifiable(variableDef)) { checkClassName(variableDef); } }
/** * Get the nearest parent frame which is CLASS_DEF, ENUM_DEF or ENUM_CONST_DEF. * @param frame to start the search from. * @return the nearest parent frame which is CLASS_DEF, ENUM_DEF or ENUM_CONST_DEF. */ private static FieldFrame getObjectFrame(FieldFrame frame) { FieldFrame objectFrame = frame; while (objectFrame != null && !objectFrame.isClassOrEnumOrEnumConstDef()) { objectFrame = objectFrame.getParent(); } return objectFrame; }
/** * Check whether the frame contains a given method. * @param methodToFind the AST of the method to find. * @return true, if a method with the same name and number of parameters is found. */ private boolean containsMethod(DetailAST methodToFind) { return containsMethodDef(instanceMethods, methodToFind) || containsMethodDef(staticMethods, methodToFind); }
@Override public void visitToken(DetailAST literalTry) { if (depth > max) { log(literalTry, MSG_KEY, depth, max); } ++depth; }
/** * Set whether or not the match is case sensitive. * @param caseInsensitive true if the match is case insensitive. */ public void setIgnoreCase(boolean caseInsensitive) { ignoreCase = caseInsensitive; updateRegexp(); }
@Override protected boolean containsFieldOrVariable(DetailAST nameToFind) { return containsFieldOrVariableDef(instanceMembers, nameToFind) || containsFieldOrVariableDef(staticMembers, nameToFind); }
/** * Checks if this is assignments of parameter. * @param ast assignment to check. */ private void visitAssign(DetailAST ast) { checkIdent(ast); }
/** * Check whether the frame contains a field or a variable with the given name. * @param nameToFind the IDENT ast of the name we're looking for. * @return whether it was found. */ protected boolean containsFieldOrVariable(DetailAST nameToFind) { return containsFieldOrVariableDef(varIdents, nameToFind); }
@Override public void visitToken(DetailAST ast) { if (depth > max) { log(ast, MSG_KEY, depth, max); } ++depth; }
/** * Checks if a given name is a known static method of the class. * @param ident the IDENT ast of the method call to check. * @return true is the given ast is correspondent to a known * instance method of the class. */ public boolean hasStaticMethod(final DetailAST ident) { return containsMethodDef(staticMethods, ident); }
/** * Checks if a given name is a known instance method of the class. * @param ident the IDENT ast of the method call to check. * @return true if the given ast is correspondent to a known * instance method of the class. */ public boolean hasInstanceMethod(final DetailAST ident) { return containsMethodDef(instanceMethods, ident); }