forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config_imports.xml
2577 lines (2399 loc) · 102 KB
/
config_imports.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"?>
<document xmlns="http://maven.apache.org/XDOC/2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 https://maven.apache.org/xsd/xdoc-2.0.xsd">
<head>
<title>Imports</title>
</head>
<body>
<section name="Content">
<macro name="toc">
<param name="fromDepth" value="1"/>
<param name="toDepth" value="1"/>
</macro>
</section>
<section name="AvoidStarImport">
<p>Since Checkstyle 3.0</p>
<subsection name="Description" id="AvoidStarImport_Description">
<p>
Checks that there are no import statements that use the <code>*</code> notation.
</p>
<p>
Rationale: Importing all classes from a package or static
members from a class leads to tight coupling between packages
or classes and might lead to problems when a new version of a
library introduces name clashes.
</p>
</subsection>
<subsection name="Notes" id="AvoidStarImport_Notes">
<p>
Note that property <code>excludes</code> is not recursive,
subpackages of excluded packages are not automatically excluded.
</p>
</subsection>
<subsection name="Properties" id="AvoidStarImport_Properties">
<div class="wrapper">
<table>
<tr>
<th>name</th>
<th>description</th>
<th>type</th>
<th>default value</th>
<th>since</th>
</tr>
<tr>
<td>excludes</td>
<td>
Specify packages where starred class imports are
allowed and classes where starred static member imports are
allowed.
</td>
<td><a href="property_types.html#String.5B.5D">String[]</a></td>
<td><code>{}</code></td>
<td>3.2</td>
</tr>
<tr>
<td>allowClassImports</td>
<td>
Control whether to allow starred class imports like
<code>import java.util.*;</code>.
</td>
<td><a href="property_types.html#boolean">boolean</a></td>
<td><code>false</code></td>
<td>5.3</td>
</tr>
<tr>
<td>allowStaticMemberImports</td>
<td>
Control whether to allow starred static member imports like
<code>import static org.junit.Assert.*;</code>.
</td>
<td><a href="property_types.html#boolean">boolean</a></td>
<td><code>false</code></td>
<td>5.3</td>
</tr>
</table>
</div>
</subsection>
<subsection name="Examples" id="AvoidStarImport_Examples">
<p>
To configure the check:
</p>
<source>
<module name="AvoidStarImport"/>
</source>
<p>
Example:
</p>
<source>
import java.util.Scanner; // OK
import java.io.*; // violation
import static java.lang.Math.*; // violation
import java.util.*; // violation
import java.net.*; // violation
</source>
<p>
To configure the check so that star imports from packages
<code>java.io and java.net</code> as well as static members from class
<code>java.lang.Math</code> are allowed:
</p>
<source>
<module name="AvoidStarImport">
<property name="excludes" value="java.io,java.net,java.lang.Math"/>
</module>
</source>
<p>
Example:
</p>
<source>
import java.util.Scanner; // OK
import java.io.*; // OK
import static java.lang.Math.*; // OK
import java.util.*; // violation
import java.net.*; // OK
</source>
<p>
To configure the check so that star imports
from all packages are allowed:
</p>
<source>
<module name="AvoidStarImport">
<property name="allowClassImports" value="true"/>
</module>
</source>
<p>
Example:
</p>
<source>
import java.util.Scanner; // OK
import java.io.*; // OK
import static java.lang.Math.*; // violation
import java.util.*; // OK
import java.net.*; // OK
</source>
<p>
To configure the check so that starred static member imports
from all packages are allowed:
</p>
<source>
<module name="AvoidStarImport">
<property name="allowStaticMemberImports" value="true"/>
</module>
</source>
<p>
Example:
</p>
<source>
import java.util.Scanner; // OK
import java.io.*; // violation
import static java.lang.Math.*; // OK
import java.util.*; // violation
import java.net.*; // violation
</source>
<p>
To configure the check so that star imports from packages
<code>java.io and java.net</code> are allowed:
</p>
<source>
<module name="AvoidStarImport">
<property name="allowClassImports" value="true"/>
<property name="excludes" value="java.io,java.net"/>
</module>
</source>
<p>
Example:
</p>
<source>
import java.util.Scanner; // OK
import java.io.*; // OK
import static java.lang.Math.*; // violation
import java.util.*; // OK
import java.net.*; // OK
</source>
<p>
To configure the check so that star imports from packages
<code>java.io and java.net</code> as well as static members imports
from all packages are allowed:
</p>
<source>
<module name="AvoidStarImport">
<property name="allowStaticMemberImports" value="true"/>
<property name="excludes" value="java.io,java.net"/>
</module>
</source>
<p>
Example:
</p>
<source>
import java.util.Scanner; // OK
import java.io.*; // OK
import static java.lang.Math.*; // OK
import java.util.*; // violation
import java.net.*; // OK
</source>
</subsection>
<subsection name="Example of Usage" id="AvoidStarImport_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+AvoidStarImport">
Google Style</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Asun_checks.xml+repo%3Acheckstyle%2Fcheckstyle+AvoidStarImport">
Sun Style</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+AvoidStarImport">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="AvoidStarImport_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fimports+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22import.avoidStar%22">
import.avoidStar</a>
</li>
</ul>
<p>
All messages can be customized if the default message doesn't suit you.
Please <a href="config.html#Custom_messages">see the documentation</a> to learn how to.
</p>
</subsection>
<subsection name="Package" id="AvoidStarImport_Package">
<p> com.puppycrawl.tools.checkstyle.checks.imports </p>
</subsection>
<subsection name="Parent Module" id="AvoidStarImport_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="AvoidStaticImport">
<p>Since Checkstyle 5.0</p>
<subsection name="Description" id="AvoidStaticImport_Description">
<p>
Checks that there are no static import statements.
</p>
<p>
Rationale: Importing static members can lead to naming
conflicts between class' members. It may lead to poor code
readability since it may no longer be clear what class a
member resides in (without looking at the import statement).
</p>
</subsection>
<subsection name="Notes" id="AvoidStaticImport_Notes">
<p>
If you exclude a starred import on a class this automatically
excludes each member individually.
</p>
<p>
For example: Excluding <code>java.lang.Math.*</code>. will allow the
import of each static member in the Math class
individually like <code>java.lang.Math.PI, java.lang.Math.cos, ...</code>.
</p>
</subsection>
<subsection name="Properties" id="AvoidStaticImport_Properties">
<div class="wrapper">
<table>
<tr>
<th>name</th>
<th>description</th>
<th>type</th>
<th>default value</th>
<th>since</th>
</tr>
<tr>
<td>excludes</td>
<td>
Control whether to allow for certain classes via a star notation to be
excluded such as <code>java.lang.Math.*</code> or specific static
members to be excluded like <code>java.lang.System.out</code> for a variable or
<code>java.lang.Math.random</code> for a method. See notes section for details.
</td>
<td><a href="property_types.html#String.5B.5D">String[]</a></td>
<td><code>{}</code></td>
<td>5.0</td>
</tr>
</table>
</div>
</subsection>
<subsection name="Examples" id="AvoidStaticImport_Examples">
<p>
To configure the check:
</p>
<source>
<module name="AvoidStaticImport"/>
</source>
<p>
Example:
</p>
<source>
import static java.lang.Math.pow; // violation
import static java.lang.System.*; // violation
import java.io.File; // OK
import java.util.*; // OK
</source>
<p>
To configure the check so that the <code>java.lang.System.out</code>
member and all members from <code>java.lang.Math</code> are allowed:
</p>
<source>
<module name="AvoidStaticImport">
<property name="excludes" value="java.lang.System.out,java.lang.Math.*"/>
</module>
</source>
<p>
Example:
</p>
<source>
import static java.lang.Math.*; // OK
import static java.lang.System.out; // OK
import static java.lang.Integer.parseInt; // violation
import java.io.*; // OK
import java.util.*; // OK
</source>
</subsection>
<subsection name="Example of Usage" id="AvoidStaticImport_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+AvoidStaticImport">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="AvoidStaticImport_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fimports+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22import.avoidStatic%22">
import.avoidStatic</a>
</li>
</ul>
<p>
All messages can be customized if the default message doesn't suit you.
Please <a href="config.html#Custom_messages">see the documentation</a> to learn how to.
</p>
</subsection>
<subsection name="Package" id="AvoidStaticImport_Package">
<p> com.puppycrawl.tools.checkstyle.checks.imports </p>
</subsection>
<subsection name="Parent Module" id="AvoidStaticImport_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="CustomImportOrder">
<p>Since Checkstyle 5.8</p>
<subsection name="Description" id="CustomImportOrder_Description">
<p>
Checks that the groups of import declarations appear in the order specified
by the user. If there is an import but its group is not specified in the
configuration such an import should be placed at the end of the import list.
</p>
</subsection>
<subsection name="Rule Description" id="CustomImportOrder_Rule_Description">
<p>
The rule consists of:
</p>
<ol>
<li>
STATIC group. This group sets the ordering of static imports.
</li>
<li>
SAME_PACKAGE(n) group. This group sets the ordering of the same package imports.
Imports are considered on SAME_PACKAGE group if <b>n</b> first domains in package
name and import name are identical:
<source>
package java.util.concurrent.locks;
import java.io.File;
import java.util.*; //#1
import java.util.List; //#2
import java.util.StringTokenizer; //#3
import java.util.concurrent.*; //#4
import java.util.concurrent.AbstractExecutorService; //#5
import java.util.concurrent.locks.LockSupport; //#6
import java.util.regex.Pattern; //#7
import java.util.regex.Matcher; //#8
</source>
If we have SAME_PACKAGE(3) on configuration file,
imports #4-6 will be considered as a SAME_PACKAGE group (java.util.concurrent.*,
java.util.concurrent.AbstractExecutorService, java.util.concurrent.locks.LockSupport).
SAME_PACKAGE(2) will include #1-8. SAME_PACKAGE(4) will include only #6.
SAME_PACKAGE(5) will result in no imports assigned to SAME_PACKAGE group because
actual package java.util.concurrent.locks has only 4 domains.
</li>
<li>
THIRD_PARTY_PACKAGE group. This group sets ordering of third party imports.
Third party imports are all imports except STATIC, SAME_PACKAGE(n),
STANDARD_JAVA_PACKAGE and SPECIAL_IMPORTS.
</li>
<li>
STANDARD_JAVA_PACKAGE group. By default, this group sets ordering of standard
java/javax imports.
</li>
<li>
SPECIAL_IMPORTS group. This group may contain some imports that have particular
meaning for the user.
</li>
</ol>
</subsection>
<subsection name="Notes" id="CustomImportOrder_Notes">
<p>
Use the separator '###' between rules.
</p>
<p>
To set RegExps for THIRD_PARTY_PACKAGE and STANDARD_JAVA_PACKAGE groups use
thirdPartyPackageRegExp and standardPackageRegExp options.
</p>
<p>
Pretty often one import can match more than one group. For example, static import
from standard package or regular expressions are configured to allow one import match
multiple groups. In this case, group will be assigned according to priorities:
</p>
<ol>
<li>STATIC has top priority</li>
<li>SAME_PACKAGE has second priority</li>
<li>STANDARD_JAVA_PACKAGE and SPECIAL_IMPORTS will compete using "best match" rule: longer
matching substring wins; in case of the same length, lower position of matching substring
wins; if position is the same, order of rules in configuration solves the puzzle.</li>
<li>THIRD_PARTY has the least priority</li>
</ol>
<p>
Few examples to illustrate "best match":
</p>
<p>
1. patterns STANDARD_JAVA_PACKAGE = "Check", SPECIAL_IMPORTS="ImportOrderCheck"
and input file:
</p>
<source>
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck;
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck;
</source>
<p>
Result: imports will be assigned to SPECIAL_IMPORTS, because matching
substring length is 16. Matching substring for STANDARD_JAVA_PACKAGE is 5.
</p>
<p>
2. patterns STANDARD_JAVA_PACKAGE = "Check", SPECIAL_IMPORTS="Avoid" and file:
</p>
<source>
import com.puppycrawl.tools.checkstyle.checks.imports.AvoidStarImportCheck;
</source>
<p>
Result: import will be assigned to SPECIAL_IMPORTS. Matching substring length is 5 for
both patterns. However, "Avoid" position is lower than "Check" position.
</p>
</subsection>
<subsection name="Properties" id="CustomImportOrder_Properties">
<div class="wrapper">
<table>
<tr>
<th>name</th>
<th>description</th>
<th>type</th>
<th>default value</th>
<th>since</th>
</tr>
<tr>
<td>customImportOrderRules</td>
<td>Specify format of order declaration customizing by user.</td>
<td><a href="property_types.html#String">String</a></td>
<td><code>""</code></td>
<td>5.8</td>
</tr>
<tr>
<td>standardPackageRegExp</td>
<td>Specify RegExp for STANDARD_JAVA_PACKAGE group imports.</td>
<td><a href="property_types.html#Pattern">Pattern</a></td>
<td><code>"^(java|javax)\."</code></td>
<td>5.8</td>
</tr>
<tr>
<td>thirdPartyPackageRegExp</td>
<td>Specify RegExp for THIRD_PARTY_PACKAGE group imports.</td>
<td><a href="property_types.html#Pattern">Pattern</a></td>
<td><code>".*"</code></td>
<td>5.8</td>
</tr>
<tr>
<td>specialImportsRegExp</td>
<td>Specify RegExp for SPECIAL_IMPORTS group imports.</td>
<td><a href="property_types.html#Pattern">Pattern</a></td>
<td><code>"^$"</code></td>
<td>5.8</td>
</tr>
<tr>
<td>separateLineBetweenGroups</td>
<td>Force empty line separator between import groups.</td>
<td><a href="property_types.html#boolean">boolean</a></td>
<td><code>true</code></td>
<td>5.8</td>
</tr>
<tr>
<td>sortImportsInGroupAlphabetically</td>
<td>Force grouping alphabetically, in
<a href="https://en.wikipedia.org/wiki/ASCII#Order">
ASCII sort order</a>.
</td>
<td><a href="property_types.html#boolean">boolean</a></td>
<td><code>false</code></td>
<td>5.8</td>
</tr>
</table>
</div>
</subsection>
<subsection name="Examples" id="CustomImportOrder_Examples">
<p>
To configure the check :
</p>
<source>
<module name="CustomImportOrder"/>
</source>
<p>
Example:
</p>
<source>
package com.company;
import org.apache.commons.io.FileUtils; // OK
import static java.util.*; // OK
import java.time.*; // OK
import static java.io.*; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
</source>
<p>
To configure the check so that it checks in the order
(static imports,standard java packages,third party package):
</p>
<source>
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="STATIC###STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE"/>
</module>
</source>
<p>
Example:
</p>
<source>
package com.company;
import static java.util.*; // OK
import java.time.*; // OK
import javax.net.*; // OK
import static java.io.*; // violation as static imports should be in top
import org.apache.commons.io.FileUtils; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
</source>
<p>
To configure the check such that only java packages are included in standard java packages
</p>
<source>
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="STATIC###STANDARD_JAVA_PACKAGE###THIRD_PARTY_PACKAGE"/>
<property name="standardPackageRegExp" value="^java\."/>
</module>
</source>
<p>
Example:
</p>
<source>
package com.company;
import static java.util.*; // OK
import static java.io.*; // OK
import java.time.*; // OK
import javax.net.*; // violation as it is not included in standard java package group.
import org.apache.commons.io.FileUtils; // violation
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
</source>
<p>
To configure the check to include only "com" packages as third party group imports:
</p>
<source>
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="STATIC###STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS###THIRD_PARTY_PACKAGE"/>
<property name="thirdPartyPackageRegExp" value="^com\."/>
</module>
</source>
<p>
Example:
</p>
<source>
package com.company;
import static java.util.*; // OK
import static java.io.*; // OK
import java.time.*; // OK
import javax.net.*; // OK
import org.apache.commons.io.FileUtils; // violation(should be in end)
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // violation
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
</source>
<p>
To configure the check to force some packages in special import group:
</p>
<source>
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="STATIC###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE"/>
<property name="specialImportsRegExp" value="^org\."/>
</module>
</source>
<p>
Example:
</p>
<source>
package com.company;
import static java.util.*; // OK
import static java.io.*; // OK
import org.json.JSONObject; // OK
import java.time.*; // OK
import javax.net.*; // OK
import org.apache.commons.io.FileUtils; // violation
</source>
<p>
To configure the check such that empty line separator between two groups is enabled:
</p>
<source>
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="STATIC###STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS###THIRD_PARTY_PACKAGE"/>
<property name="specialImportsRegExp" value="^org\."/>
<property name="thirdPartyPackageRegExp" value="^com\."/>
<property name="separateLineBetweenGroups" value="true"/>
</module>
</source>
<p>
Example:
</p>
<source>
package com.company;
import static java.util.*; // OK
import static java.io.*; // OK
import java.time.*; // OK
import javax.net.*; // OK
import org.apache.commons.io.FileUtils; // violation
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // violation
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
</source>
<p>
To configure the check such that import groups are forced to be sorted alphabetically:
</p>
<source>
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="STATIC###STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS###THIRD_PARTY_PACKAGE"/>
<property name="specialImportsRegExp" value="^org\."/>
<property name="thirdPartyPackageRegExp" value="^com\."/>
<property name="separateLineBetweenGroups" value="false"/>
<property name="sortImportsInGroupAlphabetically" value="true"/>
</module>
</source>
<p>
Example:
</p>
<source>
package com.company;
import static java.util.*; // OK
import static java.io.*; // Violation since it should come before"java.util"
import java.time.*; // OK
import javax.net.*; // OK
import org.apache.commons.io.FileUtils; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
</source>
<p>To configure the check so that it matches default Eclipse formatter configuration
(tested on Kepler and Luna releases):</p>
<ul>
<li>group of static imports is on the top</li>
<li>groups of non-static imports: "java" and "javax" packages first,
then "org" and then all other imports</li>
<li>imports will be sorted in the groups</li>
<li>groups are separated by single blank line</li>
</ul>
<p>Notes:</p>
<ul>
<li>"com" package is not mentioned on configuration, because it is
ignored by Eclipse Kepler and Luna (looks like Eclipse defect)</li>
<li>configuration below doesn't work in all 100% cases due to inconsistent behavior
prior to Mars release, but covers most scenarios</li>
</ul>
<source>
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="STATIC###STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS"/>
<property name="specialImportsRegExp" value="^org\."/>
<property name="sortImportsInGroupAlphabetically" value="true"/>
<property name="separateLineBetweenGroups" value="true"/>
</module>
</source>
<p>
Example:
</p>
<source>
package com.company;
import static java.util.*; // OK
import static java.io.*; // Violation since it should come before"java.util"
import java.time.*; // OK
import javax.net.*; // OK
import org.apache.commons.io.FileUtils; // Violation should be separated by space
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
</source>
<p>To configure the check so that it matches default Eclipse formatter configuration
(tested on Mars release):</p>
<ul>
<li>group of static imports is on the top</li>
<li>groups of non-static imports: "java" and "javax" packages first,
then "org" and "com", then all other imports as one group</li>
<li>imports will be sorted in the groups</li>
<li>groups are separated by one blank line</li>
</ul>
<source>
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="STATIC###STANDARD_JAVA_PACKAGE###SPECIAL_IMPORTS###THIRD_PARTY_PACKAGE"/>
<property name="specialImportsRegExp" value="^org\."/>
<property name="thirdPartyPackageRegExp" value="^com\."/>
<property name="sortImportsInGroupAlphabetically" value="true"/>
<property name="separateLineBetweenGroups" value="true"/>
</module>
</source>
<p>
Example:
</p>
<source>
package com.company;
import static java.io.*; // OK
import static java.util.*; // OK
import java.time.*; // OK
import javax.net.*; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // Violation
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // Violation
import org.apache.commons.io.FileUtils;
</source>
<p>To configure the check so that it matches default IntelliJ IDEA formatter configuration
(tested on v14):</p>
<ul>
<li>group of static imports is on the bottom</li>
<li>groups of non-static imports: all imports except of "javax" and
"java", then "javax" and "java"</li>
<li>imports will be sorted in the groups</li>
<li>groups are separated by one blank line</li>
</ul>
<p>
Note: "separated" option is disabled because IDEA default has blank line
between "java" and static imports, and no blank line between
"javax" and "java"
</p>
<source>
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="THIRD_PARTY_PACKAGE###SPECIAL_IMPORTS###STANDARD_JAVA_PACKAGE###STATIC"/>
<property name="specialImportsRegExp" value="^javax\."/>
<property name="standardPackageRegExp" value="^java\."/>
<property name="sortImportsInGroupAlphabetically" value="true"/>
<property name="separateLineBetweenGroups" value="false"/>
</module>
</source>
<p>
Example:
</p>
<source>
package com.company;
import static java.io.*; // OK
import static java.util.*; // OK
import java.time.*; // violation should be in standard package group
// below special import
import javax.net.*; // Violation should be in special import group
import org.apache.commons.io.FileUtils; // Violation should be in
// THIRD PARTY PACKAGE GROUP
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // Violation
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // Violation
</source>
<p>To configure the check so that it matches default NetBeans formatter configuration
(tested on v8):</p>
<ul>
<li>groups of non-static imports are not defined, all imports will be sorted as a one
group</li>
<li>static imports are not separated, they will be sorted along with other imports</li>
</ul>
<source>
<module name="CustomImportOrder"/>
</source>
<p>
Example:
</p>
<source>
package com.company;
import static java.io.*; // OK
import static java.util.*; // OK
import java.time.*; // OK
import javax.net.*; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
import org.apache.commons.io.FileUtils; // should not be separated by line
</source>
<p>
To set RegExps for THIRD_PARTY_PACKAGE and STANDARD_JAVA_PACKAGE groups use
thirdPartyPackageRegExp and standardPackageRegExp options.
</p>
<source>
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="STATIC###SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###STANDARD_JAVA_PACKAGE"/>
<property name="thirdPartyPackageRegExp" value="^(com|org)\."/>
<property name="standardPackageRegExp" value="^(java|javax)\."/>
</module>
</source>
<p>
Example:
</p>
<source>
package com.company;
import static java.io.*; // OK
import static java.util.*; // OK
import java.time.*; // violation
import javax.net.*; // violation
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
import org.apache.commons.io.FileUtils; // OK
</source>
<p>
Also, this check can be configured to force empty line separator between
import groups. For example.
</p>
<source>
<module name="CustomImportOrder">
<property name="separateLineBetweenGroups" value="true"/>
</module>
</source>
<p>
Example:
</p>
<source>
package com.company;
import static java.io.*; // OK
import static java.util.*; // OK
import java.time.*; // OK
import javax.net.*; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.CustomImportOrderCheck; // OK
import com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck; // OK
import org.apache.commons.io.FileUtils; // OK
</source>
<p>
It is possible to enforce <a href="https://en.wikipedia.org/wiki/ASCII#Order">
ASCII sort order</a>
of imports in groups using the following configuration:
</p>
<source>
<module name="CustomImportOrder">
<property name="sortImportsInGroupAlphabetically" value="true"/>
</module>
</source>
<p>
Example of ASCII order:
</p>
<source>
import java.awt.Dialog;
import java.awt.Window;
import java.awt.color.ColorSpace;
import java.awt.Frame; // violation here - in ASCII order 'F' should go before 'c',
// as all uppercase come before lowercase letters
</source>
<p>
To force checking imports sequence such as:
</p>
<source>
package com.puppycrawl.tools.checkstyle.imports;
import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.Beta;
import com.google.common.annotations.VisibleForTesting;
import org.abego.treelayout.Configuration;
import static sun.tools.util.ModifierFilter.ALL_ACCESS;
import com.google.common.annotations.GwtCompatible; // violation here - should be in the
// THIRD_PARTY_PACKAGE group
import android.*;
</source>
<p>
configure as follows:
</p>
<source>
<module name="CustomImportOrder">
<property name="customImportOrderRules"
value="SAME_PACKAGE(3)###THIRD_PARTY_PACKAGE###STATIC###SPECIAL_IMPORTS"/>
<property name="specialImportsRegExp" value="^android\."/>
</module>
</source>
</subsection>
<subsection name="Example of Usage" id="CustomImportOrder_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Agoogle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+CustomImportOrder">
Google Style</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+CustomImportOrder">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="CustomImportOrder_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fimports+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22custom.import.order%22">
custom.import.order</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fimports+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22custom.import.order.lex%22">
custom.import.order.lex</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fimports+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22custom.import.order.line.separator%22">
custom.import.order.line.separator</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fimports+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22custom.import.order.nonGroup.expected%22">
custom.import.order.nonGroup.expected</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fimports+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22custom.import.order.nonGroup.import%22">
custom.import.order.nonGroup.import</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fimports+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22custom.import.order.separated.internally%22">
custom.import.order.separated.internally</a>
</li>
</ul>
<p>
All messages can be customized if the default message doesn't suit you.
Please <a href="config.html#Custom_messages">see the documentation</a> to learn how to.
</p>