001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2019 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.coding;
021
022import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
023import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
024import com.puppycrawl.tools.checkstyle.api.DetailAST;
025import com.puppycrawl.tools.checkstyle.api.TokenTypes;
026import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
027
028/**
029 * <p>
030 * Restricts nested if-else blocks to a specified depth (default = 1).
031 * </p>
032 * <ul>
033 * <li>
034 * Property {@code max} - Specify maximum allowed nesting depth.
035 * Default value is {@code 1}.
036 * </li>
037 * </ul>
038 * <p>
039 * To configure the check:
040 * </p>
041 * <pre>
042 * &lt;module name=&quot;NestedIfDepth&quot;/&gt;
043 * </pre>
044 * <p>
045 * To configure the check to allow nesting depth 3:
046 * </p>
047 * <pre>
048 * &lt;module name=&quot;NestedIfDepth&quot;&gt;
049 *   &lt;property name=&quot;max&quot; value=&quot;3&quot;/&gt;
050 * &lt;/module&gt;
051 * </pre>
052 *
053 * @since 3.2
054 */
055@FileStatefulCheck
056public final class NestedIfDepthCheck extends AbstractCheck {
057
058    /**
059     * A key is pointing to the warning message text in "messages.properties"
060     * file.
061     */
062    public static final String MSG_KEY = "nested.if.depth";
063
064    /** Specify maximum allowed nesting depth. */
065    private int max = 1;
066    /** Current nesting depth. */
067    private int depth;
068
069    /**
070     * Setter to specify maximum allowed nesting depth.
071     * @param max maximum allowed nesting depth.
072     */
073    public void setMax(int max) {
074        this.max = max;
075    }
076
077    @Override
078    public int[] getDefaultTokens() {
079        return getRequiredTokens();
080    }
081
082    @Override
083    public int[] getAcceptableTokens() {
084        return getRequiredTokens();
085    }
086
087    @Override
088    public int[] getRequiredTokens() {
089        return new int[] {TokenTypes.LITERAL_IF};
090    }
091
092    @Override
093    public void beginTree(DetailAST rootAST) {
094        depth = 0;
095    }
096
097    @Override
098    public void visitToken(DetailAST literalIf) {
099        if (!CheckUtil.isElseIf(literalIf)) {
100            if (depth > max) {
101                log(literalIf, MSG_KEY, depth, max);
102            }
103            ++depth;
104        }
105    }
106
107    @Override
108    public void leaveToken(DetailAST literalIf) {
109        if (!CheckUtil.isElseIf(literalIf)) {
110            --depth;
111        }
112    }
113
114}