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