aboutsummaryrefslogtreecommitdiff
path: root/src/test/resources/com/puppycrawl/tools/checkstyle/checks/coding/InputVariableDeclarationUsageDistance.java
blob: ff223c802a402d7398db84a9143e9ef4718090d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
package com.puppycrawl.tools.checkstyle.checks.coding;
import java.util.*;
public class InputVariableDeclarationUsageDistance {

	private static int test1 = 0;

	static {
		int b = 0;
		int d = 0;
		{
			d = ++b;
		}
	}

	static {
		int c = 0;
		int a = 3;
		int b = 2;
		{
			a = a + b;
			c = b;
		}
		{
			c--;
		}
		a = 7;
	}

	static {
		int a = -1;
		int b = 2;
		b++;
		int c = --b;
		a = b; // DECLARATION OF VARIABLE 'a' SHOULD BE HERE (distance = 2)
	}

	public InputVariableDeclarationUsageDistance(int test1) {
		int temp = -1;
		this.test1 = test1;
		temp = test1; // DECLARATION OF VARIABLE 'temp' SHOULD BE HERE (distance = 2)
	}

	public boolean testMethod() {
		int temp = 7;
		new InputVariableDeclarationUsageDistance(2);
		String.valueOf(temp); // DECLARATION OF VARIABLE 'temp' SHOULD BE HERE (distance = 2)
		boolean result = false;
		String str = "";
		if (test1 > 1) {
			str = "123";
			result = true;
		}
		return result;
	}

	public void testMethod2() {
		int count;
		int a = 3;
		int b = 2;
		{
			a = a
					+ b
					- 5
					+ 2
					* a;
			count = b; // DECLARATION OF VARIABLE 'count' SHOULD BE HERE (distance = 2)
		}
	}

	public void testMethod3() {
		int count;
		int a = 3;
		int b = 3;
		a = a + b;
		b = a + a;
		testMethod2();
		count = b; // DECLARATION OF VARIABLE 'count' SHOULD BE HERE (distance = 4)
	}

	public void testMethod4(int arg) {
		int d = 0;
		for (int i = 0; i < 10; i++) {
			d++;
			if (i > 5) {
				d += arg;
			}
		}

		String ar[] = { "1", "2" };
		for (String st : ar) {
			System.out.println(st);
		}
	}

	public void testMethod5() {
		int arg = 7;
		boolean b = true;
		boolean bb = false;
		if (b)
			if (!bb)
				b = false;
		testMethod4(arg); // DECLARATION OF VARIABLE 'arg' SHOULD BE HERE (distance = 2)
	}

	public void testMethod6() {
		int blockNumWithSimilarVar = 3;
		int dist = 0;
		int index = 0;
		int block = 0;

		if (blockNumWithSimilarVar <= 1) {
			do {
				dist++;
				if (block > 4) {
					break;
				}
				index++;
				block++;
			} while (index < 7);
		} else {
			while (index < 8) {
				dist += block;
				index++;
				block++;
			}
		}
	}

	public boolean testMethod7(int a) {
		boolean res;
		switch (a) {
		case 1:
			res = true;
			break;
		default:
			res = false;
		}
		return res;
	}

	public void testMethod8() {
		int b = 0;
		int c = 0;
		int m = 0;
		int n = 0;
		{
			c++;
			b++;
		}
		{
			n++; // DECLARATION OF VARIABLE 'n' SHOULD BE HERE (distance = 2)
			m++; // DECLARATION OF VARIABLE 'm' SHOULD BE HERE (distance = 3)
			b++;
		}
	}

	public void testMethod9() {
		boolean result = false;
		boolean b1 = true;
		boolean b2 = false;
		if (b1) {
			if (!b2) {
				result = true;
			}
			result = true;
		}
	}

	public boolean testMethod10() {
		boolean result;
		try {
			result = true;
		} catch (Exception e) {
			result = false;
		} finally {
			result = false;
		}
		return result;
	}

