Skip to content

Commit bd45909

Browse files
pbludovromani
authored andcommitted
Issue #7487: refactor code to use DetailAST.hasChildren()
1 parent 317e51f commit bd45909

13 files changed

+51
-43
lines changed

src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/AbstractSuperCheck.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private boolean isSuperCallInOverridingMethod(DetailAST ast) {
136136
*/
137137
private static boolean hasArguments(DetailAST methodCallDotAst) {
138138
final DetailAST argumentsList = methodCallDotAst.getNextSibling();
139-
return argumentsList.getChildCount() > 0;
139+
return argumentsList.hasChildren();
140140
}
141141

142142
/**
@@ -170,7 +170,7 @@ public void leaveToken(DetailAST ast) {
170170

171171
/**
172172
* Determines whether an AST is a method definition for this check,
173-
* with 0 parameters.
173+
* without any parameters.
174174
* @param ast the method definition AST.
175175
* @return true if the method of ast is a method for this check.
176176
*/
@@ -186,7 +186,7 @@ private boolean isOverridingMethod(DetailAST ast) {
186186
if (getMethodName().equals(name)
187187
&& modifiersAST.findFirstToken(TokenTypes.LITERAL_NATIVE) == null) {
188188
final DetailAST params = ast.findFirstToken(TokenTypes.PARAMETERS);
189-
overridingMethod = params.getChildCount() == 0;
189+
overridingMethod = !params.hasChildren();
190190
}
191191
}
192192
return overridingMethod;

src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/MagicNumberCheck.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ private static boolean isInHashCodeMethod(DetailAST ast) {
431431
final DetailAST paramAST = methodDefAST.findFirstToken(TokenTypes.PARAMETERS);
432432
// we are in a 'public int hashCode()' method! The compiler will ensure
433433
// the method returns an 'int' and is public.
434-
inHashCodeMethod = paramAST.getChildCount() == 0;
434+
inHashCodeMethod = !paramAST.hasChildren();
435435
}
436436
}
437437
}

src/main/java/com/puppycrawl/tools/checkstyle/checks/coding/UnnecessaryParenthesesCheck.java

