/** * Verify that method call has one argument. * * @param methodCall METHOD_CALL DetailAST * @return true if method call has one argument. */ private static boolean containsOneArgument(DetailAST methodCall) { final DetailAST elist = methodCall.findFirstToken(TokenTypes.ELIST); return elist.getChildCount() == 1; }
/** * Does method have any arguments. * @param methodCallDotAst DOT DetailAST * @return true if any parameters found */ private static boolean hasArguments(DetailAST methodCallDotAst) { final DetailAST argumentsList = methodCallDotAst.getNextSibling(); return argumentsList.getChildCount() > 0; }
/** * Returns whether a token represents an SLIST as part of an ELSE * statement. * @param ast the token to check * @return whether the toke does represent an SLIST as part of an ELSE */ private static boolean isElseWithCurlyBraces(DetailAST ast) { return ast.getType() == TokenTypes.SLIST && ast.getChildCount() == 2 && isElse(ast.getParent()); }
/** * Checks number of throws statements. * @param ast throws for check. */ private void visitLiteralThrows(DetailAST ast) { if ((!ignorePrivateMethods || !isInPrivateMethod(ast)) && !isOverriding(ast)) { // Account for all the commas! final int count = (ast.getChildCount() + 1) / 2; if (count > max) { log(ast, MSG_KEY, count, max); } } }
/** * Whether the method definition has the same name and number of parameters. * @param ident the specified method call IDENT ast. * @param ast the ast of a method definition to compare with. * @return true if a method definition has the same name and number of parameters * as the method call. */ private static boolean isSimilarSignature(DetailAST ident, DetailAST ast) { boolean result = false; final DetailAST elistToken = ident.getParent().findFirstToken(TokenTypes.ELIST); if (elistToken != null && ident.getText().equals(ast.getText())) { final int paramsNumber = ast.getParent().findFirstToken(TokenTypes.PARAMETERS).getChildCount(); final int argsNumber = elistToken.getChildCount(); result = paramsNumber == argsNumber; } return result; }
/** * Checks for expanded style type violations. * * @param annotation the annotation token */ private void checkExpandedStyle(final DetailAST annotation) { final int valuePairCount = annotation.getChildCount(TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR); if (valuePairCount == 0 && annotation.branchContains(TokenTypes.EXPR)) { log(annotation.getLineNo(), MSG_KEY_ANNOTATION_INCORRECT_STYLE, ElementStyle.EXPANDED); } }
/** * Tests if the given lambda node has a single parameter, no defined type, and is surrounded * by parentheses. * @param ast a {@code DetailAST} whose type is * {@code TokenTypes.LAMBDA}. * @return {@code true} if the lambda has a single parameter, no defined type, and is * surrounded by parentheses. */ private static boolean isLambdaSingleParameterSurrounded(DetailAST ast) { final DetailAST firstChild = ast.getFirstChild(); return firstChild.getType() == TokenTypes.LPAREN && firstChild.getNextSibling().getChildCount(TokenTypes.PARAMETER_DEF) == 1 && firstChild.getNextSibling().getFirstChild().findFirstToken(TokenTypes.TYPE) .getChildCount() == 0; }
/** * Checks that a token follows an empty for iterator. * @param ast the token to check * @return whether a token follows an empty for iterator */ private static boolean isFollowsEmptyForIterator(DetailAST ast) { boolean result = false; final DetailAST parent = ast.getParent(); //Only traditional for statements are examined, not for-each statements if (parent.findFirstToken(TokenTypes.FOR_EACH_CLAUSE) == null) { final DetailAST forIterator = parent.findFirstToken(TokenTypes.FOR_ITERATOR); result = forIterator.getChildCount() == 0; } return result; }
/** * Finds the last position of node without children. * @param astNode DetailAST node. * @return Last position of node without children. */ private int findLastPosition(final DetailAST astNode) { final int lastPosition; if (astNode.getChildCount() == 0) { lastPosition = lines2position.get(astNode.getLineNo()) + astNode.getColumnNo() + astNode.getText().length(); } else { lastPosition = findLastPosition(astNode.getLastChild()); } return lastPosition; }
/** * Checks that semicolon is in empty for initializer or condition. * @param semicolonAst DetailAST of semicolon. * @return true if semicolon is in empty for initializer or condition. */ private static boolean isInEmptyForInitializerOrCondition(DetailAST semicolonAst) { boolean result = false; final DetailAST sibling = semicolonAst.getPreviousSibling(); if (sibling != null && (sibling.getType() == TokenTypes.FOR_INIT || sibling.getType() == TokenTypes.FOR_CONDITION) && sibling.getChildCount() == 0) { result = true; } return result; }
/** * Checks that a token precedes an empty for initializer. * @param ast the token to check * @return whether a token precedes an empty for initializer */ private static boolean isPrecedingEmptyForInit(DetailAST ast) { boolean result = false; final DetailAST parent = ast.getParent(); //Only traditional for statements are examined, not for-each statements if (parent.findFirstToken(TokenTypes.FOR_EACH_CLAUSE) == null) { final DetailAST forIterator = parent.findFirstToken(TokenTypes.FOR_INIT); result = forIterator.getChildCount() == 0; } return result; }
/** * Find start and end selection positions from AST line and Column. * @param ast DetailAST node for which selection finds */ private void findSelectionPositions(DetailAST ast) { selectionStart = lines2position.get(ast.getLineNo()) + ast.getColumnNo(); if (ast.getChildCount() == 0 && TokenUtil.getTokenName(ast.getType()).equals(ast.getText())) { selectionEnd = selectionStart; } else { selectionEnd = findLastPosition(ast); } }
@Override public void visitToken(DetailAST ast) { final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS); final int count = params.getChildCount(TokenTypes.PARAMETER_DEF); if (count > max && !shouldIgnoreNumberOfParameters(ast)) { final DetailAST name = ast.findFirstToken(TokenTypes.IDENT); log(name, MSG_KEY, max, count); } }
/** * Checks that block comment is on specified token without any modifiers. * @param blockComment block comment start DetailAST * @param parentTokenType parent token type * @param nextTokenType next token type * @return true if block comment is on specified token without modifiers */ private static boolean isOnPlainToken(DetailAST blockComment, int parentTokenType, int nextTokenType) { return blockComment.getParent() != null && blockComment.getParent().getType() == parentTokenType && getPrevSiblingSkipComments(blockComment).getChildCount() == 0 && getNextSiblingSkipComments(blockComment).getType() == nextTokenType; }
/** * Checks that block comment is on specified class member without any modifiers. * @param blockComment block comment start DetailAST * @param memberType parent token type * @return true if block comment is on specified token without modifiers */ private static boolean isOnPlainClassMember(DetailAST blockComment, int memberType) { DetailAST parent = blockComment.getParent(); // type could be in fully qualified form, so we go up to Type token while (parent != null && parent.getType() == TokenTypes.DOT) { parent = parent.getParent(); } return parent != null && parent.getType() == TokenTypes.TYPE && parent.getParent().getType() == memberType // previous parent sibling is always TokenTypes.MODIFIERS && parent.getPreviousSibling().getChildCount() == 0; }
@Test public void testSetSiblingNull() throws Exception { final DetailAST root = new DetailAST(); final DetailAST firstLevelA = new DetailAST(); root.setFirstChild(firstLevelA); assertEquals("Invalid child count", 1, root.getChildCount()); getSetParentMethod().invoke(firstLevelA, root); firstLevelA.addPreviousSibling(null); firstLevelA.addNextSibling(null); assertEquals("Invalid child count", 1, root.getChildCount()); }
@Override public void checkIndentation() { final DetailAST modifiersNode = getMainAst().findFirstToken(TokenTypes.MODIFIERS); if (modifiersNode.getChildCount() == 0) { checkType(); } else { checkModifiers(); } final DetailAST firstNode = getMainAst(); final DetailAST lastNode = getVarDefStatementSemicolon(firstNode); if (lastNode != null && !isArrayDeclaration(firstNode)) { checkWrappingIndentation(firstNode, lastNode); } }
@Override public void visitToken(DetailAST arrayInit) { final DetailAST rcurly = arrayInit.findFirstToken(TokenTypes.RCURLY); final DetailAST previousSibling = rcurly.getPreviousSibling(); if (arrayInit.getLineNo() != rcurly.getLineNo() && arrayInit.getChildCount() != 1 && rcurly.getLineNo() != previousSibling.getLineNo() && arrayInit.getLineNo() != previousSibling.getLineNo() && previousSibling.getType() != TokenTypes.COMMA) { log(rcurly.getLineNo(), MSG_KEY); } }
@Test public void testInsertSiblingBetween() throws Exception { final DetailAST root = new DetailAST(); final DetailAST firstLevelA = new DetailAST(); final DetailAST firstLevelB = new DetailAST(); final DetailAST firstLevelC = new DetailAST(); assertEquals("Invalid child count", 0, root.getChildCount()); root.setFirstChild(firstLevelA); final Method setParentMethod = getSetParentMethod(); setParentMethod.invoke(firstLevelA, root); assertEquals("Invalid child count", 1, root.getChildCount()); firstLevelA.addNextSibling(firstLevelB); setParentMethod.invoke(firstLevelB, root); assertEquals("Invalid next sibling", firstLevelB, firstLevelA.getNextSibling()); firstLevelA.addNextSibling(firstLevelC); setParentMethod.invoke(firstLevelC, root); assertEquals("Invalid next sibling", firstLevelC, firstLevelA.getNextSibling()); }
/** * Node is on enum constant. * @param blockComment DetailAST * @return true if node is before enum constant */ public static boolean isOnEnumConstant(DetailAST blockComment) { final boolean isOnPlainConst = blockComment.getParent() != null && blockComment.getParent().getType() == TokenTypes.ENUM_CONSTANT_DEF && getPrevSiblingSkipComments(blockComment).getType() == TokenTypes.ANNOTATIONS && getPrevSiblingSkipComments(blockComment).getChildCount() == 0; final boolean isOnConstWithAnnotation = !isOnPlainConst && blockComment.getParent() != null && blockComment.getParent().getType() == TokenTypes.ANNOTATION && blockComment.getParent().getParent().getParent().getType() == TokenTypes.ENUM_CONSTANT_DEF; return isOnPlainConst || isOnConstWithAnnotation; }