Class NPathComplexityCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.metrics.NPathComplexityCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public final class NPathComplexityCheck extends AbstractCheck
Checks the npath complexity against a specified limit (default = 200). The npath metric computes the number of possible execution paths through a function. Similar to the cyclomatic complexity but also takes into account the nesting of conditional statements and multi-part boolean expressions.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
NPathComplexityCheck.TokenEnd
Coordinates of token end.private static class
NPathComplexityCheck.Values
Class that store range value and expression value.
-
Field Summary
Fields Modifier and Type Field Description private boolean
branchVisited
True, when branch is visited, but not leaved.private java.math.BigInteger
currentRangeValue
NP value for current range.private static int
DEFAULT_MAX
Default allowed complexity.private java.util.Deque<java.lang.Integer>
expressionValues
Stack of NP values for expressions.private static java.math.BigInteger
INITIAL_VALUE
The initial current value.private java.util.Deque<java.lang.Boolean>
isAfterValues
Stack of belongs to range values for question operator.private int
max
Threshold to report error for.static java.lang.String
MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.private NPathComplexityCheck.TokenEnd
processingTokenEnd
Range of the last processed expression.private java.util.Deque<java.math.BigInteger>
rangeValues
Stack of NP values for ranges.
-
Constructor Summary
Constructors Constructor Description NPathComplexityCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
beginTree(DetailAST rootAST)
Called before the starting to process a tree.private static int
countCaseTokens(DetailAST ast)
Counts number of case tokens subject to a case group token.private static int
countConditionalOperators(DetailAST ast)
Calculates number of conditional operators, including inline ternary operatior, for a token.int[]
getAcceptableTokens()
The configurable token set.int[]
getDefaultTokens()
Returns the default token a check is interested in.private static DetailAST
getLastToken(DetailAST ast)
Finds a leaf, which is the most distant from the root.int[]
getRequiredTokens()
The tokens that this check must be registered for.private void
leaveAddingConditional()
Leaves catch.private void
leaveBranch()
Leaves else, default or case group tokens.private void
leaveConditional()
Leaves while, do, for, if, ternary (?::), return or switch.private void
leaveMethodDef(DetailAST ast)
Process the end of a method definition.private void
leaveMultiplyingConditional()
Leaves try.void
leaveToken(DetailAST ast)
Called after all the child nodes have been process.private void
leaveUnitaryOperator()
Leaves ternary operator (?:) and return tokens.private NPathComplexityCheck.Values
popValue()
Pops values from both stack of expression values and stack of range values.private void
pushValue(java.lang.Integer expressionValue)
Pushes the current range value on the range value stack.void
setMax(int max)
Set the maximum threshold allowed.private void
visitConditional(DetailAST ast, int basicBranchingFactor)
Visits if, while, do-while, for and switch tokens - all of them have expression in parentheses which is used for calculation.void
visitToken(DetailAST ast)
Called to process a token.private void
visitUnitaryOperator(DetailAST ast, int basicBranchingFactor)
Visits ternary operator (?:) and return tokens.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY
public static final java.lang.String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
DEFAULT_MAX
private static final int DEFAULT_MAX
Default allowed complexity.- See Also:
- Constant Field Values
-
INITIAL_VALUE
private static final java.math.BigInteger INITIAL_VALUE
The initial current value.
-
rangeValues
private final java.util.Deque<java.math.BigInteger> rangeValues
Stack of NP values for ranges.
-
expressionValues
private final java.util.Deque<java.lang.Integer> expressionValues
Stack of NP values for expressions.
-
isAfterValues
private final java.util.Deque<java.lang.Boolean> isAfterValues
Stack of belongs to range values for question operator.
-
processingTokenEnd
private final NPathComplexityCheck.TokenEnd processingTokenEnd
Range of the last processed expression. Used for checking that ternary operation which is a part of expression won't be processed for second time.
-
currentRangeValue
private java.math.BigInteger currentRangeValue
NP value for current range.
-
max
private int max
Threshold to report error for.
-
branchVisited
private boolean branchVisited
True, when branch is visited, but not leaved.
-
-
Method Detail
-
setMax
public void setMax(int max)
Set the maximum threshold allowed.- Parameters:
max
- the maximum threshold
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
beginTree
public void beginTree(DetailAST rootAST)
Description copied from class:AbstractCheck
Called before the starting to process a tree. Ideal place to initialize information that is to be collected whilst processing a tree.- Overrides:
beginTree
in classAbstractCheck
- Parameters:
rootAST
- the root of the tree
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
leaveToken
public void leaveToken(DetailAST ast)
Description copied from class:AbstractCheck
Called after all the child nodes have been process.- Overrides:
leaveToken
in classAbstractCheck
- Parameters:
ast
- the token leaving
-
visitConditional
private void visitConditional(DetailAST ast, int basicBranchingFactor)
Visits if, while, do-while, for and switch tokens - all of them have expression in parentheses which is used for calculation.- Parameters:
ast
- visited token.basicBranchingFactor
- default number of branches added.
-
visitUnitaryOperator
private void visitUnitaryOperator(DetailAST ast, int basicBranchingFactor)
Visits ternary operator (?:) and return tokens. They differ from those processed by visitConditional method in that their expression isn't bracketed.- Parameters:
ast
- visited token.basicBranchingFactor
- number of branches inherently added by this token.
-
leaveUnitaryOperator
private void leaveUnitaryOperator()
Leaves ternary operator (?:) and return tokens.
-
leaveConditional
private void leaveConditional()
Leaves while, do, for, if, ternary (?::), return or switch.
-
leaveBranch
private void leaveBranch()
Leaves else, default or case group tokens.
-
leaveMethodDef
private void leaveMethodDef(DetailAST ast)
Process the end of a method definition.- Parameters:
ast
- the token type representing the method definition
-
leaveAddingConditional
private void leaveAddingConditional()
Leaves catch.
-
pushValue
private void pushValue(java.lang.Integer expressionValue)
Pushes the current range value on the range value stack. Pushes this token expression value on the expression value stack.- Parameters:
expressionValue
- value of expression calculated for current token.
-
popValue
private NPathComplexityCheck.Values popValue()
Pops values from both stack of expression values and stack of range values.- Returns:
- pair of head values from both of the stacks.
-
leaveMultiplyingConditional
private void leaveMultiplyingConditional()
Leaves try.
-
countConditionalOperators
private static int countConditionalOperators(DetailAST ast)
Calculates number of conditional operators, including inline ternary operatior, for a token.- Parameters:
ast
- inspected token.- Returns:
- number of conditional operators.
- See Also:
- Java Language Specification, §15.23, Java Language Specification, §15.24, Java Language Specification, §15.25
-
getLastToken
private static DetailAST getLastToken(DetailAST ast)
Finds a leaf, which is the most distant from the root.- Parameters:
ast
- the root of tree.- Returns:
- the leaf.
-
countCaseTokens
private static int countCaseTokens(DetailAST ast)
Counts number of case tokens subject to a case group token.- Parameters:
ast
- case group token.- Returns:
- number of case tokens.
-
-