+9-4
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,15 @@ private static boolean isExprSurrounded(DetailAST ast) {
388388
*/
389389
private static boolean isLambdaSingleParameterSurrounded(DetailAST ast) {
390390
final DetailAST firstChild = ast.getFirstChild();
391-
return firstChild.getType() == TokenTypes.LPAREN
392-
&& firstChild.getNextSibling().getChildCount(TokenTypes.PARAMETER_DEF) == 1
393-
&& firstChild.getNextSibling().getFirstChild().findFirstToken(TokenTypes.TYPE)
394-
.getChildCount() == 0;
391+
boolean result = false;
392+
if (firstChild.getType() == TokenTypes.LPAREN) {
393+
final DetailAST parameters = firstChild.getNextSibling();
394+
if (parameters.getChildCount(TokenTypes.PARAMETER_DEF) == 1
395+
&& !parameters.getFirstChild().findFirstToken(TokenTypes.TYPE).hasChildren()) {
396+
result = true;
397+
}
398+
}
399+
return result;
395400
}
396401

397402
/**

src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ClassDefHandler.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ protected DetailAST getListChild() {
6868
@Override
6969
public void checkIndentation() {
7070
final DetailAST modifiers = getMainAst().findFirstToken(TokenTypes.MODIFIERS);
71-
if (modifiers.getChildCount() == 0) {
71+
if (modifiers.hasChildren()) {
72+
checkModifiers();
73+
}
74+
else {
7275
if (getMainAst().getType() != TokenTypes.ANNOTATION_DEF) {
7376
final DetailAST ident = getMainAst().findFirstToken(TokenTypes.IDENT);
7477
final int lineStart = getLineStart(ident);
@@ -77,9 +80,6 @@ public void checkIndentation() {
7780
}
7881
}
7982
}
80-
else {
81-
checkModifiers();
82-
}
8383
if (getMainAst().getType() == TokenTypes.ANNOTATION_DEF) {
8484
final DetailAST atAst = getMainAst().findFirstToken(TokenTypes.AT);
8585
if (isOnStartOfLine(atAst)) {

src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MemberDefHandler.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public MemberDefHandler(IndentationCheck indentCheck,
4444
@Override
4545
public void checkIndentation() {
4646
final DetailAST modifiersNode = getMainAst().findFirstToken(TokenTypes.MODIFIERS);
47-
if (modifiersNode.getChildCount() == 0) {
48-
checkType();
47+
if (modifiersNode.hasChildren()) {
48+
checkModifiers();
4949
}
5050
else {
51-
checkModifiers();
51+
checkType();
5252
}
5353
final DetailAST firstNode = getMainAst();
5454
final DetailAST lastNode = getVarDefStatementSemicolon(firstNode);

src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForInitializerPadCheck.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public int[] getRequiredTokens() {
108108

109109
@Override
110110
public void visitToken(DetailAST ast) {
111-
if (ast.getChildCount() == 0) {
111+
if (!ast.hasChildren()) {
112112
//empty for initializer. test pad before semi.
113113
final DetailAST semi = ast.getNextSibling();
114114
final int semiLineIdx = semi.getLineNo() - 1;

src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyForIteratorPadCheck.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public int[] getRequiredTokens() {
108108

109109
@Override
110110
public void visitToken(DetailAST ast) {
111-
if (ast.getChildCount() == 0) {
111+
if (!ast.hasChildren()) {
112112
//empty for iterator. test pad after semi.
113113
final DetailAST semi = ast.getPreviousSibling();
114114
final String line = getLines()[semi.getLineNo() - 1];

src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/EmptyLineSeparatorCheck.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ && hasNotAllowedTwoEmptyLinesBefore(ast)) {
499499
private void processPackage(DetailAST ast, DetailAST nextToken) {
500500
if (ast.getLineNo() > 1 && !hasEmptyLineBefore(ast)) {
501501
if (getFileContents().getFileName().endsWith("package-info.java")) {
502-
if (ast.getFirstChild().getChildCount() == 0 && !isPrecededByJavadoc(ast)) {
502+
if (!ast.getFirstChild().hasChildren() && !isPrecededByJavadoc(ast)) {
503503
log(ast.getLineNo(), MSG_SHOULD_BE_SEPARATED, ast.getText());
504504
}
505505
}

src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/NoWhitespaceBeforeCheck.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private static boolean isInEmptyForInitializerOrCondition(DetailAST semicolonAst
149149
if (sibling != null
150150
&& (sibling.getType() == TokenTypes.FOR_INIT
151151
|| sibling.getType() == TokenTypes.FOR_CONDITION)
152-
&& sibling.getChildCount() == 0) {
152+
&& !sibling.hasChildren()) {
153153
result = true;
154154
}
155155
return result;

src/main/java/com/puppycrawl/tools/checkstyle/checks/whitespace/ParenPadCheck.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ private static boolean isFollowsEmptyForIterator(DetailAST ast) {
328328
if (parent.findFirstToken(TokenTypes.FOR_EACH_CLAUSE) == null) {
329329
final DetailAST forIterator =
330330
parent.findFirstToken(TokenTypes.FOR_ITERATOR);
331-
result = forIterator.getChildCount() == 0;
331+
result = !forIterator.hasChildren();
332332
}
333333
return result;
334334
}
@@ -345,7 +345,7 @@ private static boolean isPrecedingEmptyForInit(DetailAST ast) {
345345
if (parent.findFirstToken(TokenTypes.FOR_EACH_CLAUSE) == null) {
346346
final DetailAST forIterator =
347347
parent.findFirstToken(TokenTypes.FOR_INIT);
348-
result = forIterator.getChildCount() == 0;
348+
result = !forIterator.hasChildren();
349349
}
350350
return result;
351351
}

src/main/java/com/puppycrawl/tools/checkstyle/gui/CodeSelectorPresentation.java

+7-8
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,11 @@ public void findSelectionPositions() {
9696
private void findSelectionPositions(DetailAST ast) {
9797
selectionStart = lines2position.get(ast.getLineNo()) + ast.getColumnNo();
9898

99-
if (ast.getChildCount() == 0
100-
&& TokenUtil.getTokenName(ast.getType()).equals(ast.getText())) {
101-
selectionEnd = selectionStart;
99+
if (ast.hasChildren() || !TokenUtil.getTokenName(ast.getType()).equals(ast.getText())) {
100+
selectionEnd = findLastPosition(ast);
102101
}
103102
else {
104-
selectionEnd = findLastPosition(ast);
103+
selectionEnd = selectionStart;
105104
}
106105
}
107106

@@ -123,12 +122,12 @@ private void findSelectionPositions(DetailNode detailNode) {
123122
*/
124123
private int findLastPosition(final DetailAST astNode) {
125124
final int lastPosition;
126-
if (astNode.getChildCount() == 0) {
127-
lastPosition = lines2position.get(astNode.getLineNo()) + astNode.getColumnNo()
128-
+ astNode.getText().length();
125+
if (astNode.hasChildren()) {
126+
lastPosition = findLastPosition(astNode.getLastChild());
129127
}
130128
else {
131-
lastPosition = findLastPosition(astNode.getLastChild());
129+
lastPosition = lines2position.get(astNode.getLineNo()) + astNode.getColumnNo()
130+
+ astNode.getText().length();
132131
}
133132
return lastPosition;
134133
}

src/main/java/com/puppycrawl/tools/checkstyle/utils/BlockCommentPosition.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,21 @@ public static boolean isOnConstructor(DetailAST blockComment) {
169169
* @return true if node is before enum constant
170170
*/
171171
public static boolean isOnEnumConstant(DetailAST blockComment) {
172-
final boolean isOnPlainConst = blockComment.getParent() != null
173-
&& blockComment.getParent().getType() == TokenTypes.ENUM_CONSTANT_DEF
174-
&& getPrevSiblingSkipComments(blockComment).getType() == TokenTypes.ANNOTATIONS
175-
&& getPrevSiblingSkipComments(blockComment).getChildCount() == 0;
176-
final boolean isOnConstWithAnnotation = !isOnPlainConst && blockComment.getParent() != null
177-
&& blockComment.getParent().getType() == TokenTypes.ANNOTATION
178-
&& blockComment.getParent().getParent().getParent().getType()
179-
== TokenTypes.ENUM_CONSTANT_DEF;
180-
return isOnPlainConst || isOnConstWithAnnotation;
172+
final DetailAST parent = blockComment.getParent();
173+
boolean result = false;
174+
if (parent != null) {
175+
if (parent.getType() == TokenTypes.ENUM_CONSTANT_DEF) {
176+
final DetailAST prevSibling = getPrevSiblingSkipComments(blockComment);
177+
if (prevSibling.getType() == TokenTypes.ANNOTATIONS && !prevSibling.hasChildren()) {
178+
result = true;
179+
}
180+
}
181+
else if (parent.getType() == TokenTypes.ANNOTATION
182+
&& parent.getParent().getParent().getType() == TokenTypes.ENUM_CONSTANT_DEF) {
183+
result = true;
184+
}
185+
}
186+
return result;
181187
}
182188

183189
/**
@@ -202,7 +208,7 @@ private static boolean isOnPlainToken(DetailAST blockComment,
202208
int parentTokenType, int nextTokenType) {
203209
return blockComment.getParent() != null
204210
&& blockComment.getParent().getType() == parentTokenType
205-
&& getPrevSiblingSkipComments(blockComment).getChildCount() == 0
211+
&& !getPrevSiblingSkipComments(blockComment).hasChildren()
206212
&& getNextSiblingSkipComments(blockComment).getType() == nextTokenType;
207213
}
208214

@@ -251,7 +257,7 @@ private static boolean isOnPlainClassMember(DetailAST blockComment, int memberTy
251257
|| parent.getType() == TokenTypes.TYPE_PARAMETERS)
252258
&& parent.getParent().getType() == memberType
253259
// previous parent sibling is always TokenTypes.MODIFIERS
254-
&& parent.getPreviousSibling().getChildCount() == 0
260+
&& !parent.getPreviousSibling().hasChildren()
255261
&& parent.getParent().getParent().getType() == TokenTypes.OBJBLOCK;
256262
}
257263

src/test/java/com/puppycrawl/tools/checkstyle/DetailAstImplTest.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,10 @@ public void testGetChildCount() throws Exception {
136136
}
137137

138138
@Test
139-
public void testHasChildren() throws Exception {
139+
public void testHasChildren() {
140140
final DetailAstImpl root = new DetailAstImpl();
141141
final DetailAstImpl child = new DetailAstImpl();
142-
143142
root.setFirstChild(child);
144-
getSetParentMethod().invoke(child, root);
145143

146144
assertWithMessage("Root node should have children")
147145
.that(root.hasChildren())

0 commit comments

Comments
 (0)