diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/AbstractSuperCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/AbstractSuperCheck.java index 8dffb5d88539..665f608e356e 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/AbstractSuperCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/AbstractSuperCheck.java @@ -136,7 +136,7 @@ private boolean isSuperCallInOverridingMethod(DetailAST ast) { */ private static boolean hasArguments(DetailAST methodCallDotAst) { final DetailAST argumentsList = methodCallDotAst.getNextSibling(); - return argumentsList.getChildCount() > 0; + return argumentsList.hasChildren(); } /** @@ -170,7 +170,7 @@ public void leaveToken(DetailAST ast) { /** * Determines whether an AST is a method definition for this check, - * with 0 parameters. + * without any parameters. * @param ast the method definition AST. * @return true if the method of ast is a method for this check. */ @@ -186,7 +186,7 @@ private boolean isOverridingMethod(DetailAST ast) { if (getMethodName().equals(name) && modifiersAST.findFirstToken(TokenTypes.LITERAL_NATIVE) == null) { final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS); - overridingMethod = params.getChildCount() == 0; + overridingMethod = !params.hasChildren(); } } return overridingMethod; diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java index 0602b7591f2f..16bb3064068b 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java @@ -431,7 +431,7 @@ private static boolean isInHashCodeMethod(DetailAST ast) { final DetailAST paramAST = methodDefAST.findFirstToken(TokenTypes.PARAMETERS); // we are in a 'public int hashCode()' method! The compiler will ensure // the method returns an 'int' and is public. - inHashCodeMethod = paramAST.getChildCount() == 0; + inHashCodeMethod = !paramAST.hasChildren(); } } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java index 3f71536821ad..145400fee486 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java @@ -388,10 +388,15 @@ private static boolean isExprSurrounded(DetailAST ast) { */ 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; + boolean result = false; + if (firstChild.getType() == TokenTypes.LPAREN) { + final DetailAST parameters = firstChild.getNextSibling(); + if (parameters.getChildCount(TokenTypes.PARAMETER_DEF) == 1 + && !parameters.getFirstChild().findFirstToken(TokenTypes.TYPE).hasChildren()) { + result = true; + } + } + return result; } /** diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ClassDefHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ClassDefHandler.java index b393d30b1a96..1a1d081cde2e 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ClassDefHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ClassDefHandler.java @@ -68,7 +68,10 @@ protected DetailAST getListChild() { @Override public void checkIndentation() { final DetailAST modifiers = getMainAst().findFirstToken(TokenTypes.MODIFIERS); - if (modifiers.getChildCount() == 0) { + if (modifiers.hasChildren()) { + checkModifiers(); + } + else { if (getMainAst().getType() != TokenTypes.ANNOTATION_DEF) { final DetailAST ident = getMainAst().findFirstToken(TokenTypes.IDENT); final int lineStart = getLineStart(ident); @@ -77,9 +80,6 @@ public void checkIndentation() { } } } - else { - checkModifiers(); - } if (getMainAst().getType() == TokenTypes.ANNOTATION_DEF) { final DetailAST atAst = getMainAst().findFirstToken(TokenTypes.AT); if (isOnStartOfLine(atAst)) { diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MemberDefHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MemberDefHandler.java index f5a05d347175..c7dfbbdaf23a 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MemberDefHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MemberDefHandler.java @@ -44,11 +44,11 @@ public MemberDefHandler(IndentationCheck indentCheck, @Override public void checkIndentation() { final DetailAST modifiersNode = getMainAst().findFirstToken(TokenTypes.MODIFIERS); - if (modifiersNode.getChildCount() == 0) { - checkType(); + if (modifiersNode.hasChildren()) { + checkModifiers(); } else { - checkModifiers(); + checkType(); } final DetailAST firstNode = getMainAst(); final DetailAST lastNode = getVarDefStatementSemicolon(firstNode); diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForInitializerPadCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForInitializerPadCheck.java index 2753f9c71802..762c2a69a781 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForInitializerPadCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForInitializerPadCheck.java @@ -108,7 +108,7 @@ public int[] getRequiredTokens() { @Override public void visitToken(DetailAST ast) { - if (ast.getChildCount() == 0) { + if (!ast.hasChildren()) { //empty for initializer. test pad before semi. final DetailAST semi = ast.getNextSibling(); final int semiLineIdx = semi.getLineNo() - 1; diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForIteratorPadCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForIteratorPadCheck.java index d82f1affaf78..764fb3862a58 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForIteratorPadCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForIteratorPadCheck.java @@ -108,7 +108,7 @@ public int[] getRequiredTokens() { @Override public void visitToken(DetailAST ast) { - if (ast.getChildCount() == 0) { + if (!ast.hasChildren()) { //empty for iterator. test pad after semi. final DetailAST semi = ast.getPreviousSibling(); final String line = getLines()[semi.getLineNo() - 1]; diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheck.java index 697c977162a8..84f708563485 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheck.java @@ -499,7 +499,7 @@ && hasNotAllowedTwoEmptyLinesBefore(ast)) { private void processPackage(DetailAST ast, DetailAST nextToken) { if (ast.getLineNo() > 1 && !hasEmptyLineBefore(ast)) { if (getFileContents().getFileName().endsWith("package-info.java")) { - if (ast.getFirstChild().getChildCount() == 0 && !isPrecededByJavadoc(ast)) { + if (!ast.getFirstChild().hasChildren() && !isPrecededByJavadoc(ast)) { log(ast.getLineNo(), MSG_SHOULD_BE_SEPARATED, ast.getText()); } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck.java index 8e2fcc10c59a..9723dd5a8a5f 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck.java @@ -149,7 +149,7 @@ private static boolean isInEmptyForInitializerOrCondition(DetailAST semicolonAst if (sibling != null && (sibling.getType() == TokenTypes.FOR_INIT || sibling.getType() == TokenTypes.FOR_CONDITION) - && sibling.getChildCount() == 0) { + && !sibling.hasChildren()) { result = true; } return result; diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck.java index 13eecfb23b3c..c7f79c8c299f 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck.java @@ -328,7 +328,7 @@ private static boolean isFollowsEmptyForIterator(DetailAST ast) { if (parent.findFirstToken(TokenTypes.FOR_EACH_CLAUSE) == null) { final DetailAST forIterator = parent.findFirstToken(TokenTypes.FOR_ITERATOR); - result = forIterator.getChildCount() == 0; + result = !forIterator.hasChildren(); } return result; } @@ -345,7 +345,7 @@ private static boolean isPrecedingEmptyForInit(DetailAST ast) { if (parent.findFirstToken(TokenTypes.FOR_EACH_CLAUSE) == null) { final DetailAST forIterator = parent.findFirstToken(TokenTypes.FOR_INIT); - result = forIterator.getChildCount() == 0; + result = !forIterator.hasChildren(); } return result; } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/gui/CodeSelectorPresentation.java b/src/main/java/com/puppycrawl/tools/checkstyle/gui/CodeSelectorPresentation.java index c7f6f0e65f9e..598a37a0bd50 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/gui/CodeSelectorPresentation.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/gui/CodeSelectorPresentation.java @@ -96,12 +96,11 @@ public void findSelectionPositions() { 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; + if (ast.hasChildren() || !TokenUtil.getTokenName(ast.getType()).equals(ast.getText())) { + selectionEnd = findLastPosition(ast); } else { - selectionEnd = findLastPosition(ast); + selectionEnd = selectionStart; } } @@ -123,12 +122,12 @@ private void findSelectionPositions(DetailNode detailNode) { */ private int findLastPosition(final DetailAST astNode) { final int lastPosition; - if (astNode.getChildCount() == 0) { - lastPosition = lines2position.get(astNode.getLineNo()) + astNode.getColumnNo() - + astNode.getText().length(); + if (astNode.hasChildren()) { + lastPosition = findLastPosition(astNode.getLastChild()); } else { - lastPosition = findLastPosition(astNode.getLastChild()); + lastPosition = lines2position.get(astNode.getLineNo()) + astNode.getColumnNo() + + astNode.getText().length(); } return lastPosition; } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/utils/BlockCommentPosition.java b/src/main/java/com/puppycrawl/tools/checkstyle/utils/BlockCommentPosition.java index 1023f9f98fb7..0899c32ad945 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/utils/BlockCommentPosition.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/utils/BlockCommentPosition.java @@ -169,15 +169,21 @@ public static boolean isOnConstructor(DetailAST blockComment) { * @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; + final DetailAST parent = blockComment.getParent(); + boolean result = false; + if (parent != null) { + if (parent.getType() == TokenTypes.ENUM_CONSTANT_DEF) { + final DetailAST prevSibling = getPrevSiblingSkipComments(blockComment); + if (prevSibling.getType() == TokenTypes.ANNOTATIONS && !prevSibling.hasChildren()) { + result = true; + } + } + else if (parent.getType() == TokenTypes.ANNOTATION + && parent.getParent().getParent().getType() == TokenTypes.ENUM_CONSTANT_DEF) { + result = true; + } + } + return result; } /** @@ -202,7 +208,7 @@ private static boolean isOnPlainToken(DetailAST blockComment, int parentTokenType, int nextTokenType) { return blockComment.getParent() != null && blockComment.getParent().getType() == parentTokenType - && getPrevSiblingSkipComments(blockComment).getChildCount() == 0 + && !getPrevSiblingSkipComments(blockComment).hasChildren() && getNextSiblingSkipComments(blockComment).getType() == nextTokenType; } @@ -251,7 +257,7 @@ private static boolean isOnPlainClassMember(DetailAST blockComment, int memberTy || parent.getType() == TokenTypes.TYPE_PARAMETERS) && parent.getParent().getType() == memberType // previous parent sibling is always TokenTypes.MODIFIERS - && parent.getPreviousSibling().getChildCount() == 0 + && !parent.getPreviousSibling().hasChildren() && parent.getParent().getParent().getType() == TokenTypes.OBJBLOCK; }