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>Valid code example:</p>
046 * <pre>
047 * if (true) {
048 *     if (true) {} // OK
049 *     else {}
050 * }
051 * </pre>
052 * <p>Invalid code example:</p>
053 * <pre>
054 * if (true) {
055 *     if (true) {
056 *         if (true) { // violation, nested if-else depth is 2 (max allowed is 1)
057 *         }
058 *     }
059 * }
060 * </pre>
061 * <p>
062 * To configure the check to allow nesting depth 3:
063 * </p>
064 * <pre>
065 * &lt;module name=&quot;NestedIfDepth&quot;&gt;
066 *   &lt;property name=&quot;max&quot; value=&quot;3&quot;/&gt;
067 * &lt;/module&gt;
068 * </pre>
069 * <p>Valid code example:</p>
070 * <pre>
071 * if (true) {
072 *    if (true) {
073 *       if (true) {
074 *          if (true) {} // OK
075 *          else {}
076 *       }
077 *    }
078 * }
079 * </pre>
080 * <p>Invalid code example:</p>
081 * <pre>
082 * if (true) {
083 *    if (true) {
084 *       if (true) {
085 *          if (true) {
086 *             if (true) { // violation, nested if-else depth is 4 (max allowed is 3)
087 *                if (true) {} // violation, nested if-else depth is 5 (max allowed is 3)
088 *                else {}
089 *             }
090 *          }
091 *       }
092 *    }
093 * }
094 * </pre>
095 * <p>
096 * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
097 * </p>
098 * <p>
099 * Violation Message Keys:
100 * </p>
101 * <ul>
102 * <li>
103 * {@code nested.if.depth}
104 * </li>
105 * </ul>
106 *
107 * @since 3.2
108 */
109@FileStatefulCheck
110public final class NestedIfDepthCheck extends AbstractCheck {
111
112    /**
113     * A key is pointing to the warning message text in "messages.properties"
114     * file.
115     */
116    public static final String MSG_KEY = "nested.if.depth";
117
118    /** Specify maximum allowed nesting depth. */
119    private int max = 1;
120    /** Current nesting depth. */
121    private int depth;
122
123    /**
124     * Setter to specify maximum allowed nesting depth.
125     *
126     * @param max maximum allowed nesting depth.
127     */
128    public void setMax(int max) {
129        this.max = max;
130    }
131
132    @Override
133    public int[] getDefaultTokens() {
134        return getRequiredTokens();
135    }
136
137    @Override
138    public int[] getAcceptableTokens() {
139        return getRequiredTokens();
140    }
141
142    @Override
143    public int[] getRequiredTokens() {
144        return new int[] {TokenTypes.LITERAL_IF};
145    }
146
147    @Override
148    public void beginTree(DetailAST rootAST) {
149        depth = 0;
150    }
151
152    @Override
153    public void visitToken(DetailAST literalIf) {
154        if (!CheckUtil.isElseIf(literalIf)) {
155            if (depth > max) {
156                log(literalIf, MSG_KEY, depth, max);
157            }
158            ++depth;
159        }
160    }
161
162    @Override
163    public void leaveToken(DetailAST literalIf) {
164        if (!CheckUtil.isElseIf(literalIf)) {
165            --depth;
166        }
167    }
168
169}