	public void testMethod11() {
		int a = 0;
		int b = 10;
		boolean result;
		try {
			b--;
		} catch (Exception e) {
			b++;
			result = false; // DECLARATION OF VARIABLE 'result' SHOULD BE HERE (distance = 2)
		} finally {
			a++;
		}
	}

	public void testMethod12() {
		boolean result = false;
		boolean b3 = true;
		boolean b1 = true;
		boolean b2 = false;
		if (b1) {
			if (b3) {
				if (!b2) {
					result = true;
				}
				result = true;
			}
		}
	}

	public void testMethod13() {
		int i = 9;
		int j = 6;
		int g = i + 8;
		int k = j + 10;
	}

	public void testMethod14() {
		Session s = openSession();
		Transaction t = s.beginTransaction();
		A a = new A();
		E d1 = new E();
		C1 c = new C1();
		E d2 = new E();
		a.setForward(d1);
		d1.setReverse(a);
		c.setForward(d2); // DECLARATION OF VARIABLE 'c' SHOULD BE HERE (distance = 3)
							// DECLARATION OF VARIABLE 'd2' SHOULD BE HERE (distance = 3)
		d2.setReverse(c);
		Serializable aid = s.save(a);
		Serializable d2id = s.save(d2);
		t.commit(); // DECLARATION OF VARIABLE 't' SHOULD BE HERE (distance = 5)
		s.close();
	}

	public boolean isCheckBoxEnabled(int path) {
		String model = "";
		if (true) {
			for (int index = 0; index < path; ++index) {
				int nodeIndex = model.codePointAt(path);
				if (model.contains("")) {
					return false;
				}
			}
		} else {
			int nodeIndex = model.codePointAt(path);
			if (model.contains("")) {
				return false;
			}
		}
		return true;
	}

	public Object readObject(String in) throws Exception {
		String startDay = new String("");
		String endDay = new String("");
		return new String(startDay + endDay);
	}

	public int[] getSelectedIndices() {
		int[] selected = new int[5];
		String model = "";
		int a = 0;
		a++;
		for (int index = 0; index < 5; ++index) {
			selected[index] = Integer.parseInt(model.valueOf(a)); // DECLARATION OF VARIABLE 'selected' SHOULD BE HERE (distance = 2)
																						// DECLARATION OF VARIABLE 'model' SHOULD BE HERE (distance = 2)
		}
		return selected;
	}

	public void testMethod15() {
		String confDebug = "";
		if (!confDebug.equals("") && !confDebug.equals("null")) {
			LogLog.warn("The \"" + "\" attribute is deprecated.");
			LogLog.warn("Use the \"" + "\" attribute instead.");
			LogLog.setInternalDebugging(confDebug, true);
		}

		int i = 0;
		int k = 7;
		boolean b = false;
		for (; i < k; i++) {
			b = true;
			k++;
		}

		int sw;
		switch (i) {
		case 0:
			k++;
			sw = 0; // DECLARATION OF VARIABLE 'sw' SHOULD BE HERE (distance = 2)
			break;
		case 1:
			b = false;
			break;
		default:
			b = true;
		}

		int wh = 0;
		b = true;
		do {
			k--;
			i++;
		} while (wh > 0); // DECLARATION OF VARIABLE 'wh' SHOULD BE HERE (distance = 2)

		if (wh > 0) {
			k++;
		} else if (!b) {
			i++;
		} else {
			i--;
		}
	}

	public void testMethod16() {
		int wh = 1, i = 4, k = 0;
		if (i > 0) {
			k++;
		} else if (wh > 0) {
			i++;
		} else {
			i--;
		}
	}
	
	protected JMenuItem createSubMenuItem(LogLevel level) {
	    final JMenuItem result = new JMenuItem(level.toString());
	    final LogLevel logLevel = level;
	    result.setMnemonic(level.toString().charAt(0));
	    result.addActionListener(new ActionListener() {
	      public void actionPerformed(ActionEvent e) {
	        showLogLevelColorChangeDialog(result, logLevel); // DECLARATION OF VARIABLE 'logLevel' SHOULD BE HERE (distance = 2)
	      }
	    });

	    return result;

	  }
	
