001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2015 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;
023
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026
027/**
028 * Checks cyclomatic complexity against a specified limit. The complexity is
029 * measured by the number of "if", "while", "do", "for", "?:", "catch",
030 * "switch", "case", "&&" and "||" statements (plus one) in the body of
031 * the member. It is a measure of the minimum number of possible paths through
032 * the source and therefore the number of required tests. Generally 1-4 is
033 * considered good, 5-7 ok, 8-10 consider re-factoring, and 11+ re-factor now!
034 *
035 * <p>Check has following properties:
036 *
037 * <p><b>switchBlockAsSingleDecisionPoint</b> - controls whether to treat the whole switch
038 * block as a single decision point. Default value is <b>false</b>
039 *
040 *
041 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
042 * @author Oliver Burn
043 * @author <a href="mailto:andreyselkin@gmail.com">Andrei Selkin</a>
044 */
045public class CyclomaticComplexityCheck
046    extends AbstractComplexityCheck {
047
048    /**
049     * A key is pointing to the warning message text in "messages.properties"
050     * file.
051     */
052    public static final String MSG_KEY = "cyclomaticComplexity";
053
054    /** Default allowed complexity. */
055    private static final int DEFAULT_VALUE = 10;
056
057    /** Whether to treat the whole switch block as a single decision point.*/
058    private boolean switchBlockAsSingleDecisionPoint;
059
060    /** Create an instance. */
061    public CyclomaticComplexityCheck() {
062        super(DEFAULT_VALUE);
063    }
064
065    /**
066     * Sets whether to treat the whole switch block as a single decision point.
067     * @param switchBlockAsSingleDecisionPoint whether to treat the whole switch
068     *                                          block as a single decision point.
069     */
070    public void setSwitchBlockAsSingleDecisionPoint(boolean switchBlockAsSingleDecisionPoint) {
071        this.switchBlockAsSingleDecisionPoint = switchBlockAsSingleDecisionPoint;
072    }
073
074    @Override
075    public int[] getDefaultTokens() {
076        return new int[] {
077            TokenTypes.CTOR_DEF,
078            TokenTypes.METHOD_DEF,
079            TokenTypes.INSTANCE_INIT,
080            TokenTypes.STATIC_INIT,
081            TokenTypes.LITERAL_WHILE,
082            TokenTypes.LITERAL_DO,
083            TokenTypes.LITERAL_FOR,
084            TokenTypes.LITERAL_IF,
085            TokenTypes.LITERAL_SWITCH,
086            TokenTypes.LITERAL_CASE,
087            TokenTypes.LITERAL_CATCH,
088            TokenTypes.QUESTION,
089            TokenTypes.LAND,
090            TokenTypes.LOR,
091        };
092    }
093
094    @Override
095    public int[] getAcceptableTokens() {
096        return new int[] {
097            TokenTypes.CTOR_DEF,
098            TokenTypes.METHOD_DEF,
099            TokenTypes.INSTANCE_INIT,
100            TokenTypes.STATIC_INIT,
101            TokenTypes.LITERAL_WHILE,
102            TokenTypes.LITERAL_DO,
103            TokenTypes.LITERAL_FOR,
104            TokenTypes.LITERAL_IF,
105            TokenTypes.LITERAL_SWITCH,
106            TokenTypes.LITERAL_CASE,
107            TokenTypes.LITERAL_CATCH,
108            TokenTypes.QUESTION,
109            TokenTypes.LAND,
110            TokenTypes.LOR,
111        };
112    }
113
114    @Override
115    protected final void visitTokenHook(DetailAST ast) {
116        if (switchBlockAsSingleDecisionPoint) {
117            if (ast.getType() != TokenTypes.LITERAL_CASE) {
118                incrementCurrentValue(BigInteger.ONE);
119            }
120        }
121        else if (ast.getType() != TokenTypes.LITERAL_SWITCH) {
122            incrementCurrentValue(BigInteger.ONE);
123        }
124    }
125
126    @Override
127    protected final String getMessageID() {
128        return MSG_KEY;
129    }
130}