View Javadoc
1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2017 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   * Finds nested blocks.
29   *
30   * <p>
31   * For example this Check flags confusing code like
32   * </p>
33   * <pre>
34   * public void guessTheOutput()
35   * {
36   *     int whichIsWhich = 0;
37   *     {
38   *         int whichIsWhich = 2;
39   *     }
40   *     System.out.println("value = " + whichIsWhich);
41   * }
42   * </pre>
43   * and debugging / refactoring leftovers such as
44   *
45   * <pre>
46   * // if (someOldCondition)
47   * {
48   *     System.out.println("unconditional");
49   * }
50   * </pre>
51   *
52   * <p>
53   * A case in a switch statement does not implicitly form a block.
54   * Thus to be able to introduce local variables that have case scope
55   * it is necessary to open a nested block. This is supported, set
56   * the allowInSwitchCase property to true and include all statements
57   * of the case in the block.
58   * </p>
59   *
60   * <pre>
61   * switch (a)
62   * {
63   *     case 0:
64   *         // Never OK, break outside block
65   *         {
66   *             x = 1;
67   *         }
68   *         break;
69   *     case 1:
70   *         // Never OK, statement outside block
71   *         System.out.println("Hello");
72   *         {
73   *             x = 2;
74   *             break;
75   *         }
76   *     case 1:
77   *         // OK if allowInSwitchCase is true
78   *         {
79   *             System.out.println("Hello");
80   *             x = 2;
81   *             break;
82   *         }
83   * }
84   * </pre>
85   *
86   * @author lkuehne
87   */
88  @StatelessCheck
89  public class AvoidNestedBlocksCheck extends AbstractCheck {
90      /**
91       * A key is pointing to the warning message text in "messages.properties"
92       * file.
93       */
94      public static final String MSG_KEY_BLOCK_NESTED = "block.nested";
95  
96      /**
97       * Whether nested blocks are allowed if they are the
98       * only child of a switch case.
99       */
100     private boolean allowInSwitchCase;
101 
102     @Override
103     public int[] getDefaultTokens() {
104         return getRequiredTokens();
105     }
106 
107     @Override
108     public int[] getAcceptableTokens() {
109         return getRequiredTokens();
110     }
111 
112     @Override
113     public int[] getRequiredTokens() {
114         return new int[] {TokenTypes.SLIST};
115     }
116 
117     @Override
118     public void visitToken(DetailAST ast) {
119         final DetailAST parent = ast.getParent();
120         if (parent.getType() == TokenTypes.SLIST
121                 && (!allowInSwitchCase
122                     || parent.getParent().getType() != TokenTypes.CASE_GROUP
123                     || parent.getNumberOfChildren() != 1)) {
124             log(ast.getLineNo(), ast.getColumnNo(), MSG_KEY_BLOCK_NESTED);
125         }
126     }
127 
128     /**
129      * Setter for allowInSwitchCase property.
130      * @param allowInSwitchCase whether nested blocks are allowed
131      *                 if they are the only child of a switch case.
132      */
133     public void setAllowInSwitchCase(boolean allowInSwitchCase) {
134         this.allowInSwitchCase = allowInSwitchCase;
135     }
136 }