Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 7487 add method hasChildren() to DetailAST #7489

Merged
merged 2 commits into from Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -374,6 +374,11 @@ public DetailAstImpl getFirstChild() {
return (DetailAstImpl) super.getFirstChild();
}

@Override
public boolean hasChildren() {
return getFirstChild() != null;
}

/**
* Clears the child count for the ast instance.
* @param ast The ast to clear.
Expand Down
Expand Up @@ -118,4 +118,10 @@ public interface DetailAST {
*/
int getNumberOfChildren();

/**
* Returns whether this AST has any children.
*
* @return {@code true} if this AST has any children.
*/
boolean hasChildren();
}
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;
pbludov marked this conversation as resolved.
Show resolved Hide resolved
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
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
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
rnveach marked this conversation as resolved.
Show resolved Hide resolved
&& !parameters.getFirstChild().findFirstToken(TokenTypes.TYPE).hasChildren()) {
result = true;
}
}
return result;
}

/**
Expand Down
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
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
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
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
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
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
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
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
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;
romani marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Up @@ -19,6 +19,7 @@

package com.puppycrawl.tools.checkstyle;

import static com.google.common.truth.Truth.assertWithMessage;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -134,6 +135,20 @@ public void testGetChildCount() throws Exception {
assertEquals(firstLevelA, firstLevelB.getPreviousSibling(), "Invalid previous sibling");
}

@Test
public void testHasChildren() {
final DetailAstImpl root = new DetailAstImpl();
final DetailAstImpl child = new DetailAstImpl();
root.setFirstChild(child);

assertWithMessage("Root node should have children")
.that(root.hasChildren())
.isTrue();
assertWithMessage("Child node should have no children")
.that(child.hasChildren())
.isFalse();
}

@Test
public void testGetChildCountType() throws Exception {
final DetailAstImpl root = new DetailAstImpl();
Expand Down