forked from checkstyle/checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 2
/
config_coding.xml
8437 lines (7775 loc) · 284 KB
/
config_coding.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>Coding</title>
</head>
<body>
<section name="Content">
<macro name="toc">
<param name="fromDepth" value="1"/>
<param name="toDepth" value="1"/>
</macro>
</section>
<section name="ArrayTrailingComma">
<p>Since Checkstyle 3.2</p>
<subsection name="Description" id="ArrayTrailingComma_Description">
<p>
Checks that array initialization contains a trailing comma.
</p>
<source>
int[] a = new int[]
{
1,
2,
3,
};
</source>
<p>
By default, the check demands a comma at the end if neither left nor right curly braces
are on the same line as the last element of the array.
</p>
<source>
return new int[] { 0 };
return new int[] { 0
};
return new int[] {
0 };
</source>
<p>
Rationale: Putting this comma in makes it easier to change the order
of the elements or add new elements on the end. Main benefit of a trailing
comma is that when you add new entry to an array, no surrounding lines are changed.
</p>
<source>
{
100000000000000000000,
200000000000000000000, // OK
}
{
100000000000000000000,
200000000000000000000,
300000000000000000000, // Just this line added, no other changes
}
</source>
<p>
If closing brace is on the same line as trailing comma, this benefit is gone
(as the check does not demand a certain location of curly braces the following
two cases will not produce a violation):
</p>
<source>
{100000000000000000000,
200000000000000000000,} // Trailing comma not needed, line needs to be modified anyway
{100000000000000000000,
200000000000000000000, // Modified line
300000000000000000000,} // Added line
</source>
<p>
If opening brace is on the same line as trailing comma there's also (more arguable)
problem:
</p>
<source>
{100000000000000000000, // Line cannot be just duplicated to slightly modify entry
}
{100000000000000000000,
100000000000000000001, // More work needed to duplicate
}
</source>
</subsection>
<subsection name="Properties" id="ArrayTrailingComma_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>alwaysDemandTrailingComma</td>
<td>
Control whether to always check for a trailing comma, even when an array is
inline.
</td>
<td><a href="property_types.html#boolean">boolean</a></td>
<td><code>false</code></td>
<td>8.33</td>
</tr>
</table>
</div>
</subsection>
<subsection name="Examples" id="ArrayTrailingComma_Examples">
<p>
To configure the check:
</p>
<source>
<module name="ArrayTrailingComma"/>
</source>
<p>
Which results in the following violations:
</p>
<source>
int[] numbers = {1, 2, 3}; //no violation
boolean[] bools = {
true,
true,
false
}; //violation
String[][] text = {{},{},}; //no violation
double[][] decimals = {
{0.5, 2.3, 1.1,}, //no violation
{1.7, 1.9, 0.6},
{0.8, 7.4, 6.5}
}; // violation as previous line misses a comma
char[] chars = {'a', 'b', 'c'
}; / /no violation
String[] letters = {
"a", "b", "c"}; // no violation
int[] a1 = new int[]{
1,
2
,
}; // no violation
int[] a2 = new int[]{
1,
2
,}; // no violation
</source>
<p>To configure check to always validate trailing comma:</p>
<source>
<module name="ArrayTrailingComma">
<property name="alwaysDemandTrailingComma" value="true"/>
</module>
</source>
<p>Example:</p>
<source>
int[] numbers = {1, 2, 3}; // violation
boolean[] bools = {
true,
true,
false // violation
};
String[][] text = {{},{},}; // OK
double[][] decimals = {
{0.5, 2.3, 1.1,}, // OK
{1.7, 1.9, 0.6}, // violation
{0.8, 7.4, 6.5} // violation
}; // violation, previous line misses a comma
char[] chars = {'a', 'b', 'c' // violation
};
String[] letters = {
"a", "b", "c"}; // violation
int[] a1 = new int[]{
1,
2
,
}; // OK
int[] a2 = new int[]{
1,
2
,}; // OK
</source>
</subsection>
<subsection name="Example of Usage" id="ArrayTrailingComma_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+ArrayTrailingComma">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="ArrayTrailingComma_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22array.trailing.comma%22">
array.trailing.comma</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="ArrayTrailingComma_Package">
<p>
com.puppycrawl.tools.checkstyle.checks.coding
</p>
</subsection>
<subsection name="Parent Module" id="ArrayTrailingComma_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="AvoidDoubleBraceInitialization">
<p>Since Checkstyle 8.30</p>
<subsection name="Description" id="AvoidDoubleBraceInitialization_Description">
<p>
Detects double brace initialization.
</p>
<p>
Rationale: Double brace initialization (set of
<a href="https://docs.oracle.com/javase/specs/jls/se12/html/jls-8.html#jls-8.6">
Instance Initializers</a> in class body) may look cool,
but it is considered as anti-pattern and should be avoided.
This is also can lead to a hard-to-detect memory leak, if the anonymous class instance is
returned outside and other object(s) hold reference to it.
Created anonymous class is not static, it holds an implicit reference to the outer class
instance.
See this
<a href="https://blog.jooq.org/dont-be-clever-the-double-curly-braces-anti-pattern/">
blog post</a> and
<a href="https://www.baeldung.com/java-double-brace-initialization">
article</a> for more details.
Check ignores any comments and semicolons in class body.
</p>
</subsection>
<subsection name="Examples" id="AvoidDoubleBraceInitialization_Examples">
<p>
To configure the check:
</p>
<source>
<module name="AvoidDoubleBraceInitialization"/>
</source>
<p>
Which results in the following violations:
</p>
<source>
class MyClass {
List<Integer> list1 = new ArrayList<>() { // violation
{
add(1);
}
};
List<String> list2 = new ArrayList<>() { // violation
;
// comments and semicolons are ignored
{
add("foo");
}
};
}
</source>
<p>
Check only looks for double brace initialization and it ignores cases
where the anonymous class has fields or methods.
Though these might create the same memory issues as double brace,
the extra class members can produce side effects if changed incorrectly.
</p>
<source>
class MyClass {
List<Object> list = new ArrayList<>() { // OK, not pure double brace pattern
private int field;
{
add(new Object());
}
};
}
</source>
</subsection>
<subsection name="Example of Usage" id="AvoidDoubleBraceInitialization_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+AvoidDoubleBraceInitialization">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="AvoidDoubleBraceInitialization_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22avoid.double.brace.init%22">
avoid.double.brace.init</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="AvoidDoubleBraceInitialization_Package">
<p>
com.puppycrawl.tools.checkstyle.checks.coding
</p>
</subsection>
<subsection name="Parent Module" id="AvoidDoubleBraceInitialization_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="AvoidInlineConditionals">
<p>Since Checkstyle 3.1</p>
<subsection name="Description" id="AvoidInlineConditionals_Description">
<p>
Detects inline conditionals. Here is one example of an inline conditional:
</p>
<source>
String a = getParameter("a");
String b = (a==null || a.length()<1) ? null : a.substring(1);
</source>
<p>
Rationale: Some developers find inline conditionals hard to read, so
their employer's coding standards forbid them.
</p>
</subsection>
<subsection name="Examples" id="AvoidInlineConditionals_Examples">
<p>
To configure the check:
</p>
<source>
<module name="AvoidInlineConditionals"/>
</source>
<p>Example:</p>
<source>
int x = 5;
boolean foobar = (x == 5); // OK
String text;
text = (text == null) ? "" : text; // violation
String b;
if (a != null && a.length() >= 1) { // OK
b = a.substring(1);
} else {
b = null;
}
b = (a != null && a.length() >= 1) ? a.substring(1) : null; // violation
</source>
</subsection>
<subsection name="Example of Usage" id="AvoidInlineConditionals_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+AvoidInlineConditionals">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="AvoidInlineConditionals_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22inline.conditional.avoid%22">
inline.conditional.avoid</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="AvoidInlineConditionals_Package">
<p>
com.puppycrawl.tools.checkstyle.checks.coding
</p>
</subsection>
<subsection name="Parent Module" id="AvoidInlineConditionals_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="AvoidNoArgumentSuperConstructorCall">
<p>Since Checkstyle 8.29</p>
<subsection name="Description" id="AvoidNoArgumentSuperConstructorCall_Description">
<p>
Checks if call to superclass constructor without arguments is present.
Such invocation is redundant because constructor body implicitly
begins with a superclass constructor invocation <code>super();</code>
See <a href="https://docs.oracle.com/javase/specs/jls/se13/html/jls-8.html#jls-8.8.7">
specification</a> for detailed information.
</p>
</subsection>
<subsection name="Examples" id="AvoidNoArgumentSuperConstructorCall_Examples">
<p>
To configure the check:
</p>
<source>
<module name="AvoidNoArgumentSuperConstructorCall"/>
</source>
<p>
Example of violations
</p>
<source>
class MyClass extends SomeOtherClass {
MyClass() {
super(); // violation
}
MyClass(int arg) {
super(arg); // OK, call with argument have to be explicit
}
MyClass(long arg) {
// OK, call is implicit
}
}
</source>
</subsection>
<subsection name="Example of Usage" id="AvoidNoArgumentSuperConstructorCall_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+AvoidNoArgumentSuperConstructorCall">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages"
id="AvoidNoArgumentSuperConstructorCall_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22super.constructor.call%22">
super.constructor.call</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="AvoidNoArgumentSuperConstructorCall_Package">
<p>
com.puppycrawl.tools.checkstyle.checks.coding
</p>
</subsection>
<subsection name="Parent Module" id="AvoidNoArgumentSuperConstructorCall_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="CovariantEquals">
<p>Since Checkstyle 3.2</p>
<subsection name="Description" id="CovariantEquals_Description">
<p>
Checks that classes and records which define a covariant <code>equals()</code> method
also override method <code>equals(Object)</code>.
</p>
<p>
Covariant <code>equals()</code> - method that is similar to <code>equals(Object)</code>,
but with a covariant parameter type (any subtype of Object).
</p>
<p>
<strong>Notice</strong>: the enums are also checked, even
though they cannot override <code>equals(Object)</code>. The reason is
to point out that implementing <code>equals()</code> in enums is considered an
awful practice: it may cause having two different enum values that are equal using
covariant enum method, and not equal when compared normally.
</p>
<p>
Inspired by <a href="https://www.cs.jhu.edu/~daveho/pubs/oopsla2004.pdf">
Finding Bugs is Easy, chapter '4.5 Bad Covariant Definition of Equals (Eq)'</a>:
</p>
<p>
Java classes and records may override the <code>equals(Object)</code> method to define
a predicate for object equality. This method is used by many of the Java runtime
library classes; for example, to implement generic containers.
</p>
<p>
Programmers sometimes mistakenly use the type of their class <code>Foo</code>
as the type of the parameter to <code>equals()</code>:
</p>
<source>
public boolean equals(Foo obj) {...}
</source>
<p>
This covariant version of <code>equals()</code> does not override the version in the
<code>Object</code> class, and it may lead to unexpected behavior at runtime,
especially if the class is used with one of the standard collection classes
which expect that the standard <code>equals(Object)</code> method is overridden.
</p>
<p>
This kind of bug is not obvious because it looks correct, and in circumstances where
the class is accessed through the references of the class type (rather than a supertype),
it will work correctly. However, the first time it is used in a container,
the behavior might be mysterious. For these reasons, this type of bug can elude
testing and code inspections.
</p>
</subsection>
<subsection name="Examples" id="CovariantEquals_Examples">
<p>
To configure the check:
</p>
<source>
<module name="CovariantEquals"/>
</source>
<p>
For example:
</p>
<source>
class Test {
public boolean equals(Test i) { // violation
return false;
}
}
</source>
<p>
The same class without violations:
</p>
<source>
class Test {
public boolean equals(Test i) { // no violation
return false;
}
public boolean equals(Object i) {
return false;
}
}
</source>
<p>
Another example:
</p>
<source>
record Test(String str) {
public boolean equals(Test r) { // violation
return false;
}
}
</source>
<p>
The same record without violations:
</p>
<source>
record Test(String str) {
public boolean equals(Test r) { // no violation
return false;
}
public boolean equals(Object r) {
return false;
}
}
</source>
</subsection>
<subsection name="Example of Usage" id="CovariantEquals_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+CovariantEquals">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="CovariantEquals_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22covariant.equals%22">
covariant.equals</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="CovariantEquals_Package">
<p>
com.puppycrawl.tools.checkstyle.checks.coding
</p>
</subsection>
<subsection name="Parent Module" id="CovariantEquals_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="DeclarationOrder">
<p>Since Checkstyle 3.2</p>
<subsection name="Description" id="DeclarationOrder_Description">
<p>
Checks that the parts of a class, record, or interface declaration appear in the order
suggested by the
<a href="https://checkstyle.org/styleguides/sun-code-conventions-19990420/CodeConventions.doc2.html#a1852">
Code Conventions for the Java Programming Language</a>.
</p>
<p>
According to <a
href="styleguides/sun-code-conventions-19990420/CodeConventions.doc2.html#a1852">
Code Conventions for the Java Programming Language</a> , the parts
of a class or interface declaration should appear in the following
order:
</p>
<ol>
<li>
Class (static) variables. First the public class variables, then
protected, then package level (no access modifier), and then
private.
</li>
<li>
Instance variables. First the public class variables, then
protected, then package level (no access modifier), and then
private.
</li>
<li> Constructors </li>
<li> Methods </li>
</ol>
<p>
Purpose of <b>ignore*</b> option is to ignore related violations,
however it still impacts on other class members.
</p>
<p>
ATTENTION: the check skips class fields which have
<a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-8.html#jls-8.3.3">
forward references</a>
from validation due to the fact that we have Checkstyle's limitations to clearly
detect user intention of fields location and grouping. For example:
</p>
<source>
public class A {
private double x = 1.0;
private double y = 2.0;
public double slope = x / y; // will be skipped from validation due to forward reference
}
</source>
</subsection>
<subsection name="Properties" id="DeclarationOrder_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>ignoreConstructors</td>
<td>control whether to ignore constructors.</td>
<td><a href="property_types.html#boolean">boolean</a></td>
<td><code>false</code></td>
<td>5.2</td>
</tr>
<tr>
<td>ignoreModifiers</td>
<td>control whether to ignore modifiers (fields, ...).</td>
<td><a href="property_types.html#boolean">boolean</a></td>
<td><code>false</code></td>
<td>5.2</td>
</tr>
</table>
</div>
</subsection>
<subsection name="Examples" id="DeclarationOrder_Examples">
<p>
To configure the check:
</p>
<source>
<module name="DeclarationOrder"/>
</source>
<p>Example:</p>
<source>
public class Test {
public int a;
protected int b;
public int c; // violation, variable access definition in wrong order
Test() {
this.a = 0;
}
public void foo() {
// This method does nothing
}
Test(int a) { // violation, constructor definition in wrong order
this.a = a;
}
private String name; // violation, instance variable declaration in wrong order
}
</source>
<p>
To configure the check to ignore validation of constructors:
</p>
<source>
<module name="DeclarationOrder">
<property name="ignoreConstructors" value="true"/>
</module>
</source>
<p>Example:</p>
<source>
public class Test {
public int a;
protected int b;
public int c; // violation, variable access definition in wrong order
Test() {
this.a = 0;
}
public void foo() {
// This method does nothing
}
Test(int a) { // OK, validation of constructors ignored
this.a = a;
}
private String name; // violation, instance variable declaration in wrong order
}
</source>
<p>
To configure the check to ignore modifiers:
</p>
<source>
<module name="DeclarationOrder">
<property name="ignoreModifiers" value="true"/>
</module>
</source>
<p>Example:</p>
<source>
public class Test {
public int a;
protected int b;
public int c; // OK, access modifiers not considered while validating
Test() {
this.a = 0;
}
public void foo() {
// This method does nothing
}
Test(int a) { // violation, constructor definition in wrong order
this.a = a;
}
private String name; // violation, instance variable declaration in wrong order
}
</source>
</subsection>
<subsection name="Example of Usage" id="DeclarationOrder_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+DeclarationOrder">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="DeclarationOrder_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22declaration.order.access%22">
declaration.order.access</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22declaration.order.constructor%22">
declaration.order.constructor</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22declaration.order.instance%22">
declaration.order.instance</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22declaration.order.static%22">
declaration.order.static</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="DeclarationOrder_Package">
<p>
com.puppycrawl.tools.checkstyle.checks.coding
</p>
</subsection>
<subsection name="Parent Module" id="DeclarationOrder_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="DefaultComesLast">
<p>Since Checkstyle 3.4</p>
<subsection name="Description" id="DefaultComesLast_Description">
<p>
Check that the <code>default</code> is after all the
cases in a <code>switch</code> statement.
</p>
<p>
Rationale: Java allows <code>default</code> anywhere
within the <code>switch</code> statement. But it is
more readable if it comes after the last <code>case</code>.
</p>
</subsection>
<subsection name="Properties" id="DefaultComesLast_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>skipIfLastAndSharedWithCase</td>
<td>
Control whether to allow <code>default</code> along with
<code>case</code> if they are not last.
</td>
<td><a href="property_types.html#boolean">boolean</a></td>
<td><code>false</code></td>
<td>7.7</td>
</tr>
</table>
</div>
</subsection>
<subsection name="Examples" id="DefaultComesLast_Examples">
<p>
To configure the check:
</p>
<source>
<module name="DefaultComesLast"/>
</source>
<p>
Example:
</p>
<source>
switch (i) {
case 1:
break;
case 2:
break;
default: // OK
break;
}
switch (i) {
case 1:
break;
case 2:
break; // OK, no default
}
switch (i) {
case 1:
break;
default: // violation, 'default' before 'case'
break;
case 2:
break;
}
switch (i) {
case 1:
default: // violation, 'default' before 'case'
break;
case 2:
break;
}
</source>
<p>
To configure the check to allow default label to be not last if it is shared with case:
</p>
<source>
<module name="DefaultComesLast">
<property name="skipIfLastAndSharedWithCase" value="true"/>
</module>
</source>
<p>
Example:
</p>
<source>
switch (i) {
case 1:
break;
case 2:
default: // OK
break;
case 3:
break;
}
switch (i) {
case 1:
break;
default: // violation
case 2:
break;
}
// Switch rules are not subject to fall through, so this is still a violation:
switch (i) {
case 1 -> x = 9;
default -> x = 10; // violation
case 2 -> x = 32;
}
</source>
</subsection>
<subsection name="Example of Usage" id="DefaultComesLast_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+DefaultComesLast">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="DefaultComesLast_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22default.comes.last%22">
default.comes.last</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fcoding+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22default.comes.last.in.casegroup%22">
default.comes.last.in.casegroup</a>
</li>
</ul>