Skip to content

Commit

Permalink
Issue #7487: refactor code to use DetailAST.hasChildren()
Browse files Browse the repository at this point in the history
  • Loading branch information
pbludov authored and romani committed Jan 26, 2020
1 parent 317e51f commit bd45909
Show file tree
Hide file tree
Showing 13 changed files with 51 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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.
*/
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,10 @@ public void testGetChildCount() throws Exception {
}

@Test
public void testHasChildren() throws Exception {
public void testHasChildren() {
final DetailAstImpl root = new DetailAstImpl();
final DetailAstImpl child = new DetailAstImpl();

root.setFirstChild(child);
getSetParentMethod().invoke(child, root);

assertWithMessage("Root node should have children")
.that(root.hasChildren())
Expand Down

0 comments on commit bd45909

Please sign in to comment.