Correct ExecutableStatementCountCheck counts for inner members when configured for a subset of tokens.

This commit is contained in:
Rick Giles 2003-08-31 10:04:27 +00:00
parent b8f9b199ad
commit b0bb7e1b50
4 changed files with 62 additions and 11 deletions

View File

@ -101,7 +101,7 @@ public final class ExecutableStatementCountCheck
case TokenTypes.METHOD_DEF:
case TokenTypes.INSTANCE_INIT:
case TokenTypes.STATIC_INIT:
visitMethodDef();
visitMemberDef(aAST);
break;
case TokenTypes.SLIST:
visitSlist(aAST);
@ -119,7 +119,7 @@ public final class ExecutableStatementCountCheck
case TokenTypes.METHOD_DEF:
case TokenTypes.INSTANCE_INIT:
case TokenTypes.STATIC_INIT:
leaveMethodDef(aAST);
leaveMemberDef(aAST);
break;
case TokenTypes.SLIST:
// Do nothing
@ -129,19 +129,22 @@ public final class ExecutableStatementCountCheck
}
}
/** Process the start of the method definition. */
private void visitMethodDef()
/**
* Process the start of the member definition.
* @param aAST the token representing the member definition.
*/
private void visitMemberDef(DetailAST aAST)
{
mContextStack.push(mContext);
mContext = new Context();
mContext = new Context(aAST);
}
/**
* Process the end of a method definition.
* Process the end of a member definition.
*
* @param aAST the token representing the method definition.
* @param aAST the token representing the member definition.
*/
private void leaveMethodDef(DetailAST aAST)
private void leaveMemberDef(DetailAST aAST)
{
final int count = mContext.getCount();
if (count > getMax()) {
@ -163,7 +166,23 @@ public final class ExecutableStatementCountCheck
private void visitSlist(DetailAST aAST)
{
if (mContext != null) {
mContext.addCount(aAST.getChildCount() / 2);
// find member AST for the statement list
final DetailAST contextAST = mContext.getAST();
DetailAST parent = aAST.getParent();
while (parent != null) {
final int type = parent.getType();
if ((type == TokenTypes.CTOR_DEF)
|| (type == TokenTypes.METHOD_DEF)
|| (type == TokenTypes.INSTANCE_INIT)
|| (type == TokenTypes.STATIC_INIT))
{
if (parent == contextAST) {
mContext.addCount(aAST.getChildCount() / 2);
}
break;
}
parent = parent.getParent();
}
}
}
@ -173,14 +192,19 @@ public final class ExecutableStatementCountCheck
*/
private class Context
{
/** Member AST node. */
private DetailAST mAST;
/** Counter for context elements. */
private int mCount;
/**
* Creates new method context.
* Creates new member context.
* @param aAST member AST node.
*/
public Context()
public Context(DetailAST aAST)
{
mAST = aAST;
mCount = 0;
}
@ -193,6 +217,15 @@ public final class ExecutableStatementCountCheck
mCount += aCount;
}
/**
* Gets the member AST node.
* @return the member AST node.
*/
public DetailAST getAST()
{
return mAST;
}
/**
* Gets the count.
* @return the count.

View File

@ -71,4 +71,16 @@ public class ComplexityCheckTestInput {
} else {
}
}
/** Inner */
public ComplexityCheckTestInput(int aParam)
{
Runnable runnable = new Runnable() {
public void run() {
while (true) {
}
}
};
new Thread(runnable).start();
}
}

View File

@ -22,6 +22,8 @@ public class CyclomaticComplexityCheckTest
"48:5: Cyclomatic Complexity is 4 (max allowed is 0).",
"58:5: Cyclomatic Complexity is 4 (max allowed is 0).",
"67:5: Cyclomatic Complexity is 4 (max allowed is 0).",
"76:5: Cyclomatic Complexity is 1 (max allowed is 0).",
"79:13: Cyclomatic Complexity is 2 (max allowed is 0).",
};
verify(checkConfig, getPath("ComplexityCheckTestInput.java"), expected);

View File

@ -22,6 +22,8 @@ public class ExecutableStatementCountCheckTest
"48:5: Executable statement count is 2 (max allowed is 0).",
"58:5: Executable statement count is 2 (max allowed is 0).",
"67:5: Executable statement count is 2 (max allowed is 0).",
"76:5: Executable statement count is 2 (max allowed is 0).",
"79:13: Executable statement count is 1 (max allowed is 0).",
};
verify(checkConfig, getPath("ComplexityCheckTestInput.java"), expected);
@ -41,6 +43,7 @@ public class ExecutableStatementCountCheckTest
"17:5: Executable statement count is 2 (max allowed is 0).",
"27:5: Executable statement count is 1 (max allowed is 0).",
"34:5: Executable statement count is 3 (max allowed is 0).",
"79:13: Executable statement count is 1 (max allowed is 0).",
};
verify(checkConfig, getPath("ComplexityCheckTestInput.java"), expected);
@ -56,6 +59,7 @@ public class ExecutableStatementCountCheckTest
final String[] expected = {
"48:5: Executable statement count is 2 (max allowed is 0).",
"76:5: Executable statement count is 2 (max allowed is 0).",
};
verify(checkConfig, getPath("ComplexityCheckTestInput.java"), expected);