View Javadoc
1   package com.puppycrawl.tools.checkstyle.checks.coding.variabledeclarationusagedistance;
2   import java.util.*;
3   public class InputVariableDeclarationUsageDistance {
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 InputVariableDeclarationUsageDistance(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 InputVariableDeclarationUsageDistance(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;
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();
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; // 30 min
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", l2="2", l3="3";
472         w1.write(l3); //distance=1
473         w2.write(l2); //distance=2
474         w3.write(l1); //distance=3
475     }
476     
477     public void testIssue32_9() {
478         Options options = new Options();
479         Option myOption = null;
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;
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     //////////////////////////////////////////////////
532     // False positive. Will be fixed in future.
533     //////////////////////////////////////////////////
534     private TreeMapNode buildTree(Object[][] tree)
535     {   
536         int k = 0;
537         tree.notify();
538         TreeMapNode root = null;
539         for (Object[] s : tree) {
540             Integer id = (Integer) s[0];
541             String label = (String) s[1];
542             Integer parentId = (Integer) s[2]; ///!!!!!!!!
543             Number weight = (Number) s[3];
544             Number value = (Number) s[4];
545             Integer childCount = (Integer) s[5];
546             TreeMapNode node;
547             if (childCount == 0) {
548                 node = new TreeMapNode(label,
549                         weight != null ? weight.doubleValue() : 0.0,
550                         new DefaultValue(value != null ? value.doubleValue()
551                                 : 0.0));
552             }
553             else {
554                 node = new TreeMapNode(label);
555             }
556             System.identityHashCode(id.toString() + node);
557             System.identityHashCode(node.toString() + id);
558             if (parentId == null || parentId == -1) { ///!!!!!!!
559                 root = node;
560             }
561             else {
562                 System.identityHashCode(parentId.toString() +node);
563             }
564         }
565         return root;
566     }
567     
568     private Session openSession() {
569         return null;
570         
571     }
572     
573     class Session {
574 
575         public Transaction beginTransaction() {
576             return null;
577         }
578 
579         public void close() {
580         }
581 
582         public Serializable save(E d2) {
583             return null;
584         }
585 
586         public Serializable save(A a) {
587             return null;
588         }
589         
590     }
591     
592     class Transaction {
593 
594         public void commit() {
595             
596         }
597         
598     }
599     
600     class A {
601 
602         public void setForward(E d1) {
603             
604         }
605         
606     }
607     
608     class E {
609 
610         public void setReverse(C1 c) {
611             
612         }
613 
614         public void setReverse(A a) {
615             
616         }
617         
618     }
619     
620     class C1 {
621 
622         public void setForward(E d2) {
623             
624         }
625         
626     }
627     
628     class Serializable {
629         
630     }
631     
632     class JMenuItem {
633 
634         public JMenuItem(String string) {
635         }
636 
637         public void addActionListener(ActionListener actionListener) {
638             
639         }
640 
641         public void setMnemonic(char charAt) {
642             
643         }
644         
645     }
646     
647     class LogLevel {
648         
649     }
650     
651     class ActionListener {
652         
653     }
654     
655     class ActionEvent {
656         
657     }
658     
659     private void showLogLevelColorChangeDialog(JMenuItem j, LogLevel l) {   }
660     
661     static class Color {
662 
663         public Color(int red, int green, int blue, int alpha) {
664         }
665 
666         public double getRed() {
667             return 0;
668         }
669 
670         public int getAlpha() {
671             return 0;
672         }
673 
674         public double getBlue() {
675             return 0;
676         }
677 
678         public double getGreen() {
679             return 0;
680         }
681         
682     }
683     
684     class AuthUpdateTask {
685 
686         public AuthUpdateTask(Object authCheckUrl, Object authInfo,
687                 IAuthListener iAuthListener) {
688         }
689         
690     }
691     
692     interface IAuthListener {
693 
694         void authTokenChanged(String cookie, String token);
695         
696     }
697     
698     void fireAuthTokenChanged(String s, String s1) {}
699     
700     class Timer {
701 
702         public Timer(String string, boolean b) {
703         }
704 
705         public void schedule(AuthUpdateTask authUpdateTask, long l,
706                 long intervalMs) {
707         }
708         
709     }
710     
711     class Option {
712 
713         public void setArgName(String string) {
714         }
715         
716     }
717     
718     boolean isNull(Option o) {
719 		return false;}
720     
721     class Writer {
722 
723         public void write(String l3) {
724             
725         }
726         
727     }
728     
729     class Options {
730 
731         public void addBindFile(Object object) {
732             
733         }
734 
735 		public void
736 				addOption(Option srcDdlFile, Option logDdlFile, Option help)
737 		{
738 			
739 		}
740 
741 		public void something()
742 		{
743 			
744 		}
745         
746     }
747     
748     class TreeMapNode {
749 
750         public TreeMapNode(String label, double d, DefaultValue defaultValue) {
751         }
752 
753         public TreeMapNode(String label) {
754         }
755         
756     }
757 
758     class DefaultValue {
759 
760         public DefaultValue(double d) {
761         }
762         
763     }
764     
765     static class LogLog {
766 
767 		public static void warn(String string)
768 		{
769 			
770 		}
771 
772 		public static void setInternalDebugging(String confDebug, boolean b)
773 		{
774 			
775 		}
776     	
777     }
778     
779     static class OptionBuilder {
780 
781 		public static Option create(String string)
782 		{
783 			return null;
784 		}
785     	
786     }
787     
788     class MyObject {
789 
790 		public void setEnabled(boolean b)
791 		{
792 			
793 		}
794 
795 		public void setCalendar(Calendar cal)
796 		{
797 			
798 		}
799 
800 		public void setSize(int i)
801 		{
802 			
803 		}
804 
805 		public void setUrl(String string)
806 		{
807 			
808 		}
809 
810 		public void setColor(int i)
811 		{
812 			
813 		}
814     	
815     }
816     
817     static class otherWriter {
818 
819 		public static void write(String line)
820 		{
821 			
822 		}
823     	
824     }
825     
826     void test(String s, String s1, String s2) {
827     	
828     }
829     
830     static class builder {
831 
832 		public static void append(String line)
833 		{
834 			
835 		}
836     	
837     }
838     
839 }
840 
841 class New {
842     void a() {
843         int a = 1;
844         System.lineSeparator();
845         System.lineSeparator();
846         System.lineSeparator();
847         System.lineSeparator();
848         while (true) {
849             System.lineSeparator();
850             System.identityHashCode(a);
851         }
852     }
853     
854     void b() {
855         int a = 1;
856         System.lineSeparator();
857         System.lineSeparator();
858         System.lineSeparator();
859         System.lineSeparator();
860         do {
861             System.lineSeparator();
862             System.identityHashCode(a);
863         } while (true);
864     }
865     
866     void c() {
867         int a = 1;
868         System.lineSeparator();
869         System.lineSeparator();
870         System.lineSeparator();
871         System.lineSeparator();
872         for (;;) {
873             System.lineSeparator();
874             System.identityHashCode(a);
875         }
876     }
877     
878     void d() {
879         int a = 1;
880         System.lineSeparator();
881         System.lineSeparator();
882         System.lineSeparator();
883         System.lineSeparator();
884         for (int i: new int[]{1,2,3}) {
885             System.lineSeparator();
886             System.identityHashCode(a);
887         }
888     }
889 
890     void f() {
891         int a = 1;
892         System.lineSeparator();
893         System.lineSeparator();
894         System.lineSeparator();
895         System.lineSeparator();
896         while (true)
897             System.identityHashCode(a);
898     }
899     
900     void h() {
901         int a = 1;
902         System.lineSeparator();
903         System.lineSeparator();
904         System.lineSeparator();
905         System.lineSeparator();
906         while (true)
907             while (true)
908                 a++;
909     }
910     
911     void i() {
912         int a = 1;
913         switch (Math.max(1, 2)) {
914         case 1:
915             System.lineSeparator();
916             break;
917         case 2:
918             System.lineSeparator();
919             break;
920         }
921 
922         switch (Math.max(1, 2)) {
923         case 1:
924             System.identityHashCode(a);
925             break;
926         case 2:
927             System.identityHashCode(a);
928             break;
929         }
930     }
931     
932     void k() {
933         int a = 1;
934         System.lineSeparator();
935         System.lineSeparator();
936         System.lineSeparator();
937         System.lineSeparator();
938         while (true) {
939             System.lineSeparator();
940             if (true) {
941                 System.lineSeparator();
942             } else if (true) {
943                 System.identityHashCode(a);
944             } else {
945                 System.lineSeparator();
946             }
947         }
948     }
949     
950     void l() {
951         int a = 1;
952         
953         while (true) {
954             switch (hashCode()){}
955             switch (Math.max(1, 2)) {
956             case 1:
957                 System.identityHashCode(a);
958                 break;
959             case 2:
960                 System.identityHashCode(a);
961                 break;
962             }
963         }
964     }
965     
966     void tryWithoutFinally() {
967         int a = 1;
968         System.lineSeparator();
969         System.lineSeparator();
970         System.lineSeparator();
971         try {
972             a = 2;
973         }
974         catch(Exception e){}
975     }
976 
977     void m() {
978         final int a = 1;
979         int b = 0;
980 
981         if (b == 1) {
982             System.lineSeparator();
983         }
984 
985         final int c = a + 1;
986     }
987     
988 }