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[] selected = new int[5];
261 		String model = "";
262 		int a = 0;
263 		a++;
264 		for (int index = 0; index < 5; ++index) {
265 			selected[index] = Integer.parseInt(model.valueOf(a)); // DECLARATION OF VARIABLE 'selected' SHOULD BE HERE (distance = 2)
266 																						// DECLARATION OF VARIABLE 'model' SHOULD BE HERE (distance = 2)
267 		}
268 		return selected;
269 	}
270 
271 	public void testMethod15() {
272 		String confDebug = "";
273 		if (!confDebug.equals("") && !confDebug.equals("null")) {
274 			LogLog.warn("The \"" + "\" attribute is deprecated.");
275 			LogLog.warn("Use the \"" + "\" attribute instead.");
276 			LogLog.setInternalDebugging(confDebug, true);
277 		}
278 
279 		int i = 0;
280 		int k = 7;
281 		boolean b = false;
282 		for (; i < k; i++) {
283 			b = true;
284 			k++;
285 		}
286 
287 		int sw;
288 		switch (i) {
289 		case 0:
290 			k++;
291 			sw = 0; // DECLARATION OF VARIABLE 'sw' SHOULD BE HERE (distance = 2)
292 			break;
293 		case 1:
294 			b = false;
295 			break;
296 		default:
297 			b = true;
298 		}
299 
300 		int wh = 0;
301 		b = true;
302 		do {
303 			k--;
304 			i++;
305 		} while (wh > 0); // DECLARATION OF VARIABLE 'wh' SHOULD BE HERE (distance = 2)
306 
307 		if (wh > 0) {
308 			k++;
309 		} else if (!b) {
310 			i++;
311 		} else {
312 			i--;
313 		}
314 	}
315 
316 	public void testMethod16() {
317 		int wh = 1, i = 4, k = 0;
318 		if (i > 0) {
319 			k++;
320 		} else if (wh > 0) {
321 			i++;
322 		} else {
323 			i--;
324 		}
325 	}
326 	
327 	protected JMenuItem createSubMenuItem(LogLevel level) {
328 	    final JMenuItem result = new JMenuItem(level.toString());
329 	    final LogLevel logLevel = level;
330 	    result.setMnemonic(level.toString().charAt(0));
331 	    result.addActionListener(new ActionListener() {
332 	      public void actionPerformed(ActionEvent e) {
333 	        showLogLevelColorChangeDialog(result, logLevel); // DECLARATION OF VARIABLE 'logLevel' SHOULD BE HERE (distance = 2)
334 	      }
335 	    });
336 
337 	    return result;
338 
339 	  }
340 	
341 	public static Color darker(Color color, double fraction) {
342         int red = (int) Math.round(color.getRed() * (1.0 - fraction));
343         int green = (int) Math.round(color.getGreen() * (1.0 - fraction));
344         int blue = (int) Math.round(color.getBlue() * (1.0 - fraction));
345 
346         if (red < 0) {
347             red = 0;
348         } else if (red > 255) {
349             red = 255;
350         }
351         if (green < 0) { // DECLARATION OF VARIABLE 'green' SHOULD BE HERE (distance = 2)
352             green = 0;
353         } else if (green > 255) {
354             green = 255;
355         }
356         if (blue < 0) { // DECLARATION OF VARIABLE 'blue' SHOULD BE HERE (distance = 3)
357             // blue = 0;
358         }
359 
360         int alpha = color.getAlpha();
361 
362         return new Color(red, green, blue, alpha);
363     }
364 	
365 	public void testFinal() {
366 		AuthUpdateTask authUpdateTask = null;
367 		final long intervalMs = 30 * 60000L;
368 		Object authCheckUrl = null, authInfo = null;
369         authUpdateTask = new AuthUpdateTask(authCheckUrl, authInfo, new IAuthListener() {
370             @Override
371             public void authTokenChanged(String cookie, String token) {
372                 fireAuthTokenChanged(cookie, token);
373             }
374         });
375 
376         Timer authUpdateTimer = new Timer("Auth Guard", true);
377         authUpdateTimer.schedule(authUpdateTask, intervalMs / 2, intervalMs); // DECLARATION OF VARIABLE 'intervalMs' SHOULD BE HERE (distance = 2)
378 	}
379 	
380 	public void testForCycle() {
381 		int filterCount = 0;
382 		for (int i = 0; i < 10; i++, filterCount++) {
383 			int abc = 0;
384 			System.identityHashCode(abc);
385 
386 			for (int j = 0; j < 10; j++) {
387 				abc = filterCount;
388 				System.identityHashCode(abc);
389 			}
390 		}
391 	}
392 	
393 	public void testIssue32_1()
394     {
395         Option srcDdlFile = OptionBuilder.create("f");
396         Option logDdlFile = OptionBuilder.create("o");
397         Option help = OptionBuilder.create("h");
398 
399         Options options = new Options();
400         options.something();
401         options.something();
402         options.something();
403         options.something();
404         options.addOption(srcDdlFile, logDdlFile, help); // distance=1
405     }
406 
407     public void testIssue32_2()
408     {
409         int mm = Integer.parseInt("2");
410         long timeNow = 0;
411         Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
412         cal.setTimeInMillis(timeNow);
413         cal.set(Calendar.SECOND, 0);
414         cal.set(Calendar.MILLISECOND, 0);
415         cal.set(Calendar.HOUR_OF_DAY, mm);
416         cal.set(Calendar.MINUTE, mm); // distance=1
417     }
418     
419     public void testIssue32_3(MyObject[] objects) {
420         Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
421         for(int i=0; i<objects.length; i++) {
422             objects[i].setEnabled(true);
423             objects[i].setColor(0x121212);
424             objects[i].setUrl("http://google.com");
425             objects[i].setSize(789);
426             objects[i].setCalendar(cal); // distance=1
427         }
428     }
429     
430     public String testIssue32_4(boolean flag) {
431         StringBuilder builder = new StringBuilder();
432         builder.append("flag is ");
433         builder.append(flag);
434         final String line = "";
435         if(flag) {
436             builder.append("line of AST is:");
437             builder.append("\n");
438             builder.append(String.valueOf(line)); //distance=1
439             builder.append("\n");
440         }
441         return builder.toString();
442     }
443     
444     public void testIssue32_5() {
445         Option a = null;
446         Option b = null;
447         Option c = null;
448         boolean isCNull = isNull(c); // distance=1
449         boolean isBNull = isNull(b); // distance=1
450         boolean isANull = isNull(a); // distance=1
451     }
452   
453     public void testIssue32_6() {
454         Option aOpt = null;
455         Option bOpt = null;
456         Option cOpt = null;
457         isNull(cOpt); // distance = 1
458         isNull(bOpt); // distance = 2
459         isNull(aOpt); // distance = 3
460     }
461     
462     public void testIssue32_7() {
463         String line = "abc";
464         otherWriter.write(line);
465         line.charAt(1);
466         builder.append(line);
467         test(line, line, line);
468     }
469     
470     public void testIssue32_8(Writer w1, Writer w2, Writer w3) {
471         String l1="1";
472 
473         
474         w3.write(l1); //distance=3
475     }
476     
477     public void testIssue32_9() {
478         Options options = new Options();
479         Option myOption = null; //warn
480         options.addBindFile(null);
481         options.addBindFile(null);
482         options.addBindFile(null);
483         options.addBindFile(null);
484         options.addBindFile(null);
485         System.identityHashCode("message");
486         myOption.setArgName("abc"); // distance=7
487     }
488     
489     public void testIssue32_10() {
490         Options options = new Options();
491         Option myOption = null; //warn
492         options.addBindFile(null);
493         options.addBindFile(null);
494         options.addBindFile(null);
495         options.addBindFile(null);
496         options.addBindFile(null);
497         myOption.setArgName("q"); // distance=6
498     }
499     
500     
501     public int testIssue32_11(String toDir)
502             throws Exception
503     {
504         int count = 0;
505         String[] files = {};
506 
507         System.identityHashCode("Data archivation started");
508         files.notify();
509         System.identityHashCode("sss");
510 
511         if (files == null || files.length == 0) {
512             System.identityHashCode("No files on a remote site");
513         }
514         else {
515             System.identityHashCode("Files on remote site: " + files.length);
516 
517             for (String ftpFile : files) {
518                 if (files.length == 0) {
519                     "".concat("");
520                     ftpFile.concat(files[2]);
521                     count++;
522                 }
523             }
524         }
525 
526         System.lineSeparator();
527 
528         return count;
529     }
530     
531     private Session openSession() {
532         return null;
533         
534     }
535     
536     class Session {
537 
538         public Transaction beginTransaction() {
539             return null;
540         }
541 
542         public void close() {
543         }
544 
545         public Serializable save(E d2) {
546             return null;
547         }
548 
549         public Serializable save(A a) {
550             return null;
551         }
552         
553     }
554     
555     class Transaction {
556 
557         public void commit() {
558             
559         }
560         
561     }
562     
563     class A {
564 
565         public void setForward(E d1) {
566             
567         }
568         
569     }
570     
571     class E {
572 
573         public void setReverse(C1 c) {
574             
575         }
576 
577         public void setReverse(A a) {
578             
579         }
580         
581     }
582     
583     class C1 {
584 
585         public void setForward(E d2) {
586             
587         }
588         
589     }
590     
591     class Serializable {
592         
593     }
594     
595     class JMenuItem {
596 
597         public JMenuItem(String string) {
598         }
599 
600         public void addActionListener(ActionListener actionListener) {
601             
602         }
603 
604         public void setMnemonic(char charAt) {
605             
606         }
607         
608     }
609     
610     class LogLevel {
611         
612     }
613     
614     class ActionListener {
615         
616     }
617     
618     class ActionEvent {
619         
620     }
621     
622     private void showLogLevelColorChangeDialog(JMenuItem j, LogLevel l) {   }
623     
624     static class Color {
625 
626         public Color(int red, int green, int blue, int alpha) {
627         }
628 
629         public double getRed() {
630             return 0;
631         }
632 
633         public int getAlpha() {
634             return 0;
635         }
636 
637         public double getBlue() {
638             return 0;
639         }
640 
641         public double getGreen() {
642             return 0;
643         }
644         
645     }
646     
647     class AuthUpdateTask {
648 
649         public AuthUpdateTask(Object authCheckUrl, Object authInfo,
650                 IAuthListener iAuthListener) {
651         }
652         
653     }
654     
655     interface IAuthListener {
656 
657         void authTokenChanged(String cookie, String token);
658         
659     }
660     
661     void fireAuthTokenChanged(String s, String s1) {}
662     
663     class Timer {
664 
665         public Timer(String string, boolean b) {
666         }
667 
668         public void schedule(AuthUpdateTask authUpdateTask, long l,
669                 long intervalMs) {
670         }
671         
672     }
673     
674     class Option {
675 
676         public void setArgName(String string) {
677         }
678         
679     }
680     
681     boolean isNull(Option o) {
682 		return false;}
683     
684     class Writer {
685 
686         public void write(String l3) {
687             
688         }
689         
690     }
691     
692     class Options {
693 
694         public void addBindFile(Object object) {
695             
696         }
697 
698 		public void
699 				addOption(Option srcDdlFile, Option logDdlFile, Option help)
700 		{
701 			
702 		}
703 
704 		public void something()
705 		{
706 			
707 		}
708         
709     }
710     
711     class TreeMapNode {
712 
713         public TreeMapNode(String label, double d, DefaultValue defaultValue) {
714         }
715 
716         public TreeMapNode(String label) {
717         }
718         
719     }
720 
721     class DefaultValue {
722 
723         public DefaultValue(double d) {
724         }
725         
726     }
727     
728     static class LogLog {
729 
730 		public static void warn(String string)
731 		{
732 			
733 		}
734 
735 		public static void setInternalDebugging(String confDebug, boolean b)
736 		{
737 			
738 		}
739     	
740     }
741     
742     static class OptionBuilder {
743 
744 		public static Option create(String string)
745 		{
746 			return null;
747 		}
748     	
749     }
750     
751     class MyObject {
752 
753 		public void setEnabled(boolean b)
754 		{
755 			
756 		}
757 
758 		public void setCalendar(Calendar cal)
759 		{
760 			
761 		}
762 
763 		public void setSize(int i)
764 		{
765 			
766 		}
767 
768 		public void setUrl(String string)
769 		{
770 			
771 		}
772 
773 		public void setColor(int i)
774 		{
775 			
776 		}
777     	
778     }
779     
780     static class otherWriter {
781 
782 		public static void write(String line)
783 		{
784 			
785 		}
786     	
787     }
788     
789     void test(String s, String s1, String s2) {
790     	
791     }
792     
793     static class builder {
794 
795 		public static void append(String line)
796 		{
797 			
798 		}
799     	
800     }
801     
802 }