	public static Color darker(Color color, double fraction) {
        int red = (int) Math.round(color.getRed() * (1.0 - fraction));
        int green = (int) Math.round(color.getGreen() * (1.0 - fraction));
        int blue = (int) Math.round(color.getBlue() * (1.0 - fraction));

        if (red < 0) {
            red = 0;
        } else if (red > 255) {
            red = 255;
        }
        if (green < 0) { // DECLARATION OF VARIABLE 'green' SHOULD BE HERE (distance = 2)
            green = 0;
        } else if (green > 255) {
            green = 255;
        }
        if (blue < 0) { // DECLARATION OF VARIABLE 'blue' SHOULD BE HERE (distance = 3)
            // blue = 0;
        }

        int alpha = color.getAlpha();

        return new Color(red, green, blue, alpha);
    }
	
	public void testFinal() {
		AuthUpdateTask authUpdateTask = null;
		final long intervalMs = 30 * 60000L; // 30 min
		Object authCheckUrl = null, authInfo = null;
        authUpdateTask = new AuthUpdateTask(authCheckUrl, authInfo, new IAuthListener() {
            @Override
            public void authTokenChanged(String cookie, String token) {
                fireAuthTokenChanged(cookie, token);
            }
        });

        Timer authUpdateTimer = new Timer("Auth Guard", true);
        authUpdateTimer.schedule(authUpdateTask, intervalMs / 2, intervalMs); // DECLARATION OF VARIABLE 'intervalMs' SHOULD BE HERE (distance = 2)
	}
	
	public void testForCycle() {
		int filterCount = 0;
		for (int i = 0; i < 10; i++, filterCount++) {
			int abc = 0;
			System.out.println(abc);

			for (int j = 0; j < 10; j++) {
				abc = filterCount;
				System.out.println(abc);
			}
		}
	}
	
	public void testIssue32_1()
    {
        Option srcDdlFile = OptionBuilder.create("f");
        Option logDdlFile = OptionBuilder.create("o");
        Option help = OptionBuilder.create("h");

        Options options = new Options();
        options.something();
        options.something();
        options.something();
        options.something();
        options.addOption(srcDdlFile, logDdlFile, help); // distance=1
    }

    public void testIssue32_2()
    {
        int mm = Integer.parseInt("2");
        long timeNow = 0;
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(timeNow);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cal.set(Calendar.HOUR_OF_DAY, mm);
        cal.set(Calendar.MINUTE, mm); // distance=1
    }
    
    public void testIssue32_3(MyObject[] objects) {
        Calendar cal = Calendar.getInstance();
        for(int i=0; i<objects.length; i++) {
            objects[i].setEnabled(true);
            objects[i].setColor(0x121212);
            objects[i].setUrl("http://google.com");
            objects[i].setSize(789);
            objects[i].setCalendar(cal); // distance=1
        }
    }
    
    public String testIssue32_4(boolean flag) {
        StringBuilder builder = new StringBuilder();
        builder.append("flag is ");
        builder.append(flag);
        final String line = "";
        if(flag) {
            builder.append("line of AST is:");
            builder.append("\n");
            builder.append(String.valueOf(line)); //distance=1
            builder.append("\n");
        }
        return builder.toString();
    }
    
    public void testIssue32_5() {
        Option a = null;
        Option b = null;
        Option c = null;
        boolean isCNull = isNull(c); // distance=1
        boolean isBNull = isNull(b); // distance=1
        boolean isANull = isNull(a); // distance=1
    }
  
    public void testIssue32_6() {
        Option aOpt = null;
        Option bOpt = null;
        Option cOpt = null;
        isNull(cOpt); // distance = 1
        isNull(bOpt); // distance = 2
        isNull(aOpt); // distance = 3
    }
    
