Class 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 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.
    • 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 class AbstractCheck
        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 class AbstractCheck
        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 class AbstractCheck
        Returns:
        the token set this must be registered for.
        See Also:
        TokenTypes
      • leaveToken

        public void leaveToken​(DetailAST ast)
        Description copied from class: AbstractCheck
        Called after all the child nodes have been process.
        Overrides:
        leaveToken in class AbstractCheck
        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.