View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2026 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   * {@snippet :
41   * public void guessTheOutput()
42   * {
43   *   int whichIsWhich = 0;
44   *   {
45   *     whichIsWhich = 2;
46   *   }
47   *   System.out.println("value = " + whichIsWhich);
48   * }
49   * }
50   *
51   * <p>
52   * and debugging / refactoring leftovers such as
53   * </p>
54   * {@snippet :
55   * // if (conditionThatIsNotUsedAnyLonger)
56   * {
57   *   System.out.println("unconditional");
58   * }
59   * }
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      /**
86       * Creates a new {@code AvoidNestedBlocksCheck} instance.
87       */
88      public AvoidNestedBlocksCheck() {
89          // no code by default
90      }
91  
92      @Override
93      public int[] getDefaultTokens() {
94          return getRequiredTokens();
95      }
96  
97      @Override
98      public int[] getAcceptableTokens() {
99          return getRequiredTokens();
100     }
101 
102     @Override
103     public int[] getRequiredTokens() {
104         return new int[] {TokenTypes.SLIST};
105     }
106 
107     @Override
108     public void visitToken(DetailAST ast) {
109         final DetailAST parent = ast.getParent();
110         if (parent.getType() == TokenTypes.SLIST
111                 && (!allowInSwitchCase || hasSiblings(ast))) {
112             log(ast, MSG_KEY_BLOCK_NESTED);
113         }
114     }
115 
116     /**
117      * Checks whether the AST node has any siblings or not.
118      *
119      * @param ast node to examine
120      * @return {@code true} if the node has one or more siblings
121      */
122     private static boolean hasSiblings(DetailAST ast) {
123         return ast.getPreviousSibling() != null || ast.getNextSibling() != null;
124     }
125 
126     /**
127      * Setter to allow nested blocks if they are the only child of a switch case.
128      *
129      * @param allowInSwitchCase whether nested blocks are allowed
130      *                 if they are the only child of a switch case.
131      * @since 3.2
132      */
133     public void setAllowInSwitchCase(boolean allowInSwitchCase) {
134         this.allowInSwitchCase = allowInSwitchCase;
135     }
136 
137 }