Skip to content

Commit

Permalink
Issue checkstyle#3486: clear childCount field cache in DetailAST
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and agcuda committed Oct 30, 2016
1 parent edeae05 commit b4223c7
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
33 changes: 24 additions & 9 deletions src/main/java/com/puppycrawl/tools/checkstyle/api/DetailAST.java
Expand Up @@ -80,8 +80,8 @@ public void initialize(AST ast) {

@Override
public void setFirstChild(AST ast) {
clearBranchTokenTypesCache();
childCount = NOT_INITIALIZED;
clearBranchTokenTypes();
clearChildCountCache(this);
super.setFirstChild(ast);
if (ast != null) {
((DetailAST) ast).setParent(this);
Expand All @@ -90,7 +90,8 @@ public void setFirstChild(AST ast) {

@Override
public void setNextSibling(AST ast) {
clearBranchTokenTypesCache();
clearBranchTokenTypes();
clearChildCountCache(parent);
super.setNextSibling(ast);
if (ast != null && parent != null) {
((DetailAST) ast).setParent(parent);
Expand All @@ -106,7 +107,8 @@ public void setNextSibling(AST ast) {
* DetailAST object.
*/
public void addPreviousSibling(DetailAST ast) {
clearBranchTokenTypesCache();
clearBranchTokenTypes();
clearChildCountCache(parent);
if (ast != null) {
ast.setParent(parent);
final DetailAST previousSiblingNode = previousSibling;
Expand All @@ -130,7 +132,8 @@ else if (parent != null) {
* DetailAST object.
*/
public void addNextSibling(DetailAST ast) {
clearBranchTokenTypesCache();
clearBranchTokenTypes();
clearChildCountCache(parent);
if (ast != null) {
ast.setParent(parent);
final DetailAST nextSibling = getNextSibling();
Expand All @@ -147,7 +150,8 @@ public void addNextSibling(DetailAST ast) {

@Override
public void addChild(AST ast) {
clearBranchTokenTypesCache();
clearBranchTokenTypes();
clearChildCountCache(this);
super.addChild(ast);
if (ast != null) {
((DetailAST) ast).setParent(this);
Expand Down Expand Up @@ -194,7 +198,7 @@ public int getChildCount(int type) {
* @param parent the parent token
*/
private void setParent(DetailAST parent) {
clearBranchTokenTypesCache();
clearBranchTokenTypes();
this.parent = parent;
final DetailAST nextSibling = getNextSibling();
if (nextSibling != null) {
Expand Down Expand Up @@ -402,9 +406,20 @@ public DetailAST getFirstChild() {
}

/**
* Clears branchTokenTypes cache for all parents of the current DetailAST instance.
* Clears the child count for the ast instance.
* @param ast The ast to clear.
*/
private void clearBranchTokenTypesCache() {
private static void clearChildCountCache(DetailAST ast) {
if (ast != null) {
ast.childCount = NOT_INITIALIZED;
}
}

/**
* Clears branchTokenTypes cache for all parents of the current DetailAST instance, and the
* child count for the current DetailAST instance.
*/
private void clearBranchTokenTypes() {
DetailAST prevParent = getParent();
while (prevParent != null) {
prevParent.branchTokenTypes = null;
Expand Down
34 changes: 32 additions & 2 deletions src/test/java/com/puppycrawl/tools/checkstyle/CheckerTest.java
Expand Up @@ -818,10 +818,25 @@ public void visitToken(DetailAST ast) {
final int childCount = ast.getChildCount();
if (childCount != METHOD_DEF_CHILD_COUNT) {
final String msg = String.format(Locale.getDefault(),
"AST node has wrong number of children. Expected is %d but was %d",
"AST node in no comment tree has wrong number of children. "
+ "Expected is %d but was %d",
METHOD_DEF_CHILD_COUNT, childCount);
log(ast, msg);
}
// count children where comment lives
int actualChildCount = 0;
for (DetailAST child = ast.getFirstChild().getFirstChild(); child != null; child =
child.getNextSibling()) {
actualChildCount++;
}
final int cacheChildCount = ast.getFirstChild().getChildCount();
if (cacheChildCount != actualChildCount) {
final String msg = String.format(Locale.getDefault(),
"AST node with no comment has wrong number of children. "
+ "Expected is %d but was %d",
cacheChildCount, actualChildCount);
log(ast, msg);
}
}
}

Expand Down Expand Up @@ -858,10 +873,25 @@ public void visitToken(DetailAST ast) {
final int childCount = ast.getChildCount();
if (childCount != METHOD_DEF_CHILD_COUNT) {
final String msg = String.format(Locale.getDefault(),
"AST node has wrong number of children. Expected is %d but was %d",
"AST node in comment tree has wrong number of children. "
+ "Expected is %d but was %d",
METHOD_DEF_CHILD_COUNT, childCount);
log(ast, msg);
}
// count children where comment lives
int actualChildCount = 0;
for (DetailAST child = ast.getFirstChild().getFirstChild(); child != null; child =
child.getNextSibling()) {
actualChildCount++;
}
final int cacheChildCount = ast.getFirstChild().getChildCount();
if (cacheChildCount != actualChildCount) {
final String msg = String.format(Locale.getDefault(),
"AST node with comment has wrong number of children. "
+ "Expected is %d but was %d",
cacheChildCount, actualChildCount);
log(ast, msg);
}
}
}
}
Expand Up @@ -2,10 +2,11 @@

public class InputClearDetailAstLazyLoadCache {

/**
public
/*
* Javadoc comment
*/
public void foo() {
static void foo() {
return;
}
}

0 comments on commit b4223c7

Please sign in to comment.