-
Notifications
You must be signed in to change notification settings - Fork 2
/
UnnecessaryImport.xml
1198 lines (1057 loc) · 36.3 KB
/
UnnecessaryImport.xml
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
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<test-data
xmlns="http://pmd.sourceforge.net/rule-tests"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/rule-tests http://pmd.sourceforge.net/rule-tests_1_0_0.xsd">
<test-code>
<description>simple unused single type import</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
import java.io.File;
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>one used single type import</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.io.File;
public class Foo {
private File file;
}
]]></code>
</test-code>
<test-code>
<description>2 unused single-type imports</description>
<expected-problems>2</expected-problems>
<code><![CDATA[
import java.io.File;
import java.util.List;
public class Foo {
}
]]></code>
</test-code>
<test-code>
<description>1 used single type import</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.security.AccessController;
public class Foo {
public void foo() {
AccessController.doPrivileged(null);
}
}
]]></code>
</test-code>
<test-code>
<description>1 import stmt, used only in throws clause</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.rmi.RemoteException;
public class Foo {
public void foo() throws RemoteException {}
}
]]></code>
</test-code>
<test-code>
<description>for loop</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.ArrayList;
public class Foo {
public void foo(ArrayList list) {
for (String s : list) {}
}
}
]]></code>
</test-code>
<test-code>
<description>Generics</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import foo.TestInterfaceTwo;
import java.util.*;
public class Foo {
private List<TestInterfaceTwo> x = new ArrayList<TestInterfaceTwo>();
}
]]></code>
</test-code>
<test-code>
<description>Generics 2</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;
public class Foo {
private List<Collection> x = new ArrayList<Collection>();
}
]]></code>
</test-code>
<test-code>
<description>Annotations</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import foo.annotation.Retention;
import foo.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Foo {
}
]]></code>
</test-code>
<test-code>
<description>Annotations 2</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import foo.FooAnnotation1;
import foo.FooAnnotation2;
@FooAnnotation1
@FooAnnotation2
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>import from default package</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>Unnecessary import from the current package 'Bar'</message>
</expected-messages>
<code><![CDATA[
import Bar;
public class Foo {
public Bar foo() {}
}
]]></code>
</test-code>
<test-code>
<description>import from default package from somewhere else</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package somewhere;
import Bar;
public class Foo {
public Bar foo() {}
}
]]></code>
</test-code>
<test-code>
<description>import from default package</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
package somewhere;
import Bar;
public class Foo {
public void foo() {}
}
]]></code>
</test-code>
<test-code disabled="true">
<!-- This doesn't work anymore since the import is unresolved -->
<description>Used static import</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import static com.foo.MyClass.staticMethod;
public class Foo {
public Foo(){
staticMethod("This is OK");
}
}
]]></code>
</test-code>
<test-code>
<description>Unused static import</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
import static com.foo.MyClass.staticMethod;
public class Foo {
public Foo() {
}
}
]]></code>
</test-code>
<test-code>
<description>On demand import</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.*;
public class Foo {
List list = new ArrayList();
}
]]></code>
</test-code>
<test-code>
<description>imports used in javadoc comment, see also bug #254</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.LinkedList;
import java.util.File;
import java.util.NoSuchElementException;
import java.io.IOException;
public class Foo {
/**
* {@linkplain List list}
* {@link ArrayList arraylist}
* {@link LinkedList}
* {@value Calendar#DATE}
* @see File
* @throws NoSuchElementException no such element
* @exception IOException IO operation exception
*/
public void test() {}
}
]]></code>
</test-code>
<test-code>
<description>#1280 False Positive in UnusedImports when import used in javadoc</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import some.pack.SomeUtility;
public class Foo {
/**
* The {@link SomeUtility#someHelperMethod(String, SomeObject)} method does something.
*/
public void someMethod() {
}
}
]]></code>
</test-code>
<test-code>
<description>#1720 False Positive in UnusedImports for Javadoc link with array type</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import some.pack.SomeUtility;
public class Foo {
/**
* The {@link SomeUtility#someHelperMethod(String, SomeObjectArray[])} method does something.
*/
public void someMethod() {
}
}
]]></code>
</test-code>
<test-code>
<description>Bug 2606609 : False "UnusedImports" positive in package-info.java</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
@DefaultAnnotation(NonNull.class)
package net.sourceforge.pmd.test;
import edu.umd.cs.findbugs.annotations.DefaultAnnotation;
import edu.umd.cs.findbugs.annotations.NonNull;
]]></code>
</test-code>
<test-code>
<description>bug #254 False+ : UnusedImport with Javadoc @link</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.ArrayList;
import java.util.Collections;
import java.util.NoSuchElementException;
public class Test {
/**
* {@link ArrayList#add(Object)}
* @see Collections
* @throws NoSuchElementException no such element
*/
String[] getList() { }
}
]]></code>
</test-code>
<test-code>
<description>#1181 unused import false positive if used as parameter in javadoc only.</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
public class Foo {
/**
* See here: {@link Collections#reverse(List, Collection)}
* @see Collections#reverse(ArrayList, Vector)
*/
public Object bar() {}
}
]]></code>
</test-code>
<test-code>
<description>#1280 False Positive in UnusedImports when import used in javadoc</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import some.pack.SomeUtility;
public class Foo {
/**
* The {@link SomeUtility#someHelperMethod(String, test.SomeObject)} method does something.
*/
public void someMethod() {
}
}
]]></code>
</test-code>
<test-code>
<description>#914 False +ve from UnusedImports with wildcard static imports</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import static org.junit.Assert.*;
import org.junit.Test;
import static java.lang.Math.*;
public class FooTest {
@Test
public void testStuff() {
assertEquals( "a", "a" );
assertFalse( false );
assertTrue( true );
assertNull( null );
}
public static void main(String[] args) {
System.out.println(PI);
}
}
]]></code>
</test-code>
<test-code>
<description>#1465 False Positve UnusedImports with javadoc @link</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import a.b.c.d.AgentState;
import a.b.c.d.OtherState;
public interface Foo {
/**
* Gets all active AgentHandles.
* <p/>
* An agent is active if it has not posted a {@link AgentStateChangeEvent} containing {@link AgentState#TERMINATED}.
*
* @return agent handles.
* @see OtherState#TERMINATED
*/
Iterable<AgentHandle> getAgentHandles();
}
]]></code>
</test-code>
<test-code>
<description>#1547 False Positve UnusedImports with javadoc for identifiers with underscores</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.net.HttpURLConnection;
import foo.bar.Some_Thing;
import foo.bar.Under_Score_Exception;
import foo.bar.Fixed_Values;
/**
* {@link HttpURLConnection#HTTP_NOT_ACCEPTABLE}
*
* {@value Fixed_Values#FOO_BAR}
*
* @see Some_Thing#SOMETHING_ELSE
*/
public interface Interface {
/**
* @throws Under_Score_Exception
*/
void doSomething();
}
]]></code>
</test-code>
<test-code>
<description>#348 False Positive UnusedImports with javadoc for public static inner classes of imports</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import javax.swing.GroupLayout;
public class Foo {
/**
* {@link Bar#doSomething(GroupLayout.Group)}
*/
void doSomething();
}
]]></code>
</test-code>
<test-code>
<description>#925 [java] UnusedImports false positive for static import</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.ClassWithConstants.*;
public class ClassWithImport {
public static void main(String[] args) {
System.out.println("List 1: " + LIST1);
System.out.println("List 2: " + LIST2);
}
}
]]></code>
</test-code>
<test-code>
<description>#1404 [java] UnusedImports false positive for static import</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.ClassWithStringConstants.*;
public class ClassWithImport {
public static void main(String[] args) {
if (CONST1.equals("a")) {
System.out.println("CONST1 is a");
}
}
}
]]></code>
</test-code>
<test-code>
<description>#1209 [java] UnusedImports false positive for static import with package-private method usage</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.PackagePrivateUtils.*;
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.PublicUtils.*;
public class Imports {
int importtest() {
int i = 0;
i = f1(i);
i = g1(i);
i = f2(i);
i = g2(i);
i = f3(i);
i = g3(i);
return i;
}
}
]]></code>
</test-code>
<test-code>
<description>#1625 [java] UnusedImports false positive for method parameter type in @see Javadoc</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
import java.io.File;
import java.io.FileInputStream;
public class Derived extends FileInputStream {
/** @see #FileInputStream(File) */
void main() {}
}
]]></code>
</test-code>
<test-code>
<description>#1720 False Positive in UnusedImports for Javadoc @see with array type</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
import java.io.File;
import java.io.FileInputStream;
public class Derived extends FileInputStream {
/** @see #FileInputStream(File, File[]) */
void main() {}
}
]]></code>
</test-code>
<test-code>
<description>#2025 False Positive in UnusedImports for params when using @see with FQCN</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import spark.Response; // flaged, should not
/**
* @see foo.bar.MyController#startTransaction(Response)
*/
public class VendingV2PaymentRequest {
}
]]></code>
</test-code>
<test-code>
<description>#2025 False Positive in UnusedImports for params when using @link with FQCN</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import spark.Request; // flaged, should not
/**
* {@link foo.bar.MyController#startTransaction(Request)}
*/
public class VendingV2PaymentRequest {
}
]]></code>
</test-code>
<test-code>
<description>#2016 [java] UnusedImports: False positive if wildcard is used and only static methods</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
// star import is important here for the test case!!
import java.util.*;
/**
* Note: In order for this test case to work, the class "Issue2016" must also be compiled and available
* on the auxclasspath.
*/
public class Issue2016 {
public void testFunction() {
Objects.toString(null);
}
}
]]></code>
</test-code>
<test-code>
<description>resolve ambiguous static on-demand imports (#2277)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import static java.lang.Integer.*;
import static java.lang.Long.valueOf;
import static java.lang.Long.*;
public class Foo {
public void foo() {
// the "valueOf" method is both in Integer.* and Long.*
// we need an explicit static import to specify one of them, e.g. Long.valueOf
valueOf("123", 10);
// covered by Integer.*
int i = parseInt("123");
// covered by Long.*
long l = parseLong("123");
}
}
]]></code>
</test-code>
<test-code>
<description>[java] UnusedImports with static imports on subclasses #3132</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.HelloMore.*; // flagged as unused
public class Test {
public static void main(String... args) {
sayHello();
}
}
]]></code>
</test-code>
<test-code>
<description>Unused import with member inherited AND imported</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport;
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.HelloMore.sayHello;
public class Test extends HelloMore {
public static void main(String... args) {
// import is unused as the method is inherited here
sayHello();
}
}
]]></code>
</test-code>
<test-code>
<description>NPE with static import on interface</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
import static javax.swing.WindowConstants.*; //warn
class NPEImport {}
]]></code>
</test-code>
<!-- Test cases for duplicate imports -->
<test-code>
<description>duplicate single type imports</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>2,3</expected-linenumbers>
<expected-messages>
<message>Unused import 'java.util.*'</message>
<message>Duplicate import 'java.io.File'</message>
</expected-messages>
<code><![CDATA[
import java.io.File;
import java.util.*;
import java.io.File;
public class Foo {
File f;
}
]]></code>
</test-code>
<test-code>
<description>duplicate wildcard imports</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>2</expected-linenumbers>
<expected-messages>
<message>Duplicate import 'java.io.*'</message>
</expected-messages>
<code><![CDATA[
import java.io.*;
import java.io.*;
public class Foo {
File f;
}
]]></code>
</test-code>
<test-code>
<description>single type import after wildcard import</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>1</expected-linenumbers>
<expected-messages>
<message>Unused import 'java.io.*'</message>
</expected-messages>
<code><![CDATA[
import java.io.*;
import java.io.File;
public class Foo {
File f;
}
]]></code>
</test-code>
<test-code>
<description>subpackage import, ok</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.util.*;
import java.util.logging.*;
public class Foo {
List c; Logger f;
}
]]></code>
</test-code>
<test-code>
<description>674394, disambiguation import should be allowed</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.awt.*;
import java.util.*;
import java.util.List; //False positive
class Foo{
Color color;
List list;
Set set;
}
]]></code>
</test-code>
<test-code>
<description>674394, disambiguation import because of conflict with java.lang</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>1</expected-linenumbers>
<code><![CDATA[
import foo.*;
import foo.System; //False positive
class Foo {
System system; //No, I do not mean java.lang.System
}
]]></code>
</test-code>
<test-code>
<description>#1306 False positive on duplicate when using static imports</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import static org.junit.Assert.*;
import static net.sourceforge.pmd.lang.java.rule.codestyle.UnnecessaryImportTest.*;
import static org.junit.Assert.assertTrue;
// this import is needed for disambiguation - as DuplicateImportsTest
// defines assertTrue with the same signature, too.
public class DuplicateImports {
static {
assertTrue("", true); // the one from the disambiguation import
assertSomething("", true); // from UnnecessaryImportTest.*
assertFalse("", true); // from Assert.*
}
}
]]></code>
</test-code>
<test-code>
<description>Static on-demand import is used</description>
<expected-problems>0</expected-problems>
<!-- Technically we could report assertTrue, but for now we don't. -->
<code><![CDATA[
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
public class DuplicateImports {
static {
assertTrue("", true);
assertFalse("", true);
}
}
]]></code>
</test-code>
<test-code>
<description>[java] similar imports static/nonstatic with method #2546</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>2</expected-linenumbers>
<expected-messages>
<message>Unused import 'net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*'</message>
</expected-messages>
<code><![CDATA[
// imports NonStatic -- unused
import net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*;
// imports foo -- used
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*;
public class DuplicateImports {
static {
foo();
}
}
]]></code>
</test-code>
<test-code>
<description>[java] #2546 -- similar imports static/nonstatic - static is used</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>2</expected-linenumbers>
<expected-messages>
<message>Unused import 'net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*'</message>
</expected-messages>
<code><![CDATA[
// imports NonStatic -- unused
import net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*;
// imports Static
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*;
public class DuplicateImports {
static {
Static b;
}
}
]]></code>
</test-code>
<test-code>
<description>[java] #2546 -- similar imports static/nonstatic - static is unused</description>
<expected-problems>1</expected-problems>
<expected-linenumbers>4</expected-linenumbers>
<expected-messages>
<message>Unused static import 'net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*'</message>
</expected-messages>
<code><![CDATA[
// imports NonStatic
import net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*;
// imports Static -- unused
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*;
public class DuplicateImports {
static {
NonStatic b;
}
}
]]></code>
</test-code>
<test-code>
<description>[java] #2546 -- similar imports static/nonstatic - both are used</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
// imports NonStatic
import net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*;
// imports Static
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*;
public class DuplicateImports {
static {
// both are used
NonStatic a;
Static b;
}
}
]]></code>
</test-code>
<test-code>
<description>[java] #2546 -- similar imports static/nonstatic - both are used (method)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
// imports NonStatic
import net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*;
// imports foo()
import static net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.NonStaticContainer.*;
public class DuplicateImports {
static {
// both are used
NonStatic a;
foo();
}
}
]]></code>
</test-code>
<test-code>
<description>ImportFromSamePackage: simple failure</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
package foo;
import foo.Bar;
public class Baz {
Bar bar;
}
]]></code>
</test-code>
<test-code>
<description>ImportFromSamePackage: class in default package importing from sub package</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
package foo;
import foo.buz.Bar;
public class Baz{
Bar b;
}
]]></code>
</test-code>
<test-code>
<description>ImportFromSamePackage: importing all from same package</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>Unused import 'foo.bar.*'</message>
</expected-messages>
<code><![CDATA[
package foo.bar;
import foo.bar.*;
public class Baz{}
]]></code>
</test-code>
<!-- From DontImportJavaLang -->
<test-code>
<description>import java.lang.String (used)</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>Unnecessary import from the java.lang package 'java.lang.String'</message>
</expected-messages>
<code><![CDATA[
import java.lang.String;
public class Foo {
String foo;
}
]]></code>
</test-code>
<test-code>
<description>import java.lang.String (unused)</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>Unused import 'java.lang.String'</message>
</expected-messages>
<code><![CDATA[
import java.lang.String;
public class Foo { }
]]></code>
</test-code>
<test-code>
<description>import java.lang type when it shadows a same package type</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
// this package redefines a String type
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.javalang;
import java.lang.String;
public class Foo {
String foo;
}
]]></code>
</test-code>
<test-code>
<description>import java.lang type when it shadows a same package type (unused)</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>Unused import 'java.lang.String'</message>
</expected-messages>
<code><![CDATA[
// this package redefines a String type
package net.sourceforge.pmd.lang.java.rule.codestyle.unnecessaryimport.javalang;
import java.lang.String;
public class Foo { }
]]></code>
</test-code>
<test-code>
<description>import java.lang.*</description>
<expected-problems>1</expected-problems>
<code><![CDATA[
import java.lang.*;
public class Foo {}
]]></code>
</test-code>
<test-code>
<description>import java.lang.ref/reflect/annotation/instrument/management</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.lang.ref.*;
import java.lang.reflect.*;
import java.lang.annotation.*;
import java.lang.management.*;
public class Foo {
Annotation a; Method m; WeakReference r;
RuntimeMXBean x;
}
]]></code>
</test-code>
<test-code>
<description>Static Java imports are OK</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import static java.lang.String.*;
public class Foo {
static {
valueOf(1);
}
}
]]></code>
</test-code>
<test-code>
<description>Importing java.lang.Thread.UncaughtExceptionHandler, unused</description>
<expected-problems>1</expected-problems>
<expected-messages>
<message>Unused import 'java.lang.Thread.UncaughtExceptionHandler'</message>
</expected-messages>
<code><![CDATA[
import java.lang.Thread.UncaughtExceptionHandler;
public class Foo {
Thread.UncaughtExceptionHandler unused;
}
]]></code>
</test-code>
<test-code>
<description>Importing java.lang.Thread.UncaughtExceptionHandler</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
import java.lang.Thread.UncaughtExceptionHandler;
public class Foo {