View Javadoc
1   /*xml
2   <module name="Checker">
3     <module name="TreeWalker">
4       <module name="DescendantToken">
5         <property name="tokens" value="LITERAL_SWITCH"/>
6         <property name="limitedTokens" value="LITERAL_CASE"/>
7         <property name="maximumDepth" value="2"/>
8         <property name="maximumNumber" value="1"/>
9       </module>
10    </module>
11  </module>
12  */
13  package com.puppycrawl.tools.checkstyle.checks.descendanttoken;
14  
15  // xdoc section -- start
16  class Example2 {
17    void testMethod1() {
18      int x = 1;
19      switch (x) {
20        case 1:
21          System.out.println("hi");
22          break;
23        default:
24          System.out.println("Default");
25          break;
26      }
27  
28      int y = 1;
29      switch (y) {
30        case 1:
31          System.out.println("hi");
32          break;
33      }
34    }
35  
36    void testMethod2() {
37      int x = 1;
38      switch (x) {
39        case 1:
40          // Some code
41          break;
42        default:
43          // Some code
44          break;
45      }
46  
47      switch (x) { // violation, 'Count of 2 for 'LITERAL_SWITCH' descendant'
48        case 1:
49          // Some code
50          break;
51        case 2:
52          // Some code
53          break;
54        default:
55          // Some code
56          break;
57      }
58    }
59  
60    void testMethod3() {
61      int x = 1;
62      int y = 2;
63      switch (x) {
64        case 1:
65          System.out.println("xyz");
66          break;
67      }
68      switch (y) {
69        case 1:
70          switch (y) {
71            case 2:
72              System.out.println("xyz");
73              break;
74          }
75          break;
76      }
77    }
78  }
79  // xdoc section -- end