View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2018 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.coding;
21  
22  import com.puppycrawl.tools.checkstyle.FileStatefulCheck;
23  import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
24  import com.puppycrawl.tools.checkstyle.api.DetailAST;
25  import com.puppycrawl.tools.checkstyle.api.TokenTypes;
26  
27  /**
28   * Check the number of nested {@code for} -statements. The maximum
29   * number of nested layers can be configured. The default value is 1.
30   * The code for the class is copied from the NestedIfDepthCheck-class.
31   * The only difference is the intercepted token (for instead of if).
32   * Example:
33   * <pre>
34   *  &lt;!-- Restricts nested for blocks to a specified depth (default = 1).
35   *                                                                        --&gt;
36   *  &lt;module name=&quot;com.puppycrawl.tools.checkstyle.checks.coding
37   *                                            .CatchWithLostStackCheck&quot;&gt;
38   *    &lt;property name=&quot;severity&quot; value=&quot;info&quot;/&gt;
39   *    &lt;property name=&quot;max&quot; value=&quot;1&quot;/&gt;
40   *  &lt;/module&gt;
41   * </pre>
42   * @see NestedIfDepthCheck
43   */
44  @FileStatefulCheck
45  public final class NestedForDepthCheck extends AbstractCheck {
46  
47      /**
48       * A key is pointing to the warning message text in "messages.properties"
49       * file.
50       */
51      public static final String MSG_KEY = "nested.for.depth";
52  
53      /** Maximum allowed nesting depth. */
54      private int max = 1;
55      /** Current nesting depth. */
56      private int depth;
57  
58      /**
59       * Setter for maximum allowed nesting depth.
60       * @param max maximum allowed nesting depth.
61       */
62      public void setMax(int max) {
63          this.max = max;
64      }
65  
66      @Override
67      public int[] getDefaultTokens() {
68          return getRequiredTokens();
69      }
70  
71      @Override
72      public int[] getAcceptableTokens() {
73          return getRequiredTokens();
74      }
75  
76      @Override
77      public int[] getRequiredTokens() {
78          return new int[] {TokenTypes.LITERAL_FOR};
79      }
80  
81      @Override
82      public void beginTree(DetailAST rootAST) {
83          depth = 0;
84      }
85  
86      @Override
87      public void visitToken(DetailAST ast) {
88          if (depth > max) {
89              log(ast, MSG_KEY, depth, max);
90          }
91          ++depth;
92      }
93  
94      @Override
95      public void leaveToken(DetailAST ast) {
96          --depth;
97      }
98  
99  }