001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2018 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks.metrics;
021
022import java.math.BigInteger;
023import java.util.ArrayDeque;
024import java.util.Deque;
025
026import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
027import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
028import com.puppycrawl.tools.checkstyle.api.DetailAST;
029import com.puppycrawl.tools.checkstyle.api.TokenTypes;
030
031/**
032 * Checks cyclomatic complexity against a specified limit. The complexity is
033 * measured by the number of "if", "while", "do", "for", "?:", "catch",
034 * "switch", "case", "&&" and "||" statements (plus one) in the body of
035 * the member. It is a measure of the minimum number of possible paths through
036 * the source and therefore the number of required tests. Generally 1-4 is
037 * considered good, 5-7 ok, 8-10 consider re-factoring, and 11+ re-factor now!
038 *
039 * <p>Check has following properties:
040 *
041 * <p><b>switchBlockAsSingleDecisionPoint</b> - controls whether to treat the whole switch
042 * block as a single decision point. Default value is <b>false</b>
043 *
044 *
045 */
046@FileStatefulCheck
047public class CyclomaticComplexityCheck
048    extends AbstractCheck {
049
050    /**
051     * A key is pointing to the warning message text in "messages.properties"
052     * file.
053     */
054    public static final String MSG_KEY = "cyclomaticComplexity";
055
056    /** The initial current value. */
057    private static final BigInteger INITIAL_VALUE = BigInteger.ONE;
058
059    /** Default allowed complexity. */
060    private static final int DEFAULT_COMPLEXITY_VALUE = 10;
061
062    /** Stack of values - all but the current value. */
063    private final Deque<BigInteger> valueStack = new ArrayDeque<>();
064
065    /** Whether to treat the whole switch block as a single decision point.*/
066    private boolean switchBlockAsSingleDecisionPoint;
067
068    /** The current value. */
069    private BigInteger currentValue = INITIAL_VALUE;
070
071    /** Threshold to report error for. */
072    private int max = DEFAULT_COMPLEXITY_VALUE;
073
074    /**
075     * Sets whether to treat the whole switch block as a single decision point.
076     * @param switchBlockAsSingleDecisionPoint whether to treat the whole switch
077     *                                          block as a single decision point.
078     */
079    public void setSwitchBlockAsSingleDecisionPoint(boolean switchBlockAsSingleDecisionPoint) {
080        this.switchBlockAsSingleDecisionPoint = switchBlockAsSingleDecisionPoint;
081    }
082
083    /**
084     * Set the maximum threshold allowed.
085     *
086     * @param max the maximum threshold
087     */
088    public final void setMax(int max) {
089        this.max = max;
090    }
091
092    @Override
093    public int[] getDefaultTokens() {
094        return new int[] {
095            TokenTypes.CTOR_DEF,
096            TokenTypes.METHOD_DEF,
097            TokenTypes.INSTANCE_INIT,
098            TokenTypes.STATIC_INIT,
099            TokenTypes.LITERAL_WHILE,
100            TokenTypes.LITERAL_DO,
101            TokenTypes.LITERAL_FOR,
102            TokenTypes.LITERAL_IF,
103            TokenTypes.LITERAL_SWITCH,
104            TokenTypes.LITERAL_CASE,
105            TokenTypes.LITERAL_CATCH,
106            TokenTypes.QUESTION,
107            TokenTypes.LAND,
108            TokenTypes.LOR,
109        };
110    }
111
112    @Override
113    public int[] getAcceptableTokens() {
114        return new int[] {
115            TokenTypes.CTOR_DEF,
116            TokenTypes.METHOD_DEF,
117            TokenTypes.INSTANCE_INIT,
118            TokenTypes.STATIC_INIT,
119            TokenTypes.LITERAL_WHILE,
120            TokenTypes.LITERAL_DO,
121            TokenTypes.LITERAL_FOR,
122            TokenTypes.LITERAL_IF,
123            TokenTypes.LITERAL_SWITCH,
124            TokenTypes.LITERAL_CASE,
125            TokenTypes.LITERAL_CATCH,
126            TokenTypes.QUESTION,
127            TokenTypes.LAND,
128            TokenTypes.LOR,
129        };
130    }
131
132    @Override
133    public final int[] getRequiredTokens() {
134        return new int[] {
135            TokenTypes.CTOR_DEF,
136            TokenTypes.METHOD_DEF,
137            TokenTypes.INSTANCE_INIT,
138            TokenTypes.STATIC_INIT,
139        };
140    }
141
142    @Override
143    public void visitToken(DetailAST ast) {
144        switch (ast.getType()) {
145            case TokenTypes.CTOR_DEF:
146            case TokenTypes.METHOD_DEF:
147            case TokenTypes.INSTANCE_INIT:
148            case TokenTypes.STATIC_INIT:
149                visitMethodDef();
150                break;
151            default:
152                visitTokenHook(ast);
153        }
154    }
155
156    @Override
157    public void leaveToken(DetailAST ast) {
158        switch (ast.getType()) {
159            case TokenTypes.CTOR_DEF:
160            case TokenTypes.METHOD_DEF:
161            case TokenTypes.INSTANCE_INIT:
162            case TokenTypes.STATIC_INIT:
163                leaveMethodDef(ast);
164                break;
165            default:
166                break;
167        }
168    }
169
170    /**
171     * Hook called when visiting a token. Will not be called the method
172     * definition tokens.
173     *
174     * @param ast the token being visited
175     */
176    private void visitTokenHook(DetailAST ast) {
177        if (switchBlockAsSingleDecisionPoint) {
178            if (ast.getType() != TokenTypes.LITERAL_CASE) {
179                incrementCurrentValue(BigInteger.ONE);
180            }
181        }
182        else if (ast.getType() != TokenTypes.LITERAL_SWITCH) {
183            incrementCurrentValue(BigInteger.ONE);
184        }
185    }
186
187    /**
188     * Process the end of a method definition.
189     *
190     * @param ast the token representing the method definition
191     */
192    private void leaveMethodDef(DetailAST ast) {
193        final BigInteger bigIntegerMax = BigInteger.valueOf(max);
194        if (currentValue.compareTo(bigIntegerMax) > 0) {
195            log(ast, MSG_KEY, currentValue, bigIntegerMax);
196        }
197        popValue();
198    }
199
200    /**
201     * Increments the current value by a specified amount.
202     *
203     * @param amount the amount to increment by
204     */
205    private void incrementCurrentValue(BigInteger amount) {
206        currentValue = currentValue.add(amount);
207    }
208
209    /** Push the current value on the stack. */
210    private void pushValue() {
211        valueStack.push(currentValue);
212        currentValue = INITIAL_VALUE;
213    }
214
215    /**
216     * Pops a value off the stack and makes it the current value.
217     */
218    private void popValue() {
219        currentValue = valueStack.pop();
220    }
221
222    /** Process the start of the method definition. */
223    private void visitMethodDef() {
224        pushValue();
225    }
226
227}