Issue #1566: Fixed JavaNSCC violations

This commit is contained in:
Baratali Izmailov 2015-08-28 12:20:44 -04:00 committed by Roman Ivanov
parent 0024de5711
commit ee84bccfe1
3 changed files with 42 additions and 18 deletions

View File

@ -326,6 +326,7 @@
</module>
<module name="NPathComplexity"/>
<module name="JavaNCSS"/>
<!--
<module name="ClassDataAbstractionCoupling"/>
@ -334,7 +335,6 @@
<module name="IllegalToken"/>
<module name="JavadocParagraph"/>
<module name="JavadocTagContinuationIndentation"/>
<module name="JavaNCSS"/>
<module name="MissingCtor"/>
<module name="OneTopLevelClass"/>
<module name="OverloadMethodsDeclarationOrder"/>

View File

@ -58,6 +58,7 @@
<!-- Methods that build fake AST are very long-->
<suppress checks="MethodLength" files="src[\\/]test[\\/]java[\\/]com[\\/]puppycrawl[\\/]tools[\\/]checkstyle[\\/]comments[\\/]CommentsTest\.java"/>
<suppress checks="ExecutableStatementCount" files="src[\\/]test[\\/]java[\\/]com[\\/]puppycrawl[\\/]tools[\\/]checkstyle[\\/]comments[\\/]CommentsTest\.java"/>
<suppress checks="JavaNCSS" files="src[\\/]test[\\/]java[\\/]com[\\/]puppycrawl[\\/]tools[\\/]checkstyle[\\/]comments[\\/]CommentsTest\.java"/>
<suppress checks="." files=".*JavadocTokenTypes\.java"/>
<suppress checks="." files=".*ParseTreeBuilder\.java"/>
@ -77,4 +78,7 @@
<!-- getDetails() method - huge Switch, it has to be monolithic -->
<suppress checks="ExecutableStatementCount" files="RightCurlyCheck\.java" lines="280"/>
<suppress checks="JavaNCSS" files="RightCurlyCheck\.java" lines="280"/>
<suppress checks="CyclomaticComplexity" files="RightCurlyCheck\.java" lines="280"/>
</suppressions>

View File

@ -465,24 +465,14 @@ public class VariableDeclarationUsageDistanceCheck extends Check {
DetailAST currentScopeAst = ast;
DetailAST variableUsageAst = null;
while (currentScopeAst != null) {
final List<DetailAST> variableUsageExpressions = new ArrayList<>();
DetailAST currentStatementAst = currentScopeAst;
final Entry<List<DetailAST>, Integer> searchResult =
searchVariableUsageExpressions(variable, currentScopeAst);
currentScopeAst = null;
while (currentStatementAst != null
&& currentStatementAst.getType() != TokenTypes.RCURLY) {
if (currentStatementAst.getFirstChild() != null) {
if (isChild(currentStatementAst, variable)) {
variableUsageExpressions.add(currentStatementAst);
}
// If expression doesn't contain variable and this variable
// hasn't been met yet, than distance + 1.
else if (variableUsageExpressions.isEmpty()
&& currentStatementAst.getType() != TokenTypes.VARIABLE_DEF) {
dist++;
}
}
currentStatementAst = currentStatementAst.getNextSibling();
}
final List<DetailAST> variableUsageExpressions = searchResult.getKey();
dist += searchResult.getValue();
// If variable usage exists in a single scope, then look into
// this scope and count distance until variable usage.
if (variableUsageExpressions.size() == 1) {
@ -538,6 +528,36 @@ public class VariableDeclarationUsageDistanceCheck extends Check {
return new SimpleEntry<>(variableUsageAst, dist);
}
/**
* Searches variable usages starting from specified statement.
* @param variableAst Variable that is used.
* @param statementAst DetailAST to start searching from.
* @return entry which contains list with found expressions that use the variable
* and distance from specified statement to first found expression.
*/
private static Entry<List<DetailAST>, Integer>
searchVariableUsageExpressions(final DetailAST variableAst, final DetailAST statementAst) {
final List<DetailAST> variableUsageExpressions = new ArrayList<>();
int distance = 0;
DetailAST currentStatementAst = statementAst;
while (currentStatementAst != null
&& currentStatementAst.getType() != TokenTypes.RCURLY) {
if (currentStatementAst.getFirstChild() != null) {
if (isChild(currentStatementAst, variableAst)) {
variableUsageExpressions.add(currentStatementAst);
}
// If expression doesn't contain variable and this variable
// hasn't been met yet, than distance + 1.
else if (variableUsageExpressions.isEmpty()
&& currentStatementAst.getType() != TokenTypes.VARIABLE_DEF) {
distance++;
}
}
currentStatementAst = currentStatementAst.getNextSibling();
}
return new SimpleEntry<>(variableUsageExpressions, distance);
}
/**
* Gets first Ast node inside FOR, WHILE or DO-WHILE blocks if variable
* usage is met only inside the block (not in its declaration!).