/
design.xml
1998 lines (1882 loc) · 64.3 KB
/
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"?>
<ruleset name="Design"
xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">
<description>
The Design ruleset contains rules that flag suboptimal code implementations. Alternate approaches
are suggested.
</description>
<rule deprecated="true" name="UseSingleton" ref="UseUtilityClass"/>
<rule name="UseUtilityClass"
since="0.3"
message="All methods are static. Consider using a utility class instead. Alternatively, you could add a private constructor or make the class abstract to silence this warning."
class="net.sourceforge.pmd.lang.java.rule.design.UseUtilityClassRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#UseUtilityClass">
<description>
<![CDATA[
For classes that only have static methods, consider making them utility classes.
Note that this doesn't apply to abstract classes, since their subclasses may
well include non-static methods. Also, if you want this class to be a utility class,
remember to add a private constructor to prevent instantiation.
(Note, that this use was known before PMD 5.1.0 as UseSingleton).
]]>
</description>
<priority>3</priority>
<example>
<![CDATA[
public class MaybeAUtility {
public static void foo() {}
public static void bar() {}
}
]]>
</example>
</rule>
<rule name="SimplifyBooleanReturns"
since="0.9"
message="Avoid unnecessary if..then..else statements when returning booleans"
class="net.sourceforge.pmd.lang.java.rule.design.SimplifyBooleanReturnsRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#SimplifyBooleanReturns">
<description>
Avoid unnecessary if-then-else statements when returning a boolean. The result of
the conditional test can be returned instead.
</description>
<priority>3</priority>
<example>
<![CDATA[
public boolean isBarEqualTo(int x) {
if (bar == x) { // this bit of code...
return true;
} else {
return false;
}
}
public boolean isBarEqualTo(int x) {
return bar == x; // can be replaced with this
}
]]>
</example>
</rule>
<rule name="SimplifyBooleanExpressions"
language="java"
since="1.05"
message="Avoid unnecessary comparisons in boolean expressions"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#SimplifyBooleanExpressions">
<description>
Avoid unnecessary comparisons in boolean expressions, they serve no purpose and impacts readability.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//EqualityExpression/PrimaryExpression
/PrimaryPrefix/Literal/BooleanLiteral
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Bar {
// can be simplified to
// bar = isFoo();
private boolean bar = (isFoo() == true);
public isFoo() { return false;}
}
]]>
</example>
</rule>
<rule name="SwitchStmtsShouldHaveDefault"
language="java"
since="1.0"
message="Switch statements should have a default label"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#SwitchStmtsShouldHaveDefault">
<description>
All switch statements should include a default option to catch any unspecified values.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//SwitchStatement[not(SwitchLabel[@Default='true'])]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public void bar() {
int x = 2;
switch (x) {
case 1: int j = 6;
case 2: int j = 8;
// missing default: here
}
}
]]>
</example>
</rule>
<rule name="AvoidDeeplyNestedIfStmts"
since="1.0"
message="Deeply nested if..then statements are hard to read"
class="net.sourceforge.pmd.lang.java.rule.design.AvoidDeeplyNestedIfStmtsRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#AvoidDeeplyNestedIfStmts">
<description>
Avoid creating deeply nested if-then statements since they are harder to read and error-prone to maintain.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
public void bar(int x, int y, int z) {
if (x>y) {
if (y>z) {
if (z==x) {
// !! too deep
}
}
}
}
}
]]>
</example>
</rule>
<rule name="AvoidReassigningParameters"
since="1.0"
message="Avoid reassigning parameters such as ''{0}''"
class="net.sourceforge.pmd.lang.java.rule.design.AvoidReassigningParametersRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#AvoidReassigningParameters">
<description>
Reassigning values to incoming parameters is not recommended. Use temporary local variables instead.
</description>
<priority>2</priority>
<example>
<![CDATA[
public class Foo {
private void foo(String bar) {
bar = "something else";
}
}
]]>
</example>
</rule>
<rule name="SwitchDensity"
since="1.02"
message="A high ratio of statements to labels in a switch statement. Consider refactoring."
class="net.sourceforge.pmd.lang.java.rule.design.SwitchDensityRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#SwitchDensity">
<description>
A high ratio of statements to labels in a switch statement implies that the switch statement
is overloaded. Consider moving the statements into new methods or creating subclasses based
on the switch variable.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
public void bar(int x) {
switch (x) {
case 1: {
// lots of statements
break;
} case 2: {
// lots of statements
break;
}
}
}
}
]]>
</example>
</rule>
<rule name="ConstructorCallsOverridableMethod"
since="1.04"
message="Overridable {0} called during object construction"
class="net.sourceforge.pmd.lang.java.rule.design.ConstructorCallsOverridableMethodRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#ConstructorCallsOverridableMethod">
<description>
Calling overridable methods during construction poses a risk of invoking methods on an incompletely
constructed object and can be difficult to debug.
It may leave the sub-class unable to construct its superclass or forced to replicate the construction
process completely within itself, losing the ability to call super(). If the default constructor
contains a call to an overridable method, the subclass may be completely uninstantiable. Note that
this includes method calls throughout the control flow graph - i.e., if a constructor Foo() calls a
private method bar() that calls a public method buz(), this denotes a problem.
</description>
<priority>1</priority>
<example>
<![CDATA[
public class SeniorClass {
public SeniorClass(){
toString(); //may throw NullPointerException if overridden
}
public String toString(){
return "IAmSeniorClass";
}
}
public class JuniorClass extends SeniorClass {
private String name;
public JuniorClass(){
super(); //Automatic call leads to NullPointerException
name = "JuniorClass";
}
public String toString(){
return name.toUpperCase();
}
}
]]>
</example>
</rule>
<rule name="AccessorClassGeneration"
since="1.04"
message="Avoid instantiation through private constructors from outside of the constructor's class."
class="net.sourceforge.pmd.lang.java.rule.design.AccessorClassGenerationRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#AccessorClassGeneration">
<description>
Instantiation by way of private constructors from outside of the constructor's class often causes the
generation of an accessor. A factory method, or non-privatization of the constructor can eliminate this
situation. The generated class file is actually an interface. It gives the accessing class the ability
to invoke a new hidden package scope constructor that takes the interface as a supplementary parameter.
This turns a private constructor effectively into one with package scope, and is challenging to discern.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Outer {
void method(){
Inner ic = new Inner();//Causes generation of accessor class
}
public class Inner {
private Inner(){}
}
}
]]>
</example>
</rule>
<rule name="FinalFieldCouldBeStatic"
language="java"
since="1.1"
message="This final field could be made static"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#FinalFieldCouldBeStatic">
<description>
If a final field is assigned to a compile-time constant, it could be made static, thus saving overhead
in each object at runtime.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//FieldDeclaration
[@Final='true' and @Static='false']
[not (../../../../ClassOrInterfaceDeclaration[@Interface='true'])]
/VariableDeclarator/VariableInitializer/Expression
/PrimaryExpression[not(PrimarySuffix)]/PrimaryPrefix/Literal
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
public final int BAR = 42; // this could be static and save some space
}
]]>
</example>
</rule>
<rule name="CloseResource"
since="1.2.2"
message="Ensure that resources like this {0} object are closed after use"
class="net.sourceforge.pmd.lang.java.rule.design.CloseResourceRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#CloseResource">
<description>
Ensure that resources (like Connection, Statement, and ResultSet objects) are always closed after use.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Bar {
public void foo() {
Connection c = pool.getConnection();
try {
// do stuff
} catch (SQLException ex) {
// handle exception
} finally {
// oops, should close the connection using 'close'!
// c.close();
}
}
}
]]>
</example>
</rule>
<rule name="NonStaticInitializer"
language="java"
since="1.5"
message="Non-static initializers are confusing"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#NonStaticInitializer">
<description>
A non-static initializer block will be called any time a constructor is invoked (just prior to
invoking the constructor). While this is a valid language construct, it is rarely used and is
confusing.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//Initializer[@Static='false']
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class MyClass {
// this block gets run before any call to a constructor
{
System.out.println("I am about to construct myself");
}
}
]]>
</example>
</rule>
<rule name="DefaultLabelNotLastInSwitchStmt"
language="java"
since="1.5"
message="The default label should be the last label in a switch statement"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#DefaultLabelNotLastInSwitchStmt">
<description>
By convention, the default label should be the last label in a switch statement.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//SwitchStatement
[not(SwitchLabel[position() = last()][@Default='true'])]
[SwitchLabel[@Default='true']]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
void bar(int a) {
switch (a) {
case 1: // do something
break;
default: // the default case should be last, by convention
break;
case 2:
break;
}
}
} ]]>
</example>
</rule>
<rule name="NonCaseLabelInSwitchStatement"
language="java"
since="1.5"
message="A non-case label was present in a switch statement"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#NonCaseLabelInSwitchStatement">
<description>
A non-case label (e.g. a named break/continue label) was present in a switch statement.
This legal, but confusing. It is easy to mix up the case labels and the non-case labels.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//SwitchStatement//BlockStatement/Statement/LabeledStatement
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
void bar(int a) {
switch (a) {
case 1:
// do something
break;
mylabel: // this is legal, but confusing!
break;
default:
break;
}
}
}
]]>
</example>
</rule>
<rule name="OptimizableToArrayCall"
language="java"
since="1.8"
message="This call to Collection.toArray() may be optimizable"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#OptimizableToArrayCall">
<description>
Calls to a collection's toArray() method should specify target arrays sized to match the size of the
collection. Initial arrays that are too small are discarded in favour of new ones that have to be created
that are the proper size.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression
[PrimaryPrefix/Name[ends-with(@Image, 'toArray')]]
[
PrimarySuffix/Arguments/ArgumentList/Expression
/PrimaryExpression/PrimaryPrefix/AllocationExpression
/ArrayDimsAndInits/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image='0']
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
List foos = getFoos();
// inefficient, the array will be discarded
Foo[] fooArray = foos.toArray(new Foo[0]);
// much better; this one sizes the destination array,
// avoiding of a new one via reflection
Foo[] fooArray = foos.toArray(new Foo[foos.size()]);
]]>
</example>
</rule>
<rule name="BadComparison"
language="java"
since="1.8"
message="Avoid equality comparisons with Double.NaN"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#BadComparison">
<description>
Avoid equality comparisons with Double.NaN. Due to the implicit lack of representation
precision when comparing floating point numbers these are likely to cause logic errors.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//EqualityExpression[@Image='==']
/PrimaryExpression/PrimaryPrefix
/Name[@Image='Double.NaN' or @Image='Float.NaN']
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
boolean x = (y == Double.NaN);
]]>
</example>
</rule>
<rule name="EqualsNull"
language="java"
since="1.9"
message="Avoid using equals() to compare against null"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#EqualsNull">
<description>
Tests for null should not use the equals() method. The '==' operator should be used instead.
</description>
<priority>1</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression
[
PrimaryPrefix[Name[ends-with(@Image, 'equals')]]
[following-sibling::node()/Arguments/ArgumentList[count(Expression)=1]
/Expression/PrimaryExpression/PrimaryPrefix/Literal/NullLiteral]
or
PrimarySuffix[ends-with(@Image, 'equals')]
[following-sibling::node()/Arguments/ArgumentList[count(Expression)=1]
/Expression/PrimaryExpression/PrimaryPrefix/Literal/NullLiteral]
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
String x = "foo";
if (x.equals(null)) { // bad form
doSomething();
}
if (x == null) { // preferred
doSomething();
}
]]>
</example>
</rule>
<rule name="ConfusingTernary"
since="1.9"
message="Avoid if (x != y) ..; else ..;"
class="net.sourceforge.pmd.lang.java.rule.design.ConfusingTernaryRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#ConfusingTernary">
<description>
Avoid negation within an "if" expression with an "else" clause. For example, rephrase:
if (x != y) diff(); else same();
as:
if (x == y) same(); else diff();
Most "if (x != y)" cases without an "else" are often return cases, so consistent use of this
rule makes the code easier to read. Also, this resolves trivial ordering problems, such
as "does the error case go first?" or "does the common case go first?".
</description>
<priority>3</priority>
<example>
<![CDATA[
boolean bar(int x, int y) {
return (x != y) ? diff : same;
}
]]>
</example>
</rule>
<rule name="InstantiationToGetClass"
language="java"
since="2.0"
message="Avoid instantiating an object just to call getClass() on it; use the .class public member instead"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#InstantiationToGetClass">
<description>
Avoid instantiating an object just to call getClass() on it; use the .class public member instead.
</description>
<priority>4</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimarySuffix
[@Image='getClass']
[parent::PrimaryExpression
[PrimaryPrefix/AllocationExpression]
[count(PrimarySuffix) = 2]
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
// replace this
Class c = new String().getClass();
// with this:
Class c = String.class;
]]>
</example>
</rule>
<rule name="IdempotentOperations"
since="2.0"
message="Avoid idempotent operations (like assigning a variable to itself)."
class="net.sourceforge.pmd.lang.java.rule.design.IdempotentOperationsRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#IdempotentOperations">
<description>
Avoid idempotent operations - they have no effect.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
public void bar() {
int x = 2;
x = x;
}
}
]]>
</example>
</rule>
<rule
name="SimpleDateFormatNeedsLocale"
language="java"
since="2.0"
message="When instantiating a SimpleDateFormat object, specify a Locale"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#SimpleDateFormatNeedsLocale">
<description>
Be sure to specify a Locale when creating SimpleDateFormat instances to ensure that locale-appropriate
formatting is used.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//AllocationExpression
[ClassOrInterfaceType[@Image='SimpleDateFormat']]
[Arguments[@ArgumentCount=1]]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
// Should specify Locale.US (or whatever)
private SimpleDateFormat sdf = new SimpleDateFormat("pattern");
}
]]>
</example>
</rule>
<rule name="ImmutableField"
since="2.0"
message="Private field ''{0}'' could be made final; it is only initialized in the declaration or constructor."
class="net.sourceforge.pmd.lang.java.rule.design.ImmutableFieldRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#ImmutableField">
<description>
Identifies private fields whose values never change once they are initialized either in the declaration
of the field or by a constructor. This helps in converting existing classes to becoming immutable ones.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
private int x; // could be final
public Foo() {
x = 7;
}
public void foo() {
int a = x + 2;
}
}
]]>
</example>
</rule>
<rule name="UseLocaleWithCaseConversions"
language="java"
since="2.0"
message="When doing a String.toLowerCase()/toUpperCase() call, use a Locale"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#UseLocaleWithCaseConversions">
<description>
When doing String.toLowerCase()/toUpperCase() conversions, use Locales to avoids problems with languages that
have unusual conventions, i.e. Turkish.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//PrimaryExpression
[PrimaryPrefix/Name
[ends-with(@Image, 'toLowerCase') or ends-with(@Image,
'toUpperCase')]
]
[PrimarySuffix[position() = 1]/Arguments[@ArgumentCount=0]]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
class Foo {
// BAD
if (x.toLowerCase().equals("list")) { }
/*
This will not match "LIST" when in Turkish locale
The above could be
if (x.toLowerCase(Locale.US).equals("list")) { }
or simply
if (x.equalsIgnoreCase("list")) { }
*/
// GOOD
String z = a.toLowerCase(Locale.EN);
}
]]>
</example>
</rule>
<rule name="AvoidProtectedFieldInFinalClass"
language="java"
since="2.1"
message="Avoid protected fields in a final class. Change to private or package access."
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#AvoidProtectedFieldInFinalClass">
<description>
Do not use protected fields in final classes since they cannot be subclassed.
Clarify your intent by using private or package access modifiers instead.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration[@Final='true']
/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration
/FieldDeclaration[@Protected='true']
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public final class Bar {
private int x;
protected int y; // bar cannot be subclassed, so is y really private or package visible?
Bar() {}
}
]]>
</example>
</rule>
<rule name="AssignmentToNonFinalStatic"
since="2.2"
message="Possible unsafe assignment to a non-final static field in a constructor."
class="net.sourceforge.pmd.lang.java.rule.design.AssignmentToNonFinalStaticRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#AssignmentToNonFinalStatic">
<description>
Identifies a possible unsafe usage of a static field.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class StaticField {
static int x;
public FinalFields(int y) {
x = y; // unsafe
}
}
]]>
</example>
</rule>
<rule name="MissingStaticMethodInNonInstantiatableClass"
language="java"
since="3.0"
message="Class cannot be instantiated and does not provide any static methods or fields"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#MissingStaticMethodInNonInstantiatableClass">
<description>
A class that has private constructors and does not have any static methods or fields cannot be used.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration[@Nested='false']
[
(
count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration)>0
and
count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration) = count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[@Private='true'])
)
and
count(.//MethodDeclaration[@Static='true'])=0
and
count(.//FieldDeclaration[@Private='false'][@Static='true'])=0
and
count(.//ClassOrInterfaceDeclaration[@Nested='true']
[@Public='true']
[@Static='true']
[count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/ConstructorDeclaration[@Public='true']) > 0]
[count(./ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration/MethodDeclaration
[@Public='true']
[./ResultType/Type/ReferenceType/ClassOrInterfaceType
[@Image = //ClassOrInterfaceDeclaration[@Nested='false']/@Image]
]
) > 0]
) = 0
and
count(//ClassOrInterfaceDeclaration
[@Nested='true']
[@Static='true']
[@Public='true']
[.//MethodDeclaration
[@Public='true']
[.//ReturnStatement//AllocationExpression
[ClassOrInterfaceType
[@Image = //ClassOrInterfaceDeclaration/@Image]
]
[./Arguments//PrimaryPrefix/@ThisModifier='true']
]
]
) = 0
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
// This class is unusable, since it cannot be
// instantiated (private constructor),
// and no static method can be called.
public class Foo {
private Foo() {}
void foo() {}
}
]]>
</example>
</rule>
<rule name="AvoidSynchronizedAtMethodLevel"
language="java"
since="3.0"
message="Use block level rather than method level synchronization"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#AvoidSynchronizedAtMethodLevel">
<description>
Method-level synchronization can cause problems when new code is added to the method.
Block-level synchronization helps to ensure that only the code that needs synchronization
gets it.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//MethodDeclaration[@Synchronized='true']
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
// Try to avoid this:
synchronized void foo() {
}
// Prefer this:
void bar() {
synchronized(this) {
}
}
// Try to avoid this for static methods:
static synchronized void fooStatic() {
}
// Prefer this:
static void barStatic() {
synchronized(Foo.class) {
}
}
}
]]>
</example>
</rule>
<rule name="MissingBreakInSwitch"
language="java"
since="3.0"
message="A switch statement does not contain a break"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#MissingBreakInSwitch">
<description>
Switch statements without break or return statements for each case option
may indicate problematic behaviour. Empty cases are ignored as these indicate an intentional fall-through.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//SwitchStatement
[(count(.//BreakStatement)
+ count(BlockStatement//Statement/ReturnStatement)
+ count(BlockStatement//Statement/ThrowStatement)
+ count(BlockStatement//Statement/IfStatement[@Else='true' and Statement[2][ReturnStatement|ThrowStatement]]/Statement[1][ReturnStatement|ThrowStatement])
+ count(SwitchLabel[name(following-sibling::node()) = 'SwitchLabel'])
+ count(SwitchLabel[count(following-sibling::node()) = 0])
< count (SwitchLabel))]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public void bar(int status) {
switch(status) {
case CANCELLED:
doCancelled();
// break; hm, should this be commented out?
case NEW:
doNew();
// is this really a fall-through?
case REMOVED:
doRemoved();
// what happens if you add another case after this one?
case OTHER: // empty case - this is interpreted as an intentional fall-through
case ERROR:
doErrorHandling();
break;
}
}
]]>
</example>
</rule>
<rule name="UseNotifyAllInsteadOfNotify"
language="java"
since="3.0"
message="Call Thread.notifyAll() rather than Thread.notify()"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/design.html#UseNotifyAllInsteadOfNotify">
<description>
Thread.notify() awakens a thread monitoring the object. If more than one thread is monitoring, then only
one is chosen. The thread chosen is arbitrary; thus its usually safer to call notifyAll() instead.
</description>
<priority>3</priority>
<properties>
<property name="xpath">
<value>
<![CDATA[
//StatementExpression/PrimaryExpression
[PrimarySuffix/Arguments[@ArgumentCount = '0']]
[
PrimaryPrefix[./Name[@Image='notify' or ends-with(@Image,'.notify')]
or ../PrimarySuffix/@Image='notify'
or (./AllocationExpression and ../PrimarySuffix[@Image='notify'])