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 * Type is {@code int}.
036 * Default value is {@code 1}.
037 * </li>
038 * </ul>
039 * <p>
040 * To configure the check:
041 * </p>
042 * <pre>
043 * &lt;module name=&quot;NestedIfDepth&quot;/&gt;
044 * </pre>
045 * <p>
046 * To configure the check to allow nesting depth 3:
047 * </p>
048 * <pre>
049 * &lt;module name=&quot;NestedIfDepth&quot;&gt;
050 *   &lt;property name=&quot;max&quot; value=&quot;3&quot;/&gt;
051 * &lt;/module&gt;
052 * </pre>
053 * <p>
054 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
055 * </p>
056 * <p>
057 * Violation Message Keys:
058 * </p>
059 * <ul>
060 * <li>
061 * {@code nested.if.depth}
062 * </li>
063 * </ul>
064 *
065 * @since 3.2
066 */
067@FileStatefulCheck
068public final class NestedIfDepthCheck extends AbstractCheck {
069
070    /**
071     * A key is pointing to the warning message text in "messages.properties"
072     * file.
073     */
074    public static final String MSG_KEY = "nested.if.depth";
075
076    /** Specify maximum allowed nesting depth. */
077    private int max = 1;
078    /** Current nesting depth. */
079    private int depth;
080
081    /**
082     * Setter to specify maximum allowed nesting depth.
083     *
084     * @param max maximum allowed nesting depth.
085     */
086    public void setMax(int max) {
087        this.max = max;
088    }
089
090    @Override
091    public int[] getDefaultTokens() {
092        return getRequiredTokens();
093    }
094
095    @Override
096    public int[] getAcceptableTokens() {
097        return getRequiredTokens();
098    }
099
100    @Override
101    public int[] getRequiredTokens() {
102        return new int[] {TokenTypes.LITERAL_IF};
103    }
104
105    @Override
106    public void beginTree(DetailAST rootAST) {
107        depth = 0;
108    }
109
110    @Override
111    public void visitToken(DetailAST literalIf) {
112        if (!CheckUtil.isElseIf(literalIf)) {
113            if (depth > max) {
114                log(literalIf, MSG_KEY, depth, max);
115            }
116            ++depth;
117        }
118    }
119
120    @Override
121    public void leaveToken(DetailAST literalIf) {
122        if (!CheckUtil.isElseIf(literalIf)) {
123            --depth;
124        }
125    }
126
127}