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
- Author:
- Simon Harris, Oliver Burn, Andrei Selkin
-
-
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 void
incrementCurrentValue(BigInteger amount)
Increments the current value by a specified amount.void
leaveToken(DetailAST ast)
Called after all the child nodes have been process.protected BigInteger
popValue()
Pops a value off the stack and makes it the current value.protected void
pushValue()
Push the current value on the stack.void
setMax(int max)
Set the maximum threshold allowed.void
setSwitchBlockAsSingleDecisionPoint(boolean switchBlockAsSingleDecisionPoint)
Sets whether to treat the whole switch block as a single decision point.void
visitToken(DetailAST ast)
Called to process a token.protected void
visitTokenHook(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 String MSG_KEY
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
CyclomaticComplexityCheck
public CyclomaticComplexityCheck()
-
-
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: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 final 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
-
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
-
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
-
incrementCurrentValue
protected final void incrementCurrentValue(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 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
-
-