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