001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2014  Oliver Burn
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////////////////////////////////////////////////////////////////////////////////
019package com.puppycrawl.tools.checkstyle.checks.metrics;
020
021import java.math.BigInteger;
022
023import com.puppycrawl.tools.checkstyle.api.DetailAST;
024import com.puppycrawl.tools.checkstyle.api.TokenTypes;
025
026/**
027 * Checks the npath complexity against a specified limit (default = 200).
028 * The npath metric computes the number of possible execution paths
029 * through a function. Similar to the cyclomatic complexity but also
030 * takes into account the nesting of conditional statements and
031 * multi-part boolean expressions.
032 *
033 * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris</a>
034 * @author o_sukhodolsky
035 * TODO: For every or: _value += (_orCount * (nestedValue - 1));
036 * TODO: For every and: ???
037 */
038public final class NPathComplexityCheck extends AbstractComplexityCheck
039{
040    /** Default allowed complexity. */
041    private static final int DEFAULT_MAX = 200;
042
043    /** Creates new instance of the check. */
044    public NPathComplexityCheck()
045    {
046        super(DEFAULT_MAX);
047    }
048
049    @Override
050    public int[] getDefaultTokens()
051    {
052        return new int[] {
053            TokenTypes.CTOR_DEF,
054            TokenTypes.METHOD_DEF,
055            TokenTypes.STATIC_INIT,
056            TokenTypes.INSTANCE_INIT,
057            TokenTypes.LITERAL_WHILE,
058            TokenTypes.LITERAL_DO,
059            TokenTypes.LITERAL_FOR,
060            TokenTypes.LITERAL_IF,
061            TokenTypes.LITERAL_ELSE,
062            TokenTypes.LITERAL_SWITCH,
063            TokenTypes.LITERAL_CASE,
064            TokenTypes.LITERAL_TRY,
065            TokenTypes.LITERAL_CATCH,
066            TokenTypes.QUESTION,
067        };
068    }
069
070    @Override
071    public void visitToken(DetailAST ast)
072    {
073        switch (ast.getType()) {
074            case TokenTypes.LITERAL_WHILE:
075            case TokenTypes.LITERAL_DO:
076            case TokenTypes.LITERAL_FOR:
077            case TokenTypes.LITERAL_IF:
078            case TokenTypes.QUESTION:
079            case TokenTypes.LITERAL_TRY:
080            case TokenTypes.LITERAL_SWITCH:
081                visitMultiplyingConditional();
082                break;
083            case TokenTypes.LITERAL_ELSE:
084            case TokenTypes.LITERAL_CATCH:
085            case TokenTypes.LITERAL_CASE:
086                visitAddingConditional();
087                break;
088            default:
089                super.visitToken(ast);
090        }
091    }
092
093    @Override
094    public void leaveToken(DetailAST ast)
095    {
096        switch (ast.getType()) {
097            case TokenTypes.LITERAL_WHILE:
098            case TokenTypes.LITERAL_DO:
099            case TokenTypes.LITERAL_FOR:
100            case TokenTypes.LITERAL_IF:
101            case TokenTypes.QUESTION:
102            case TokenTypes.LITERAL_TRY:
103            case TokenTypes.LITERAL_SWITCH:
104                leaveMultiplyingConditional();
105                break;
106            case TokenTypes.LITERAL_ELSE:
107            case TokenTypes.LITERAL_CATCH:
108            case TokenTypes.LITERAL_CASE:
109                leaveAddingConditional();
110                break;
111            default:
112                super.leaveToken(ast);
113        }
114    }
115
116    @Override
117    protected String getMessageID()
118    {
119        return "npathComplexity";
120    }
121
122    /** Visits else, catch or case. */
123    private void visitAddingConditional()
124    {
125        pushValue();
126    }
127
128    /** Leaves else, catch or case. */
129    private void leaveAddingConditional()
130    {
131        setCurrentValue(
132                getCurrentValue().subtract(BigInteger.ONE).add(popValue()));
133    }
134
135    /** Visits while, do, for, if, try, ? (in ?::) or switch. */
136    private void visitMultiplyingConditional()
137    {
138        pushValue();
139    }
140
141    /** Leaves while, do, for, if, try, ? (in ?::) or switch. */
142    private void leaveMultiplyingConditional()
143    {
144        setCurrentValue(
145                getCurrentValue().add(BigInteger.ONE).multiply(popValue()));
146    }
147}