Class CyclomaticComplexityCheck
- 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.CyclomaticComplexityCheck
-
- All Implemented Interfaces:
Configurable,Contextualizable
public class CyclomaticComplexityCheck extends AbstractCheck
Checks cyclomatic complexity against a specified limit. The complexity is measured by the number of "if", "while", "do", "for", "?:", "catch", "switch", "case", "&&" and "||" statements (plus one) in the body of the member. It is a measure of the minimum number of possible paths through the source and therefore the number of required tests. Generally 1-4 is considered good, 5-7 ok, 8-10 consider re-factoring, and 11+ re-factor now!Check has following properties:
switchBlockAsSingleDecisionPoint - controls whether to treat the whole switch block as a single decision point. Default value is false
-
-
Field Summary
Fields Modifier and Type Field Description private java.math.BigIntegercurrentValueThe current value.private static intDEFAULT_COMPLEXITY_VALUEDefault allowed complexity.private static java.math.BigIntegerINITIAL_VALUEThe initial current value.private intmaxThreshold to report error for.static java.lang.StringMSG_KEYA key is pointing to the warning message text in "messages.properties" file.private booleanswitchBlockAsSingleDecisionPointWhether to treat the whole switch block as a single decision point.private java.util.Deque<java.math.BigInteger>valueStackStack of values - all but the current value.
-
Constructor Summary
Constructors Constructor Description CyclomaticComplexityCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]getAcceptableTokens()The configurable token set.int[]getDefaultTokens()Returns the default token a check is interested in.int[]getRequiredTokens()The tokens that this check must be registered for.protected voidincrementCurrentValue(java.math.BigInteger amount)Increments the current value by a specified amount.private voidleaveMethodDef(DetailAST ast)Process the end of a method definition.voidleaveToken(DetailAST ast)Called after all the child nodes have been process.protected java.math.BigIntegerpopValue()Pops a value off the stack and makes it the current value.protected voidpushValue()Push the current value on the stack.voidsetMax(int max)Set the maximum threshold allowed.voidsetSwitchBlockAsSingleDecisionPoint(boolean switchBlockAsSingleDecisionPoint)Sets whether to treat the whole switch block as a single decision point.private voidvisitMethodDef()Process the start of the method definition.voidvisitToken(DetailAST ast)Called to process a token.protected voidvisitTokenHook(DetailAST ast)Hook called when visiting a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, 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
-
INITIAL_VALUE
private static final java.math.BigInteger INITIAL_VALUE
The initial current value.
-
DEFAULT_COMPLEXITY_VALUE
private static final int DEFAULT_COMPLEXITY_VALUE
Default allowed complexity.- See Also:
- Constant Field Values
-
valueStack
private final java.util.Deque<java.math.BigInteger> valueStack
Stack of values - all but the current value.
-
switchBlockAsSingleDecisionPoint
private boolean switchBlockAsSingleDecisionPoint
Whether to treat the whole switch block as a single decision point.
-
currentValue
private java.math.BigInteger currentValue
The current value.
-
max
private int max
Threshold to report error for.
-
-
Method Detail
-
setSwitchBlockAsSingleDecisionPoint
public void setSwitchBlockAsSingleDecisionPoint(boolean switchBlockAsSingleDecisionPoint)
Sets whether to treat the whole switch block as a single decision point.- Parameters:
switchBlockAsSingleDecisionPoint- whether to treat the whole switch block as a single decision point.
-
setMax
public final void setMax(int max)
Set the maximum threshold allowed.- Parameters:
max- the maximum threshold
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokensin classAbstractCheck- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheckThe 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:
getAcceptableTokensin classAbstractCheck- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public final int[] getRequiredTokens()
Description copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
getRequiredTokensin classAbstractCheck- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheckCalled to process a token.- Overrides:
visitTokenin classAbstractCheck- Parameters:
ast- the token to process
-
leaveToken
public void leaveToken(DetailAST ast)
Description copied from class:AbstractCheckCalled after all the child nodes have been process.- Overrides:
leaveTokenin classAbstractCheck- Parameters:
ast- the token leaving
-
visitTokenHook
protected final void visitTokenHook(DetailAST ast)
Hook called when visiting a token. Will not be called the method definition tokens.- Parameters:
ast- the token being visited
-
leaveMethodDef
private void leaveMethodDef(DetailAST ast)
Process the end of a method definition.- Parameters:
ast- the token representing the method definition
-
incrementCurrentValue
protected final void incrementCurrentValue(java.math.BigInteger amount)
Increments the current value by a specified amount.- Parameters:
amount- the amount to increment by
-
pushValue
protected final void pushValue()
Push the current value on the stack.
-
popValue
protected final java.math.BigInteger popValue()
Pops a value off the stack and makes it the current value.- Returns:
- pop a value off the stack and make it the current value
-
visitMethodDef
private void visitMethodDef()
Process the start of the method definition.
-
-