001////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code for adherence to a set of rules.
003// Copyright (C) 2001-2020 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.
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     *
072     * @param max maximum allowed nesting depth.
073     */
074    public void setMax(int max) {
075        this.max = max;
076    }
077
078    @Override
079    public int[] getDefaultTokens() {
080        return getRequiredTokens();
081    }
082
083    @Override
084    public int[] getAcceptableTokens() {
085        return getRequiredTokens();
086    }
087
088    @Override
089    public int[] getRequiredTokens() {
090        return new int[] {TokenTypes.LITERAL_IF};
091    }
092
093    @Override
094    public void beginTree(DetailAST rootAST) {
095        depth = 0;
096    }
097
098    @Override
099    public void visitToken(DetailAST literalIf) {
100        if (!CheckUtil.isElseIf(literalIf)) {
101            if (depth > max) {
102                log(literalIf, MSG_KEY, depth, max);
103            }
104            ++depth;
105        }
106    }
107
108    @Override
109    public void leaveToken(DetailAST literalIf) {
110        if (!CheckUtil.isElseIf(literalIf)) {
111            --depth;
112        }
113    }
114
115}