    public void testIssue32_7() {
        String line = "abc";
        otherWriter.write(line);
        line.charAt(1);
        builder.append(line);
        test(line, line, line);
    }
    
    public void testIssue32_8(Writer w1, Writer w2, Writer w3) {
        String l1="1", l2="2", l3="3";
        w1.write(l3); //distance=1
        w2.write(l2); //distance=2
        w3.write(l1); //distance=3
    }
    
    public void testIssue32_9() {
        Options options = new Options();
        Option myOption = null;
        options.addBindFile(null);
        options.addBindFile(null);
        options.addBindFile(null);
        options.addBindFile(null);
        options.addBindFile(null);
        System.out.println("message");
        myOption.setArgName("abc"); // distance=7
    }
    
    public void testIssue32_10() {
        Options options = new Options();
        Option myOption = null;
        options.addBindFile(null);
        options.addBindFile(null);
        options.addBindFile(null);
        options.addBindFile(null);
        options.addBindFile(null);
        myOption.setArgName("q"); // distance=6
    }
    
    
    public int testIssue32_11(String toDir)
            throws Exception
    {
        int count = 0;
        String[] files = {};

        System.out.println("Data archivation started");
        files.notify();
        System.out.println("sss");

        if (files == null || files.length == 0) {
            System.out.println("No files on a remote site");
        }
        else {
            System.out.println("Files on remote site: " + files.length);

            for (String ftpFile : files) {
                if (files.length == 0) {
                    "".concat("");
                    ftpFile.concat(files[2]);
                    count++;
                }
            }
        }

        System.out.println();

        return count;
    }
    
    //////////////////////////////////////////////////
    // False positive. Will be fixed in future.
    //////////////////////////////////////////////////
    private TreeMapNode buildTree(Object[][] tree)
    {   
        int k = 0;
        tree.notify();
        TreeMapNode root = null;
        for (Object[] s : tree) {
            Integer id = (Integer) s[0];
            String label = (String) s[1];
            Integer parentId = (Integer) s[2]; ///!!!!!!!!
            Number weight = (Number) s[3];
            Number value = (Number) s[4];
            Integer childCount = (Integer) s[5];
            TreeMapNode node;
            if (childCount == 0) {
                node = new TreeMapNode(label,
                        weight != null ? weight.doubleValue() : 0.0,
                        new DefaultValue(value != null ? value.doubleValue()
                                : 0.0));
            }
            else {
                node = new TreeMapNode(label);
            }
            System.out.println(id.toString() + node);
            System.out.println(node.toString() + id);
            if (parentId == null || parentId == -1) { ///!!!!!!!
                root = node;
            }
            else {
                System.out.println(parentId.toString() +node);
            }
        }
        return root;
    }
    
    private Session openSession() {
        return null;
        
    }
    
    class Session {

        public Transaction beginTransaction() {
            return null;
        }

        public void close() {
        }

        public Serializable save(E d2) {
            return null;
        }

        public Serializable save(A a) {
            return null;
        }
        
    }
    
    class Transaction {

        public void commit() {
            
        }
        
    }
    
    class A {

        public void setForward(E d1) {
            
        }
        
    }
    
    class E {

        public void setReverse(C1 c) {
            
        }

        public void setReverse(A a) {
            
        }
        
    }
    
    class C1 {

        public void setForward(E d2) {
            
        }
        
    }
    
    class Serializable {
        
    }
    
    class JMenuItem {

        public JMenuItem(String string) {
        }

        public void addActionListener(ActionListener actionListener) {
            
        }

        public void setMnemonic(char charAt) {
            
        }
        
    }
    
    class LogLevel {
        
    }
    
    class ActionListener {
        
    }
    
    class ActionEvent {
        
    }
    
    private void showLogLevelColorChangeDialog(JMenuItem j, LogLevel l) {   }
    
    static class Color {

        public Color(int red, int green, int blue, int alpha) {
        }

