View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2025 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.blocks;
21  
22  import com.puppycrawl.tools.checkstyle.StatelessCheck;
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   * <div>
29   * Finds nested blocks (blocks that are used freely in the code).
30   * </div>
31   *
32   * <p>
33   * Rationale: Nested blocks are often leftovers from the
34   * debugging process, they confuse the reader.
35   * </p>
36   *
37   * <p>
38   * For example, this check finds the obsolete braces in
39   * </p>
40   * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
41   * public void guessTheOutput()
42   * {
43   *   int whichIsWhich = 0;
44   *   {
45   *     whichIsWhich = 2;
46   *   }
47   *   System.out.println("value = " + whichIsWhich);
48   * }
49   * </code></pre></div>
50   *
51   * <p>
52   * and debugging / refactoring leftovers such as
53   * </p>
54   * <div class="wrapper"><pre class="prettyprint"><code class="language-java">
55   * // if (conditionThatIsNotUsedAnyLonger)
56   * {
57   *   System.out.println("unconditional");
58   * }
59   * </code></pre></div>
60   *
61   * <p>
62   * A case in a switch statement does not implicitly form a block.
63   * Thus, to be able to introduce local variables that have case scope
64   * it is necessary to open a nested block. This is supported, set
65   * the allowInSwitchCase property to true and include all statements
66   * of the case in the block.
67   * </p>
68   *
69   * @since 3.1
70   */
71  @StatelessCheck
72  public class AvoidNestedBlocksCheck extends AbstractCheck {
73  
74      /**
75       * A key is pointing to the warning message text in "messages.properties"
76       * file.
77       */
78      public static final String MSG_KEY_BLOCK_NESTED = "block.nested";
79  
80      /**
81       * Allow nested blocks if they are the only child of a switch case.
82       */
83      private boolean allowInSwitchCase;
84  
85      @Override
86      public int[] getDefaultTokens() {
87          return getRequiredTokens();
88      }
89  
90      @Override
91      public int[] getAcceptableTokens() {
92          return getRequiredTokens();
93      }
94  
95      @Override
96      public int[] getRequiredTokens() {
97          return new int[] {TokenTypes.SLIST};
98      }
99  
100     @Override
101     public void visitToken(DetailAST ast) {
102         final DetailAST parent = ast.getParent();
103         if (parent.getType() == TokenTypes.SLIST
104                 && (!allowInSwitchCase || hasSiblings(ast))) {
105             log(ast, MSG_KEY_BLOCK_NESTED);
106         }
107     }
108 
109     /**
110      * Checks whether the AST node has any siblings or not.
111      *
112      * @param ast node to examine
113      * @return {@code true} if the node has one or more siblings
114      */
115     private static boolean hasSiblings(DetailAST ast) {
116         return ast.getPreviousSibling() != null || ast.getNextSibling() != null;
117     }
118 
119     /**
120      * Setter to allow nested blocks if they are the only child of a switch case.
121      *
122      * @param allowInSwitchCase whether nested blocks are allowed
123      *                 if they are the only child of a switch case.
124      * @since 3.2
125      */
126     public void setAllowInSwitchCase(boolean allowInSwitchCase) {
127         this.allowInSwitchCase = allowInSwitchCase;
128     }
129 
130 }