View Javadoc
1   /*
2   NPathComplexity
3   max = 0
4   
5   
6   */
7   
8   package com.puppycrawl.tools.checkstyle.checks.metrics.npathcomplexity;
9   
10  public class InputNPathComplexityDefault {
11      // NP = 2
12      public void foo() { // violation
13          //NP(while-statement) = (while-range=1) + (expr=0) + 1 = 2
14          while (true) {
15              Runnable runnable = new Runnable() {
16                 // NP = 2
17                  public void run() { // violation
18                      // NP(while-statement) = (while-range=1) + (expr=0) + 1 = 2
19                      while (true) {
20                      }
21                  }
22              };
23  
24              new Thread(runnable).start();
25          }
26      }
27  
28      // NP = 10
29      public void bar() { // violation
30          // NP = (if-range=3*3) + (expr=0) + 1 = 10
31          if (System.currentTimeMillis() == 0) {
32              //NP = (if-range=1) + 1 + (expr=1) = 3
33              if (System.currentTimeMillis() == 0 && System.currentTimeMillis() == 0) {
34              }
35              //NP = (if-range=1) + 1 + (expr=1) = 3
36              if (System.currentTimeMillis() == 0 || System.currentTimeMillis() == 0) {
37              }
38          }
39      }
40  
41      // NP = 3
42      public void simpleElseIf() { // violation
43          // NP = (if-range=1) + (else-range=2) + 0 = 3
44          if (System.currentTimeMillis() == 0) {
45          // NP(else-range) = (if-range=1) + (else-range=1) + (expr=0) = 2
46          } else if (System.currentTimeMillis() == 0) {
47          } else {
48          }
49      }
50  
51      // NP = 7
52      public void stupidElseIf() { // violation
53          // NP = (if-range=1) + (else-range=3*2) + (expr=0) = 7
54          if (System.currentTimeMillis() == 0) {
55          } else {
56              // NP = (if-range=1) + (else-range=2) + (expr=0) = 3
57              if (System.currentTimeMillis() == 0) {
58              } else {
59                  // NP = (if-range=1) + 1 + (expr=0) = 2
60                  if (System.currentTimeMillis() == 0) {
61                  }
62              }
63              // NP = (if-range=1) + 1 + (expr=0) = 2
64              if (System.currentTimeMillis() == 0) {
65              }
66          }
67      }
68  
69      // NP = 3
70      public InputNPathComplexityDefault() // violation
71      {
72          int i = 1;
73          // NP = (if-range=1) + (else-range=2) + 0 = 3
74          if (System.currentTimeMillis() == 0) {
75          // NP(else-range) = (if-range=1) + (else-range=1) + (expr=0) = 2
76          } else if (System.currentTimeMillis() == 0) {
77          } else {
78          }
79      }
80  
81      // STATIC_INIT
82      // NP = 3
83      static { // violation
84          int i = 1;
85          // NP = (if-range=1) + (else-range=2) + 0 = 3
86          if (System.currentTimeMillis() == 0) {
87          // NP(else-range) = (if-range=1) + (else-range=1) + (expr=0) = 2
88          } else if (System.currentTimeMillis() == 0) {
89          } else {
90          }
91      }
92  
93      // INSTANCE_INIT
94      // NP = 3
95      { // violation
96          int i = 1;
97          // NP = (if-range=1) + (else-range=2) + 0 = 3
98          if (System.currentTimeMillis() == 0) {
99          // NP(else-range) = (if-range=1) + (else-range=1) + (expr=0) = 2
100         } else if (System.currentTimeMillis() == 0) {
101         } else {
102         }
103     }
104 
105     /** Inner */
106     // NP = 0
107     public InputNPathComplexityDefault(int aParam)
108     {
109         Runnable runnable = new Runnable() {
110             // NP = 2
111             public void run() { // violation
112                 // NP(while-statement) = (while-range=1) + (expr=0) + 1 = 2
113                 while (true) {
114                 }
115             }
116         };
117         new Thread(runnable).start();
118     }
119 
120     public void InputNestedTernaryCheck() { // violation
121         double x = (getSmth() || Math.random() == 5) ? null : (int) Math
122                 .cos(400 * (10 + 40)); // good
123         double y = (0.2 == Math.random()) ? (0.3 == Math.random()) ? null : (int) Math
124                 .cos(400 * (10 + 40)) : 6; // bad (nested in first position)
125         double z = (Integer) ((0.2 == Math.random()) ? (Integer) null + apply(null)
126                 : (0.3 == Math.random()) ? (Integer) null : (int) Math
127                         .sin(300 * (12 + 30))); // bad (nested in second
128                                                 // position)
129     }
130     public boolean getSmth() { return true; }; // violation
131     public int apply(Object o) { return 0; } // violation
132 
133     public void inClass(int type, Short s, int color) {
134         switch (type) {
135         case 3:
136             new Object() {
137                 public void anonymousMethod() { // violation
138                     {
139                         switch (s) {
140                         case 5:
141                             switch (type) {
142                             default:
143                             }
144                         }
145                     }
146                 }
147             };
148         default:
149             new Object() {
150                 class SwitchClass {
151                     { // violation
152                         switch (color) {
153                         case 5:
154                             switch (type) {
155                             default:
156                             }
157                         }
158                     }
159                 }
160             };
161         }
162     }
163 }