/** * Checks whether there is a use of an object reference to invoke an object's method on line. * @param root root token of the line. * @return true if there is a use of an object reference to invoke an object's method on line. */ private static boolean isUsingOfObjectReferenceToInvokeMethod(DetailAST root) { return root.getFirstChild().getFirstChild().getFirstChild() != null && root.getFirstChild().getFirstChild().getFirstChild().getNextSibling() != null; }
/** * Checks whether type definition is in canonical form. * @param type type definition token. * @return true if type definition is in canonical form. */ private static boolean isCanonicalName(DetailAST type) { return type.getFirstChild().getType() == TokenTypes.DOT; }
/** * Retrieves the first modifier that is not an annotation. * @param modifiers The ast to examine. * @return The first modifier or {@code null} if none found. */ private static DetailAST getFirstModifierAst(DetailAST modifiers) { DetailAST modifier = modifiers.getFirstChild(); while (modifier != null && modifier.getType() == TokenTypes.ANNOTATION) { modifier = modifier.getNextSibling(); } return modifier; }
/** * Gets the list of annotations on method definition. * @param methodDef method definition node * @return List of annotations */ private static List<DetailAST> getMethodAnnotationsList(DetailAST methodDef) { final List<DetailAST> annotationsList = new ArrayList<>(); final DetailAST modifiers = methodDef.findFirstToken(TokenTypes.MODIFIERS); DetailAST modifier = modifiers.getFirstChild(); while (modifier != null) { if (modifier.getType() == TokenTypes.ANNOTATION) { annotationsList.add(modifier); } modifier = modifier.getNextSibling(); } return annotationsList; }
/** * Checks that return type is {@code void}. * @param method the METHOD_DEF node * @return true if check passed, false otherwise */ private static boolean checkType(DetailAST method) { final DetailAST type = method.findFirstToken(TokenTypes.TYPE).getFirstChild(); return type.getType() == TokenTypes.LITERAL_VOID; }
/** * Returns generic type arguments token. * @param type type token. * @param isCanonicalName whether type name is in canonical form. * @return generic type arguments token. */ private static DetailAST getGenericTypeArgs(DetailAST type, boolean isCanonicalName) { final DetailAST typeArgs; if (isCanonicalName) { // if type class name is in canonical form, abstract tree has specific structure typeArgs = type.getFirstChild().findFirstToken(TokenTypes.TYPE_ARGUMENTS); } else { typeArgs = type.findFirstToken(TokenTypes.TYPE_ARGUMENTS); } return typeArgs; }
/** * Walks over the type members and push the first javadoc sentence of every * {@code public} {@code static} {@code int} field to the consumer. * @param objBlock the OBJBLOCK of a class to iterate over its members * @param consumer first javadoc sentence consumer * @throws CheckstyleException if failed to parse a javadoc comment */ private static void iteratePublicStaticIntFields(DetailAST objBlock, Consumer<String> consumer) throws CheckstyleException { for (DetailAST member = objBlock.getFirstChild(); member != null; member = member.getNextSibling()) { if (isPublicStaticFinalIntField(member)) { final DetailAST modifiers = member.findFirstToken(TokenTypes.MODIFIERS); final String firstJavadocSentence = getFirstJavadocSentence(modifiers); if (firstJavadocSentence != null) { consumer.accept(getName(member) + "=" + firstJavadocSentence.trim()); } } } }
/** * Skips over an inner assign portion of an argument expression. * @param currentAST current token in the argument expression * @return the next relevant token */ private static DetailAST skipVariableAssign(final DetailAST currentAST) { DetailAST result = currentAST; if (currentAST.getType() == TokenTypes.ASSIGN && currentAST.getFirstChild().getType() == TokenTypes.IDENT) { result = currentAST.getFirstChild().getNextSibling(); } return result; }
/** * Returns the last child token that makes a specified type and contains containType in * its branch. * @param ast token to be tested * @param childType the token type to match * @param containType the token type which has to be present in the branch * @return the matching token, or null if no match */ private static DetailAST findLastChildWhichContainsSpecifiedToken(DetailAST ast, int childType, int containType) { DetailAST returnValue = null; for (DetailAST astIterator = ast.getFirstChild(); astIterator != null; astIterator = astIterator.getNextSibling()) { if (astIterator.getType() == childType && astIterator.findFirstToken(containType) != null) { returnValue = astIterator; } } return returnValue; }
/** * Returns the n'th child of an AST node. * @param ast the AST node to get the child of * @param index the index of the child to get * @return the n'th child of the given AST node, or {@code null} if none */ private static DetailAST getNthChild(DetailAST ast, int index) { DetailAST child = ast.getFirstChild(); for (int i = 0; i < index && child != null; ++i) { child = child.getNextSibling(); } return child; }
/** * Check if given parameter definition is a multiple type catch. * @param parameterDefAst parameter definition * @return true if it is a multiple type catch, false otherwise */ private static boolean isMultipleTypeCatch(DetailAST parameterDefAst) { final DetailAST typeAst = parameterDefAst.findFirstToken(TokenTypes.TYPE); return typeAst.getFirstChild().getType() == TokenTypes.BOR; }
/** * Creates {@code FullIdent} for given type node. * @param typeAST a type node. * @return {@code FullIdent} for given type. */ public static FullIdent createFullType(final DetailAST typeAST) { DetailAST ast = typeAST; // ignore array part of type while (ast.findFirstToken(TokenTypes.ARRAY_DECLARATOR) != null) { ast = ast.findFirstToken(TokenTypes.ARRAY_DECLARATOR); } return FullIdent.createFullIdent(ast.getFirstChild()); }
/** * Checks if current type is primitive type (int, short, float, boolean, double, etc.). * As primitive types have special tokens for each one, such as: * LITERAL_INT, LITERAL_BOOLEAN, etc. * So, if type's identifier differs from {@link TokenTypes#IDENT IDENT} token - it's a * primitive type. * @param type Ast {@link TokenTypes#TYPE TYPE} node. * @return true if current type is primitive type. */ private static boolean isPrimitive(DetailAST type) { return type.getFirstChild().getType() != TokenTypes.IDENT; }
/** * Check the indentation level of modifiers. */ protected void checkModifiers() { final DetailAST modifiers = mainAst.findFirstToken(TokenTypes.MODIFIERS); for (DetailAST modifier = modifiers.getFirstChild(); modifier != null; modifier = modifier.getNextSibling()) { if (isOnStartOfLine(modifier) && !getIndent().isAcceptable(expandedTabsColumnNo(modifier))) { logError(modifier, "modifier", expandedTabsColumnNo(modifier)); } } }
/** * Counts number of case tokens subject to a case group token. * @param ast case group token. * @return number of case tokens. */ private static int countCaseTokens(DetailAST ast) { int counter = 0; for (DetailAST iterator = ast.getFirstChild(); iterator != null; iterator = iterator.getNextSibling()) { if (iterator.getType() == TokenTypes.LITERAL_CASE) { counter++; } } return counter; }
/** * Process validation of parameters for Methods with no definition. * @param ast method AST */ private void processAbstractMethodParameters(DetailAST ast) { final DetailAST parameters = ast.findFirstToken(TokenTypes.PARAMETERS); for (DetailAST child = parameters.getFirstChild(); child != null; child = child .getNextSibling()) { if (child.getType() == TokenTypes.PARAMETER_DEF) { checkForRedundantModifier(child, TokenTypes.FINAL); } } }
/** * Gets the last child node. * @return the last child node */ public DetailAST getLastChild() { DetailAST ast = getFirstChild(); while (ast != null && ast.getNextSibling() != null) { ast = ast.getNextSibling(); } return ast; }
@Override public boolean isValidOn(final DetailAST ast) { final int astType = ast.getType(); final DetailAST returnType = ast.findFirstToken(TokenTypes.TYPE); return astType == TokenTypes.METHOD_DEF && returnType.getFirstChild().getType() != TokenTypes.LITERAL_VOID; }
/** * Get super class name of given class. * @param classAst class * @return super class name or null if super class is not specified */ private static String getSuperClassName(DetailAST classAst) { String superClassName = null; final DetailAST classExtend = classAst.findFirstToken(TokenTypes.EXTENDS_CLAUSE); if (classExtend != null) { superClassName = extractQualifiedName(classExtend.getFirstChild()); } return superClassName; }
/** * Tests if the given expression node is surrounded by parentheses. * @param ast a {@code DetailAST} whose type is * {@code TokenTypes.EXPR}. * @return {@code true} if the expression is surrounded by * parentheses. */ private static boolean isExprSurrounded(DetailAST ast) { return ast.getFirstChild().getType() == TokenTypes.LPAREN; }