forked from checkstyle/checkstyle
/
config_design.xml
1713 lines (1549 loc) · 56.4 KB
/
config_design.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>Class Design</title>
</head>
<body>
<section name="Content">
<macro name="toc">
<param name="fromDepth" value="1"/>
<param name="toDepth" value="1"/>
</macro>
</section>
<section name="DesignForExtension">
<p>Since Checkstyle 3.1</p>
<subsection name="Description" id="DesignForExtension_Description">
<p>
Checks that classes are designed for extension (subclass creation).
</p>
<p>
Nothing wrong could be with founded classes.
This check makes sense only for library projects (not application projects)
which care of ideal OOP-design to make sure that class works in all cases even misusage.
Even in library projects this check most likely will find classes that are designed
for extension by somebody. User needs to use suppressions extensively to got a benefit
from this check, and keep in suppressions all confirmed/known classes that are deigned
for inheritance intentionally to let the check catch only new classes, and bring this to
team/user attention.
</p>
<p>
ATTENTION: Only user can decide whether a class is designed for extension or not.
The check just shows all classes which are possibly designed for extension.
If smth inappropriate is found please use suppression.
</p>
<p>
ATTENTION: If the method which can be overridden in a subclass has a javadoc comment
(a good practice is to explain its self-use of overridable methods) the check will not
rise a violation. The violation can also be skipped if the method which can be overridden
in a subclass has one or more annotations that are specified in ignoredAnnotations
option. Note, that by default @Override annotation is not included in the
ignoredAnnotations set as in a subclass the method which has the annotation can also be
overridden in its subclass.
</p>
<p>
Problem is described at "Effective Java, 2nd Edition by Joshua Bloch" book, chapter
"Item 17: Design and document for inheritance or else prohibit it".
</p>
<p>
Some quotes from book:
</p>
<blockquote>The class must document its self-use of overridable methods.
By convention, a method that invokes overridable methods contains a description
of these invocations at the end of its documentation comment. The description
begins with the phrase “This implementation.”
</blockquote>
<blockquote>The best solution to this problem is to prohibit subclassing in classes that
are not designed and documented to be safely subclassed.
</blockquote>
<blockquote>If a concrete class does not implement a standard interface, then you may
inconvenience some programmers by prohibiting inheritance. If you feel that you
must allow inheritance from such a class, one reasonable approach is to ensure
that the class never invokes any of its overridable methods and to document this
fact. In other words, eliminate the class’s self-use of overridable methods entirely.
In doing so, you’ll create a class that is reasonably safe to subclass. Overriding a
method will never affect the behavior of any other method.
</blockquote>
<p>
The check finds classes that have overridable methods (public or protected methods
that are non-static, not-final, non-abstract) and have non-empty implementation.
</p>
<p>
Rationale: This library design style protects superclasses against
being broken by subclasses. The downside is that subclasses are
limited in their flexibility, in particular they cannot prevent
execution of code in the superclass, but that also means that
subclasses cannot corrupt the state of the superclass by forgetting
to call the superclass's method.
</p>
<p>
More specifically,
it enforces a programming style where superclasses provide empty
"hooks" that can be implemented by subclasses.
</p>
<p>
Example of code that cause violation as it is designed for extension:
</p>
<source>
public abstract class Plant {
private String roots;
private String trunk;
protected void validate() {
if (roots == null) throw new IllegalArgumentException("No roots!");
if (trunk == null) throw new IllegalArgumentException("No trunk!");
}
public abstract void grow();
}
public class Tree extends Plant {
private List leaves;
@Overrides
protected void validate() {
super.validate();
if (leaves == null) throw new IllegalArgumentException("No leaves!");
}
public void grow() {
validate();
}
}
</source>
<p>
Example of code without violation:
</p>
<source>
public abstract class Plant {
private String roots;
private String trunk;
private void validate() {
if (roots == null) throw new IllegalArgumentException("No roots!");
if (trunk == null) throw new IllegalArgumentException("No trunk!");
validateEx();
}
protected void validateEx() { }
public abstract void grow();
}
</source>
</subsection>
<subsection name="Properties" id="DesignForExtension_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>ignoredAnnotations</td>
<td>
Specify annotations which allow the check to skip the method from validation.
</td>
<td><a href="property_types.html#String.5B.5D">String[]</a></td>
<td><code>After, AfterClass, Before, BeforeClass, Test</code></td>
<td>7.2</td>
</tr>
<tr>
<td>requiredJavadocPhrase</td>
<td>
Specify the comment text pattern which qualifies a method as designed for
extension. Supports multi-line regex.
</td>
<td><a href="property_types.html#Pattern">Pattern</a></td>
<td><code>".*"</code></td>
<td>8.40</td>
</tr>
</table>
</div>
</subsection>
<subsection name="Examples" id="DesignForExtension_Examples">
<p>
To configure the check:
</p>
<source>
<module name="DesignForExtension"/>
</source>
<p>
To configure the check to allow methods which have @Override and @Test annotations to be
designed for extension.
</p>
<source>
<module name="DesignForExtension">
<property name="ignoredAnnotations" value="Override, Test"/>
</module>
</source>
<source>
public class A {
@Override
public int foo() {
return 2;
}
public int foo2() {return 8;} // violation
}
public class B {
/**
* This implementation ...
@return some int value.
*/
public int foo() {
return 1;
}
public int foo3() {return 3;} // violation
}
public class FooTest {
@Test
public void testFoo() {
final B b = new A();
assertEquals(2, b.foo());
}
public int foo4() {return 4;} // violation
}
</source>
<p>
To configure the check to allow methods which contain a specified comment text
pattern in their javadoc to be designed for extension.
</p>
<source>
<module name="DesignForExtension">
<property name="requiredJavadocPhrase" value="This implementation"/>
</module>
</source>
<source>
public class A {
/**
* This implementation ...
*/
public int foo() {return 2;} // ok, required javadoc phrase in comment
/**
* Do not extend ...
*/
public int foo2() {return 8;} // violation, required javadoc phrase not in comment
public int foo3() {return 3;} // violation, required javadoc phrase not in comment
}
</source>
<p>
To configure the check to allow methods which contain a specified comment text
pattern in their javadoc which can span multiple lines
to be designed for extension.
</p>
<source>
<module name="DesignForExtension">
<property name="requiredJavadocPhrase"
value="This[\s\S]*implementation"/>
</module>
</source>
<source>
public class A {
/**
* This
* implementation ...
*/
public int foo() {return 2;} // ok, required javadoc phrase in comment
/**
* Do not extend ...
*/
public int foo2() {return 8;} // violation, required javadoc phrase not in comment
public int foo3() {return 3;} // violation, required javadoc phrase not in comment
}
</source>
</subsection>
<subsection name="Example of Usage" id="DesignForExtension_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Asun_checks.xml+repo%3Acheckstyle%2Fcheckstyle+DesignForExtension">
Sun Style</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+DesignForExtension">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="DesignForExtension_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fdesign+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22design.forExtension%22">
design.forExtension</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="DesignForExtension_Package">
<p>
com.puppycrawl.tools.checkstyle.checks.design
</p>
</subsection>
<subsection name="Parent Module" id="DesignForExtension_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="FinalClass">
<p>Since Checkstyle 3.1</p>
<subsection name="Description" id="FinalClass_Description">
<p>
Checks that a class that has only private constructors and
has no descendant classes is declared as final.
</p>
</subsection>
<subsection name="Examples" id="FinalClass_Examples">
<p>
To configure the check:
</p>
<source>
<module name="FinalClass"/>
</source>
<p>
Example:
</p>
<source>
final class MyClass { // OK
private MyClass() { }
}
class MyClass { // violation, class should be declared final
private MyClass() { }
}
class MyClass { // OK, since it has a public constructor
int field1;
String field2;
private MyClass(int value) {
this.field1 = value;
this.field2 = " ";
}
public MyClass(String value) {
this.field2 = value;
this.field1 = 0;
}
}
class TestAnonymousInnerClasses { // OK, class has an anonymous inner class.
public static final TestAnonymousInnerClasses ONE = new TestAnonymousInnerClasses() {
};
private TestAnonymousInnerClasses() {
}
}
</source>
</subsection>
<subsection name="Example of Usage" id="FinalClass_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Asun_checks.xml+repo%3Acheckstyle%2Fcheckstyle+FinalClass">
Sun Style</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+FinalClass">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="FinalClass_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fdesign+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22final.class%22">
final.class</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="FinalClass_Package">
<p>
com.puppycrawl.tools.checkstyle.checks.design
</p>
</subsection>
<subsection name="Parent Module" id="FinalClass_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="HideUtilityClassConstructor">
<p>Since Checkstyle 3.1</p>
<subsection name="Description" id="HideUtilityClassConstructor_Description">
<p>
Makes sure that utility classes (classes that contain only static
methods or fields in their API) do not have a public constructor.
</p>
<p>
Rationale: Instantiating utility classes does not make sense. Hence,
the constructors should either be private or (if you want to allow
subclassing) protected. A common mistake is forgetting to hide the
default constructor.
</p>
<p>
If you make the constructor protected you may want to consider the
following constructor implementation technique to disallow
instantiating subclasses:
</p>
<source>
public class StringUtils // not final to allow subclassing
{
protected StringUtils() {
// prevents calls from subclass
throw new UnsupportedOperationException();
}
public static int count(char c, String s) {
// ...
}
}
</source>
</subsection>
<subsection name="Examples" id="HideUtilityClassConstructor_Examples">
<p>
To configure the check:
</p>
<source>
<module name="HideUtilityClassConstructor"/>
</source>
<p>
Example:
</p>
<source>
class Test { // violation, class only has a static method and a constructor
public Test() {
}
public static void fun() {
}
}
class Foo { // OK
private Foo() {
}
static int n;
}
class Bar { // OK
protected Bar() {
// prevents calls from subclass
throw new UnsupportedOperationException();
}
}
class UtilityClass { // violation, class only has a static field
static float f;
}
</source>
</subsection>
<subsection name="Example of Usage" id="HideUtilityClassConstructor_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Asun_checks.xml+repo%3Acheckstyle%2Fcheckstyle+HideUtilityClassConstructor">
Sun Style</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+HideUtilityClassConstructor">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="HideUtilityClassConstructor_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fdesign+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22hide.utility.class%22">
hide.utility.class</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="HideUtilityClassConstructor_Package">
<p>
com.puppycrawl.tools.checkstyle.checks.design
</p>
</subsection>
<subsection name="Parent Module" id="HideUtilityClassConstructor_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="InnerTypeLast">
<p>Since Checkstyle 5.2</p>
<subsection name="Description" id="InnerTypeLast_Description">
<p>
Checks nested (internal) classes/interfaces are declared at the bottom of the
primary (top-level) class after all init and static init blocks,
method, constructor and field declarations.
</p>
</subsection>
<subsection name="Examples" id="InnerTypeLast_Examples">
<p>
To configure the check:
</p>
<source>
<module name="InnerTypeLast"/>
</source>
<p>Example:</p>
<source>
class Test {
private String s; // OK
class InnerTest1 {}
public void test() {} // violation, method should be declared before inner types.
}
class Test2 {
static {}; // OK
class InnerTest1 {}
public Test2() {} // violation, constructor should be declared before inner types.
}
class Test3 {
private String s; // OK
public void test() {} // OK
class InnerTest1 {}
}
</source>
</subsection>
<subsection name="Example of Usage" id="InnerTypeLast_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+InnerTypeLast">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="InnerTypeLast_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fdesign+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22arrangement.members.before.inner%22">
arrangement.members.before.inner</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="InnerTypeLast_Package">
<p>
com.puppycrawl.tools.checkstyle.checks.design
</p>
</subsection>
<subsection name="Parent Module" id="InnerTypeLast_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="InterfaceIsType">
<p>Since Checkstyle 3.1</p>
<subsection name="Description" id="InterfaceIsType_Description">
<p>
Implements Joshua Bloch, Effective Java, Item 17 - Use Interfaces only to
define types.
</p>
<p>
According to Bloch, an interface should describe a <em>type</em>.
It is therefore inappropriate to define an interface that does not
contain any methods but only constants. The Standard interface <a
href="https://docs.oracle.com/javase/8/docs/api/javax/swing/SwingConstants.html">
javax.swing.SwingConstants</a>
is an example of an interface that would be flagged by this check.
</p>
<p>
The check can be configured to also disallow marker interfaces like
<code>java.io.Serializable</code>, that do not contain methods or
constants at all.
</p>
</subsection>
<subsection name="Properties" id="InterfaceIsType_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>allowMarkerInterfaces</td>
<td>
Control whether marker interfaces like Serializable are
allowed.
</td>
<td><a href="property_types.html#boolean">boolean</a></td>
<td><code>true</code></td>
<td>3.1</td>
</tr>
</table>
</div>
</subsection>
<subsection name="Examples" id="InterfaceIsType_Examples">
<p>
To configure the check:
</p>
<source>
<module name="InterfaceIsType"/>
</source>
<p>Example:</p>
<source>
public interface Test1 { // violation
int a = 3;
}
public interface Test2 { // OK
}
public interface Test3 { // OK
int a = 3;
void test();
}
</source>
<p>
To configure the check to report violation so that it doesn't allow Marker Interfaces:
</p>
<source>
<module name="InterfaceIsType">
<property name="allowMarkerInterfaces" value="false"/>
</module>
</source>
<p>Example:</p>
<source>
public interface Test1 { // violation
int a = 3;
}
public interface Test2 { // violation
}
</source>
</subsection>
<subsection name="Example of Usage" id="InterfaceIsType_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources+filename%3Asun_checks.xml+repo%3Acheckstyle%2Fcheckstyle+InterfaceIsType">
Sun Style</a>
</li>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+InterfaceIsType">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="InterfaceIsType_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fdesign+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22interface.type%22">
interface.type</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="InterfaceIsType_Package">
<p>
com.puppycrawl.tools.checkstyle.checks.design
</p>
</subsection>
<subsection name="Parent Module" id="InterfaceIsType_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="MutableException">
<p>Since Checkstyle 3.2</p>
<subsection name="Description" id="MutableException_Description">
<p>
Ensures that exception classes (classes with names conforming to some pattern
and explicitly extending classes with names conforming to other
pattern) are immutable, that is, that they have only final fields.
</p>
<p>
The current algorithm is very simple: it checks that all members of
exception are final. The user can still mutate an exception's instance
(e.g. Throwable has a method called <code>setStackTrace</code>
which changes the exception's stack trace). But, at least, all information
provided by this exception type is unchangeable.
</p>
<p>
Rationale: Exception instances should represent an error
condition. Having non-final fields not only allows the state to be
modified by accident and therefore mask the original condition but
also allows developers to accidentally forget to set the initial state.
In both cases, code catching the exception could draw incorrect
conclusions based on the state.
</p>
</subsection>
<subsection name="Properties" id="MutableException_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>format</td>
<td>Specify pattern for exception class names.</td>
<td><a href="property_types.html#Pattern">Pattern</a></td>
<td><code>"^.*Exception$|^.*Error$|^.*Throwable$"</code></td>
<td>3.2</td>
</tr>
<tr>
<td>extendedClassNameFormat</td>
<td>Specify pattern for extended class names.</td>
<td><a href="property_types.html#Pattern">Pattern</a></td>
<td><code>"^.*Exception$|^.*Error$|^.*Throwable$"</code></td>
<td>6.2</td>
</tr>
</table>
</div>
</subsection>
<subsection name="Examples" id="MutableException_Examples">
<p>
To configure the check:
</p>
<source>
<module name="MutableException"/>
</source>
<p>Example:</p>
<div class="wrapper">
<source>
class FirstClass extends Exception {
private int code; // OK, Class-Name doesn't match with default pattern
public FirstClass() {
code = 1;
}
}
class MyException extends Exception {
private int code; // violation, The field 'code' must be declared final
public MyException() {
code = 2;
}
}
class MyThrowable extends Throwable {
final int code;
String message; // violation, The field 'message' must be declared final
public MyThrowable(int code, String message) {
this.code = code;
this.message = message;
}
}
class BadException extends java.lang.Exception {
int code; // violation, The field 'code' must be declared final
public BadException(int code) {
this.code = code;
}
}
</source>
</div>
<p>
To configure the check so that it only checks for class names
that match pattern:
</p>
<source>
<module name="MutableException">
<property name="format" value="^.*Exception$"/>
</module>
</source>
<p>Example:</p>
<div class="wrapper">
<source>
class FirstClass extends Exception {
private int code; // OK, Class-Name doesn't match with Given pattern
public FirstClass() {
code = 1;
}
}
class MyException extends Exception {
private int code; // violation, The field 'code' must be declared final
public MyException() {
code = 2;
}
}
class MyThrowable extends Throwable {
final int code; // OK
String message; // OK
public MyThrowable(int code, String message) {
this.code = code;
this.message = message;
}
}
class BadException extends java.lang.Exception {
int code; // violation, The field 'code' must be declared final
public BadException(int code) {
this.code = code;
}
}
</source>
</div>
<p>
To configure the check so that it only checks for type names used in 'extends'
that match pattern:
</p>
<source>
<module name="MutableException">
<property name="extendedClassNameFormat" value="^.*Throwable$"/>
</module>
</source>
<p>Example:</p>
<div class="wrapper">
<source>
class FirstClass extends Exception {
private int code; // OK, Extended-Class-Name doesn't match with Given pattern
public FirstClass() {
code = 1;
}
}
class MyException extends Exception {
private int code; // OK, Extended-Class-Name doesn't match with Given pattern
public MyException() {
code = 2;
}
}
class MyThrowable extends Throwable {
final int code; // OK
String message; // violation, The field 'message' must be declared final
public MyThrowable(int code, String message) {
this.code = code;
this.message = message;
}
}
class BadException extends java.lang.Exception {
int code; // OK, Extended-Class-Name doesn't match with Given pattern
public BadException(int code) {
this.code = code;
}
}
</source>
</div>
</subsection>
<subsection name="Example of Usage" id="MutableException_Example_of_Usage">
<ul>
<li>
<a href="https://github.com/search?q=path%3Aconfig+filename%3Acheckstyle_checks.xml+repo%3Acheckstyle%2Fcheckstyle+MutableException">
Checkstyle Style</a>
</li>
</ul>
</subsection>
<subsection name="Violation Messages" id="MutableException_Violation_Messages">
<ul>
<li>
<a href="https://github.com/search?q=path%3Asrc%2Fmain%2Fresources%2Fcom%2Fpuppycrawl%2Ftools%2Fcheckstyle%2Fchecks%2Fdesign+filename%3Amessages*.properties+repo%3Acheckstyle%2Fcheckstyle+%22mutable.exception%22">
mutable.exception</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="MutableException_Package">
<p>
com.puppycrawl.tools.checkstyle.checks.design
</p>
</subsection>
<subsection name="Parent Module" id="MutableException_Parent_Module">
<p>
<a href="config.html#TreeWalker">TreeWalker</a>
</p>
</subsection>
</section>
<section name="OneTopLevelClass">
<p>Since Checkstyle 5.8</p>
<subsection name="Description" id="OneTopLevelClass_Description">
<p>
Checks that each top-level class, interface, enum
or annotation resides in a source file of its own.
Official description of a 'top-level' term:<a
href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.6">
7.6. Top Level Type Declarations</a>.
If file doesn't contain public class, interface, enum or annotation,
top-level type is the first type in file.
</p>
</subsection>
<subsection name="Examples" id="OneTopLevelClass_Examples">
<p>
To configure the check:
</p>
<source>
<module name="OneTopLevelClass"/>
</source>
<p>
<b>ATTENTION:</b> This Check does not support customization of validated tokens, so do
not use the "tokens" property.
</p>
<p>
An example of code with violations:
</p>
<div class="wrapper">
<pre>
public class Foo { // OK, first top-level class
// methods
}
class Foo2 { // violation, second top-level class
// methods
}