View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 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   * <p>
29   * Restricts nested try-catch-finally blocks to a specified depth.
30   * </p>
31   * <ul>
32   * <li>
33   * Property {@code max} - Specify maximum allowed nesting depth.
34   * Type is {@code int}.
35   * Default value is {@code 1}.
36   * </li>
37   * </ul>
38   * <p>
39   * Parent is {@code com.puppycrawl.tools.checkstyle.TreeWalker}
40   * </p>
41   * <p>
42   * Violation Message Keys:
43   * </p>
44   * <ul>
45   * <li>
46   * {@code nested.try.depth}
47   * </li>
48   * </ul>
49   *
50   * @since 3.2
51   */
52  @FileStatefulCheck
53  public final class NestedTryDepthCheck extends AbstractCheck {
54  
55      /**
56       * A key is pointing to the warning message text in "messages.properties"
57       * file.
58       */
59      public static final String MSG_KEY = "nested.try.depth";
60  
61      /** Specify maximum allowed nesting depth. */
62      private int max = 1;
63      /** Current nesting depth. */
64      private int depth;
65  
66      /**
67       * Setter to specify maximum allowed nesting depth.
68       *
69       * @param max maximum allowed nesting depth.
70       * @since 3.2
71       */
72      public void setMax(int max) {
73          this.max = max;
74      }
75  
76      @Override
77      public int[] getDefaultTokens() {
78          return getRequiredTokens();
79      }
80  
81      @Override
82      public int[] getAcceptableTokens() {
83          return getRequiredTokens();
84      }
85  
86      @Override
87      public int[] getRequiredTokens() {
88          return new int[] {TokenTypes.LITERAL_TRY};
89      }
90  
91      @Override
92      public void visitToken(DetailAST literalTry) {
93          if (depth > max) {
94              log(literalTry, MSG_KEY, depth, max);
95          }
96          ++depth;
97      }
98  
99      @Override
100     public void leaveToken(DetailAST literalTry) {
101         --depth;
102     }
103 
104 }