        public double getRed() {
            return 0;
        }

        public int getAlpha() {
            return 0;
        }

        public double getBlue() {
            return 0;
        }

        public double getGreen() {
            return 0;
        }
        
    }
    
    class AuthUpdateTask {

        public AuthUpdateTask(Object authCheckUrl, Object authInfo,
                IAuthListener iAuthListener) {
        }
        
    }
    
    interface IAuthListener {

        void authTokenChanged(String cookie, String token);
        
    }
    
    void fireAuthTokenChanged(String s, String s1) {}
    
    class Timer {

        public Timer(String string, boolean b) {
        }

        public void schedule(AuthUpdateTask authUpdateTask, long l,
                long intervalMs) {
        }
        
    }
    
    class Option {

        public void setArgName(String string) {
        }
        
    }
    
    boolean isNull(Option o) {
		return false;}
    
    class Writer {

        public void write(String l3) {
            
        }
        
    }
    
    class Options {

        public void addBindFile(Object object) {
            
        }

		public void
				addOption(Option srcDdlFile, Option logDdlFile, Option help)
		{
			
		}

		public void something()
		{
			
		}
        
    }
    
    class TreeMapNode {

        public TreeMapNode(String label, double d, DefaultValue defaultValue) {
        }

        public TreeMapNode(String label) {
        }
        
    }

    class DefaultValue {

        public DefaultValue(double d) {
        }
        
    }
    
    static class LogLog {

		public static void warn(String string)
		{
			
		}

		public static void setInternalDebugging(String confDebug, boolean b)
		{
			
		}
    	
    }
    
    static class OptionBuilder {

		public static Option create(String string)
		{
			return null;
		}
    	
    }
    
    class MyObject {

		public void setEnabled(boolean b)
		{
			
		}

		public void setCalendar(Calendar cal)
		{
			
		}

		public void setSize(int i)
		{
			
		}

		public void setUrl(String string)
		{
			
		}

		public void setColor(int i)
		{
			
		}
    	
    }
    
    static class otherWriter {

		public static void write(String line)
		{
			
		}
    	
    }
    
    void test(String s, String s1, String s2) {
    	
    }
    
    static class builder {

		public static void append(String line)
		{
			
		}
    	
    }
    
}

class New {
    void a() {
        int a = 1;
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        while (true) {
            System.out.println();
            System.out.println(a);
        }
    }
    
    void b() {
        int a = 1;
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        do {
            System.out.println();
            System.out.println(a);
        } while (true);
    }
    
    void c() {
        int a = 1;
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        for (;;) {
            System.out.println();
            System.out.println(a);
        }
    }
    
    void d() {
        int a = 1;
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        for (int i: new int[]{1,2,3}) {
            System.out.println();
            System.out.println(a);
        }
    }

    void f() {
        int a = 1;
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        while (true)
            System.out.println(a);
    }
    
    void h() {
        int a = 1;
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        while (true)
            while (true)
                a++;
    }
    
    void i() {
        int a = 1;
        switch (Math.max(1, 2)) {
        case 1:
            System.out.println();
            break;
        case 2:
            System.out.println();
            break;
        }

        switch (Math.max(1, 2)) {
        case 1:
            System.out.println(a);
            break;
        case 2:
            System.out.println(a);
            break;
        }
    }
    
    void k() {
        int a = 1;
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        while (true) {
            System.out.println();
            if (true) {
                System.out.println();
            } else if (true) {
                System.out.println(a);
            } else {
                System.out.println();
            }
        }
    }
    
    void l() {
        int a = 1;
        
        while (true) {
            switch (hashCode()){}
            switch (Math.max(1, 2)) {
            case 1:
                System.out.println(a);
                break;
            case 2:
                System.out.println(a);
                break;
            }
        }
    }
    
    void tryWithoutFinally() {
        int a = 1;
        System.out.println();
        System.out.println();
        System.out.println();
        try {
            a = 2;
        }
        catch(Exception e){}
    }
    
}