View Javadoc
1   package com.google.checkstyle.test.chapter4formatting.rule4822variabledistance;
2   import java.util.*;
3   public class InputVariableDeclarationUsageDistanceCheck {
4   
5       private static int test1 = 0;
6   
7       static {
8           int b = 0;
9           int d = 0;
10          {
11              d = ++b;
12          }
13      }
14  
15      static {
16          int c = 0;
17          int a = 3;
18          int b = 2;
19          {
20              a = a + b;
21              c = b;
22          }
23          {
24              c--;
25          }
26          a = 7;
27      }
28  
29      static {
30          int a = -1;
31          int b = 2;
32          b++;
33          int c = --b;
34          a = b; // DECLARATION OF VARIABLE 'a' SHOULD BE HERE (distance = 2)
35      }
36  
37      public InputVariableDeclarationUsageDistanceCheck(int test1) {
38          int temp = -1;
39          this.test1 = test1;
40          temp = test1; // DECLARATION OF VARIABLE 'temp' SHOULD BE HERE (distance = 2)
41      }
42  
43      public boolean testMethod() {
44          int temp = 7;
45          new InputVariableDeclarationUsageDistanceCheck(2);
46          String.valueOf(temp); // DECLARATION OF VARIABLE 'temp' SHOULD BE HERE (distance = 2)
47          boolean result = false;
48          String str = "";
49          if (test1 > 1) {
50              str = "123";
51              result = true;
52          }
53          return result;
54      }
55  
56      public void testMethod2() {
57          int count;
58          int a = 3;
59          int b = 2;
60          {
61              a = a
62                      + b
63                      - 5
64                      + 2
65                      * a;
66              count = b; // DECLARATION OF VARIABLE 'count' SHOULD BE HERE (distance = 2)
67          }
68      }
69  
70      public void testMethod3() {
71          int count; //warn
72          int a = 3;
73          int b = 3;
74          a = a + b;
75          b = a + a;
76          testMethod2();
77          count = b; // DECLARATION OF VARIABLE 'count' SHOULD BE HERE (distance = 4)
78      }
79  
80      public void testMethod4(int arg) {
81          int d = 0;
82          for (int i = 0; i < 10; i++) {
83              d++;
84              if (i > 5) {
85                  d += arg;
86              }
87          }
88  
89          String ar[] = { "1", "2" };
90          for (String st : ar) {
91              System.identityHashCode(st);
92          }
93      }
94  
95      public void testMethod5() {
96          int arg = 7;
97          boolean b = true;
98          boolean bb = false;
99          if (b)
100             if (!bb)
101                 b = false;
102         testMethod4(arg); // DECLARATION OF VARIABLE 'arg' SHOULD BE HERE (distance = 2)
103     }
104 
105     public void testMethod6() {
106         int blockNumWithSimilarVar = 3;
107         int dist = 0;
108         int index = 0;
109         int block = 0;
110 
111         if (blockNumWithSimilarVar <= 1) {
112             do {
113                 dist++;
114                 if (block > 4) {
115                     break;
116                 }
117                 index++;
118                 block++;
119             } while (index < 7);
120         } else {
121             while (index < 8) {
122                 dist += block;
123                 index++;
124                 block++;
125             }
126         }
127     }
128 
129     public boolean testMethod7(int a) {
130         boolean res;
131         switch (a) {
132         case 1:
133             res = true;
134             break;
135         default:
136             res = false;
137         }
138         return res;
139     }
140 
141     public void testMethod8() {
142         int b = 0;
143         int c = 0;
144         int m = 0;
145         int n = 0;
146         {
147             c++;
148             b++;
149         }
150         {
151             n++; // DECLARATION OF VARIABLE 'n' SHOULD BE HERE (distance = 2)
152             m++; // DECLARATION OF VARIABLE 'm' SHOULD BE HERE (distance = 3)
153             b++;
154         }
155     }
156 
157     public void testMethod9() {
158         boolean result = false;
159         boolean b1 = true;
160         boolean b2 = false;
161         if (b1) {
162             if (!b2) {
163                 result = true;
164             }
165             result = true;
166         }
167     }
168 
169     public boolean testMethod10() {
170         boolean result;
171         try {
172             result = true;
173         } catch (Exception e) {
174             result = false;
175         } finally {
176             result = false;
177         }
178         return result;
179     }
180 
181     public void testMethod11() {
182         int a = 0;
183         int b = 10;
184         boolean result;
185         try {
186             b--;
187         } catch (Exception e) {
188             b++;
189             result = false; // DECLARATION OF VARIABLE 'result' SHOULD BE HERE (distance = 2)
190         } finally {
191             a++;
192         }
193     }
194 
195     public void testMethod12() {
196         boolean result = false;
197         boolean b3 = true;
198         boolean b1 = true;
199         boolean b2 = false;
200         if (b1) {
201             if (b3) {
202                 if (!b2) {
203                     result = true;
204                 }
205                 result = true;
206             }
207         }
208     }
209 
210     public void testMethod13() {
211         int i = 9;
212         int j = 6;
213         int g = i + 8;
214         int k = j + 10;
215     }
216 
217     public void testMethod14() {
218         Session s = openSession();
219         Transaction t = s.beginTransaction(); //warn
220         A a = new A();
221         E d1 = new E();
222         C1 c = new C1();
223         E d2 = new E();
224         a.setForward(d1);
225         d1.setReverse(a);
226         c.setForward(d2); // DECLARATION OF VARIABLE 'c' SHOULD BE HERE (distance = 3)
227                             // DECLARATION OF VARIABLE 'd2' SHOULD BE HERE (distance = 3)
228         d2.setReverse(c);
229         Serializable aid = s.save(a);
230         Serializable d2id = s.save(d2);
231         t.commit(); // DECLARATION OF VARIABLE 't' SHOULD BE HERE (distance = 5)
232         s.close();
233     }
234 
235     public boolean isCheckBoxEnabled(int path) {
236         String model = "";
237         if (true) {
238             for (int index = 0; index < path; ++index) {
239                 int nodeIndex = model.codePointAt(path);
240                 if (model.contains("")) {
241                     return false;
242                 }
243             }
244         } else {
245             int nodeIndex = model.codePointAt(path);
246             if (model.contains("")) {
247                 return false;
248             }
249         }
250         return true;
251     }
252 
253     public Object readObject(String in) throws Exception {
254         String startDay = new String("");
255         String endDay = new String("");
256         return new String(startDay + endDay);
257     }
258 
259     public int[] getSelectedIndices() {
260         int[] sel = new int[5];
261         String model = "";
262         int a = 0;
263         a++;
264         for (int index = 0; index < 5; ++index) {
265             sel[index] = Integer.parseInt(model.valueOf(a)); // DECLARATION OF VARIABLE 'sel'
266                                                              // SHOULD BE HERE (distance = 2)
267                                                              // DECLARATION OF VARIABLE 'model'
268                                                              // SHOULD BE HERE (distance = 2)
269         }
270         return sel;
271     }
272 
273     public void testMethod15() {
274         String confDebug = "";
275         if (!confDebug.equals("") && !confDebug.equals("null")) {
276             LogLog.warn("The \"" + "\" attribute is deprecated.");
277             LogLog.warn("Use the \"" + "\" attribute instead.");
278             LogLog.setInternalDebugging(confDebug, true);
279         }
280 
281         int i = 0;
282         int k = 7;
283         boolean b = false;
284         for (; i < k; i++) {
285             b = true;
286             k++;
287         }
288 
289         int sw;
290         switch (i) {
291         case 0:
292             k++;
293             sw = 0; // DECLARATION OF VARIABLE 'sw' SHOULD BE HERE (distance = 2)
294             break;
295         case 1:
296             b = false;
297             break;
298         default:
299             b = true;
300         }
301 
302         int wh = 0;
303         b = true;
304         do {
305             k--;
306             i++;
307         } while (wh > 0); // DECLARATION OF VARIABLE 'wh' SHOULD BE HERE (distance = 2)
308 
309         if (wh > 0) {
310             k++;
311         } else if (!b) {
312             i++;
313         } else {
314             i--;
315         }
316     }
317 
318     public void testMethod16() {
319         int wh = 1, i = 4, k = 0;
320         if (i > 0) {
321             k++;
322         } else if (wh > 0) {
323             i++;
324         } else {
325             i--;
326         }
327     }
328 
329     protected JMenuItem createSubMenuItem(LogLevel level) {
330         final JMenuItem result = new JMenuItem(level.toString());
331         final LogLevel logLevel = level;
332         result.setMnemonic(level.toString().charAt(0));
333         result.addActionListener(new ActionListener() {
334           public void actionPerformed(ActionEvent e) {
335             showLogLevelColorChangeDialog(result, logLevel); // DECLARATION OF VARIABLE 'logLevel'
336                                                              // SHOULD BE HERE (distance = 2)
337           }
338         });
339 
340         return result;
341 
342       }
343 
344     public static Color darker(Color color, double fraction) {
345         int red = (int) Math.round(color.getRed() * (1.0 - fraction));
346         int green = (int) Math.round(color.getGreen() * (1.0 - fraction));
347         int blue = (int) Math.round(color.getBlue() * (1.0 - fraction));
348 
349         if (red < 0) {
350             red = 0;
351         } else if (red > 255) {
352             red = 255;
353         }
354         if (green < 0) { // DECLARATION OF VARIABLE 'green' SHOULD BE HERE (distance = 2)
355             green = 0;
356         } else if (green > 255) {
357             green = 255;
358         }
359         if (blue < 0) { // DECLARATION OF VARIABLE 'blue' SHOULD BE HERE (distance = 3)
360             // blue = 0;
361         }
362 
363         int alpha = color.getAlpha();
364 
365         return new Color(red, green, blue, alpha);
366     }
367 
368     public void testFinal() {
369         AuthUpdateTask task = null;
370         final long intervalMs = 30 * 60000L;
371         Object authCheckUrl = null, authInfo = null;
372         task = new AuthUpdateTask(authCheckUrl, authInfo, new IAuthListener() {
373             @Override
374             public void authTokenChanged(String cookie, String token) {
375                 fireAuthTokenChanged(cookie, token);
376             }
377         });
378 
379         Timer timer = new Timer("Auth Guard", true);
380         timer.schedule(task, intervalMs / 2, intervalMs); // DECLARATION OF VARIABLE 'intervalMs'
381                                                           // SHOULD BE HERE (distance = 2)
382     }
383 
384     public void testForCycle() {
385         int filterCount = 0;
386         for (int i = 0; i < 10; i++, filterCount++) {
387             int abc = 0;
388             System.identityHashCode(abc);
389 
390             for (int j = 0; j < 10; j++) {
391                 abc = filterCount;
392                 System.identityHashCode(abc);
393             }
394         }
395     }
396 
397     public void testIssue32_1()
398     {
399         Option srcDdlFile = OptionBuilder.create("f");
400         Option logDdlFile = OptionBuilder.create("o");
401         Option help = OptionBuilder.create("h");
402 
403         Options options = new Options();
404         options.something();
405         options.something();
406         options.something();
407         options.something();
408         options.addOption(srcDdlFile, logDdlFile, help); // distance=1
409     }
410 
411     public void testIssue32_2()
412     {
413         int mm = Integer.parseInt("2");
414         long timeNow = 0;
415         Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
416         cal.setTimeInMillis(timeNow);
417         cal.set(Calendar.SECOND, 0);
418         cal.set(Calendar.MILLISECOND, 0);
419         cal.set(Calendar.HOUR_OF_DAY, mm);
420         cal.set(Calendar.MINUTE, mm); // distance=1
421     }
422 
423     public void testIssue32_3(MyObject[] objects) {
424         Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
425         for(int i=0; i<objects.length; i++) {
426             objects[i].setEnabled(true);
427             objects[i].setColor(0x121212);
428             objects[i].setUrl("http://google.com");
429             objects[i].setSize(789);
430             objects[i].setCalendar(cal); // distance=1
431         }
432     }
433 
434     public String testIssue32_4(boolean flag) {
435         StringBuilder builder = new StringBuilder();
436         builder.append("flag is ");
437         builder.append(flag);
438         final String line = "";
439         if(flag) {
440             builder.append("line of AST is:");
441             builder.append("\n");
442             builder.append(String.valueOf(line)); //distance=1
443             builder.append("\n");
444         }
445         return builder.toString();
446     }
447 
448     public void testIssue32_5() {
449         Option a = null;
450         Option b = null;
451         Option c = null;
452         boolean isCNull = isNull(c); // distance=1
453         boolean isBNull = isNull(b); // distance=1
454         boolean isANull = isNull(a); // distance=1
455     }
456 
457     public void testIssue32_6() {
458         Option aOpt = null;
459         Option bOpt = null;
460         Option cOpt = null;
461         isNull(cOpt); // distance = 1
462         isNull(bOpt); // distance = 2
463         isNull(aOpt); // distance = 3
464     }
465 
466     public void testIssue32_7() {
467         String line = "abc";
468         otherWriter.write(line);
469         line.charAt(1);
470         builder.append(line);
471         test(line, line, line);
472     }
473 
474     public void testIssue32_8(Writer w1, Writer w2, Writer w3) {
475         String l1="1";
476 
477 
478         w3.write(l1); //distance=3
479     }
480 
481     public void testIssue32_9() {
482         Options options = new Options();
483         Option myOption = null; //warn
484         options.addBindFile(null);
485         options.addBindFile(null);
486         options.addBindFile(null);
487         options.addBindFile(null);
488         options.addBindFile(null);
489         System.identityHashCode("message");
490         myOption.setArgName("abc"); // distance=7
491     }
492 
493     public void testIssue32_10() {
494         Options options = new Options();
495         Option myOption = null; //warn
496         options.addBindFile(null);
497         options.addBindFile(null);
498         options.addBindFile(null);
499         options.addBindFile(null);
500         options.addBindFile(null);
501         myOption.setArgName("q"); // distance=6
502     }
503 
504 
505     public int testIssue32_11(String toDir)
506             throws Exception
507     {
508         int count = 0;  // warn
509         String[] files = {};
510 
511         System.identityHashCode("Data archival started");
512         files.notify();
513         System.identityHashCode("sss");
514 
515         if (files == null || files.length == 0) {
516             System.identityHashCode("No files on a remote site");
517         }
518         else {
519             System.identityHashCode("Files on remote site: " + files.length);
520 
521             for (String ftpFile : files) {
522                 if (files.length == 0) {
523                     "".concat("");
524                     ftpFile.concat(files[2]);
525                     count++;
526                 }
527             }
528         }
529 
530         System.lineSeparator();
531 
532         return count;
533     }
534 
535     private Session openSession() {
536         return null;
537 
538     }
539 
540     class Session {
541 
542         public Transaction beginTransaction() {
543             return null;
544         }
545 
546         public void close() {
547         }
548 
549         public Serializable save(E d2) {
550             return null;
551         }
552 
553         public Serializable save(A a) {
554             return null;
555         }
556 
557     }
558 
559     class Transaction {
560 
561         public void commit() {
562 
563         }
564 
565     }
566 
567     class A {
568 
569         public void setForward(E d1) {
570 
571         }
572 
573     }
574 
575     class E {
576 
577         public void setReverse(C1 c) {
578 
579         }
580 
581         public void setReverse(A a) {
582 
583         }
584 
585     }
586 
587     class C1 {
588 
589         public void setForward(E d2) {
590 
591         }
592 
593     }
594 
595     class Serializable {
596 
597     }
598 
599     class JMenuItem {
600 
601         public JMenuItem(String string) {
602         }
603 
604         public void addActionListener(ActionListener actionListener) {
605 
606         }
607 
608         public void setMnemonic(char charAt) {
609 
610         }
611 
612     }
613 
614     class LogLevel {
615 
616     }
617 
618     class ActionListener {
619 
620     }
621 
622     class ActionEvent {
623 
624     }
625 
626     private void showLogLevelColorChangeDialog(JMenuItem j, LogLevel l) {   }
627 
628     static class Color {
629 
630         public Color(int red, int green, int blue, int alpha) {
631         }
632 
633         public double getRed() {
634             return 0;
635         }
636 
637         public int getAlpha() {
638             return 0;
639         }
640 
641         public double getBlue() {
642             return 0;
643         }
644 
645         public double getGreen() {
646             return 0;
647         }
648 
649     }
650 
651     class AuthUpdateTask {
652 
653         public AuthUpdateTask(Object authCheckUrl, Object authInfo,
654                 IAuthListener iAuthListener) {
655         }
656 
657     }
658 
659     interface IAuthListener {
660 
661         void authTokenChanged(String cookie, String token);
662 
663     }
664 
665     void fireAuthTokenChanged(String s, String s1) {}
666 
667     class Timer {
668 
669         public Timer(String string, boolean b) {
670         }
671 
672         public void schedule(AuthUpdateTask authUpdateTask, long l,
673                 long intervalMs) {
674         }
675 
676     }
677 
678     class Option {
679 
680         public void setArgName(String string) {
681         }
682 
683     }
684 
685     boolean isNull(Option o) {
686         return false;}
687 
688     class Writer {
689 
690         public void write(String l3) {
691 
692         }
693 
694     }
695 
696     class Options {
697 
698         public void addBindFile(Object object) {
699 
700         }
701 
702         public void
703                 addOption(Option srcDdlFile, Option logDdlFile, Option help)
704         {
705 
706         }
707 
708         public void something()
709         {
710 
711         }
712 
713     }
714 
715     class TreeMapNode {
716 
717         public TreeMapNode(String label, double d, DefaultValue defaultValue) {
718         }
719 
720         public TreeMapNode(String label) {
721         }
722 
723     }
724 
725     class DefaultValue {
726 
727         public DefaultValue(double d) {
728         }
729 
730     }
731 
732     static class LogLog {
733 
734         public static void warn(String string)
735         {
736 
737         }
738 
739         public static void setInternalDebugging(String confDebug, boolean b)
740         {
741 
742         }
743 
744     }
745 
746     static class OptionBuilder {
747 
748         public static Option create(String string)
749         {
750             return null;
751         }
752 
753     }
754 
755     class MyObject {
756 
757         public void setEnabled(boolean b)
758         {
759 
760         }
761 
762         public void setCalendar(Calendar cal)
763         {
764 
765         }
766 
767         public void setSize(int i)
768         {
769 
770         }
771 
772         public void setUrl(String string)
773         {
774 
775         }
776 
777         public void setColor(int i)
778         {
779 
780         }
781 
782     }
783 
784     static class otherWriter {
785 
786         public static void write(String line)
787         {
788 
789         }
790 
791     }
792 
793     void test(String s, String s1, String s2) {
794 
795     }
796 
797     static class builder {
798 
799         public static void append(String line)
800         {
801 
802         }
803 
804     }
805 
806 }