forked from Project-Books/book-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
codestyle.xml
1714 lines (1585 loc) · 64.4 KB
/
codestyle.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="Code Style"
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 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
Rules which enforce a specific coding style.
</description>
<rule ref="category/plsql/codestyle.xml/CodeFormat">
<properties>
<property name="indentation" value="4" />
</properties>
</rule>
<rule name="AvoidDollarSigns"
since="1.5"
message="Avoid using dollar signs in variable/method/class/interface names"
class="net.sourceforge.pmd.lang.java.rule.codestyle.AvoidDollarSignsRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#avoiddollarsigns">
<description>
Avoid using dollar signs in variable/method/class/interface names.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Fo$o { // not a recommended name
}
]]>
</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}/pmd_rules_java_codestyle.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="version" value="2.0"/>
<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="AvoidProtectedMethodInFinalClassNotExtending"
language="java"
since="5.1"
message="Avoid protected methods in a final class that doesn't extend anything other than Object. Change to private or package access."
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#avoidprotectedmethodinfinalclassnotextending">
<description>
Do not use protected methods in most final classes since they cannot be subclassed. This should
only be allowed in final classes that extend other classes with protected methods (whose
visibility cannot be reduced). Clarify your intent by using private or package access modifiers instead.
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration[@Final= true() and not(ExtendsList)]
/ClassOrInterfaceBody/ClassOrInterfaceBodyDeclaration
/MethodDeclaration[@Protected=true() and @Name != 'finalize']
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public final class Foo {
private int bar() {}
protected int baz() {} // Foo cannot be subclassed, and doesn't extend anything, so is baz() really private or package visible?
}
]]>
</example>
</rule>
<rule name="AvoidUsingNativeCode"
language="java"
since="4.1"
message="The use of native code is not recommended."
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#avoidusingnativecode">
<description>
Unnecessary reliance on Java Native Interface (JNI) calls directly reduces application portability
and increases the maintenance burden.
</description>
<priority>2</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>//Name[starts-with(@Image,'System.loadLibrary')]</value>
</property>
</properties>
<example>
<![CDATA[
public class SomeJNIClass {
public SomeJNIClass() {
System.loadLibrary("nativelib");
}
static {
System.loadLibrary("nativelib");
}
public void invalidCallsInMethod() throws SecurityException, NoSuchMethodException {
System.loadLibrary("nativelib");
}
}
]]>
</example>
</rule>
<rule name="BooleanGetMethodName"
language="java"
since="4.0"
message="A 'getX()' method which returns a boolean should be named 'isX()'"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#booleangetmethodname">
<description>
Methods that return boolean results should be named as predicate statements to denote this.
I.e, 'isReady()', 'hasValues()', 'canCommit()', 'willFail()', etc. Avoid the use of the 'get'
prefix for these methods.
</description>
<priority>4</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//MethodDeclaration
[starts-with(@Name, 'get')]
[@Arity = 0 or $checkParameterizedMethods = true()]
[
ResultType/Type/PrimitiveType[@Image = 'boolean']
and not(../Annotation//Name[@Image = 'Override'])
]
]]>
</value>
</property>
<property name="checkParameterizedMethods" type="Boolean" description="Check parameterized methods" value="false"/>
</properties>
<example>
<![CDATA[
public boolean getFoo(); // bad
public boolean isFoo(); // ok
public boolean getFoo(boolean bar); // ok, unless checkParameterizedMethods=true
]]>
</example>
</rule>
<rule name="CallSuperInConstructor"
language="java"
since="3.0"
message="It is a good practice to call super() in a constructor"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#callsuperinconstructor">
<description>
It is a good practice to call super() in a constructor. If super() is not called but
another constructor (such as an overloaded constructor) is called, this rule will not report it.
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration[ExtendsList/*]
/ClassOrInterfaceBody
/ClassOrInterfaceBodyDeclaration
/ConstructorDeclaration[ not(.//ExplicitConstructorInvocation) ]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo extends Bar{
public Foo() {
// call the constructor of Bar
super();
}
public Foo(int code) {
// do something with code
this();
// no problem with this
}
}
]]>
</example>
</rule>
<rule name="ClassNamingConventions"
since="1.2"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.java.rule.codestyle.ClassNamingConventionsRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#classnamingconventions">
<description>
Configurable naming conventions for type declarations. This rule reports
type declarations which do not match the regex that applies to their
specific kind (e.g. enum or interface). Each regex can be configured through
properties.
By default this rule uses the standard Java naming convention (Pascal case),
and reports utility class names not ending with 'Util'.
</description>
<priority>1</priority>
<example>
<![CDATA[
// This is Pascal case, the recommended naming convention in Java
// Note that the default values of this rule don't allow underscores
// or accented characters in type names
public class FooBar {}
// You may want abstract classes to be named 'AbstractXXX',
// in which case you can customize the regex for abstract
// classes to 'Abstract[A-Z]\w+'
public abstract class Thing {}
// This class doesn't respect the convention, and will be flagged
public class Éléphant {}
]]>
</example>
</rule>
<rule name="CommentDefaultAccessModifier"
since="5.4.0"
class="net.sourceforge.pmd.lang.java.rule.codestyle.CommentDefaultAccessModifierRule"
message="Missing commented default access modifier"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#commentdefaultaccessmodifier">
<description>
To avoid mistakes if we want that an Annotation, Class, Enum, Method, Constructor or Field have a default access modifier
we must add a comment at the beginning of it's declaration.
By default the comment must be `/* default */` or `/* package */`, if you want another, you have to provide a regular expression.
This rule ignores by default all cases that have a @VisibleForTesting annotation. Use the
property "ignoredAnnotations" to customize the recognized annotations.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
final String stringValue = "some string";
String getString() {
return stringValue;
}
class NestedFoo {
}
}
// should be
public class Foo {
/* default */ final String stringValue = "some string";
/* default */ String getString() {
return stringValue;
}
/* default */ class NestedFoo {
}
}
]]>
</example>
</rule>
<rule name="ConfusingTernary"
since="1.9"
message="Avoid if (x != y) ..; else ..;"
class="net.sourceforge.pmd.lang.java.rule.codestyle.ConfusingTernaryRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.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="ControlStatementBraces"
language="java"
since="6.2.0"
message="This statement should have braces"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#controlstatementbraces">
<description>
Enforce a policy for braces on control statements. It is recommended to use braces on 'if ... else'
statements and loop statements, even if they are optional. This usually makes the code clearer, and
helps prepare the future when you need to add another statement. That said, this rule lets you control
which statements are required to have braces via properties.
From 6.2.0 on, this rule supersedes WhileLoopMustUseBraces, ForLoopMustUseBraces, IfStmtMustUseBraces,
and IfElseStmtMustUseBraces.
</description>
<priority>3</priority>
<properties>
<property name="checkIfElseStmt" type="Boolean" value="true" description="Require that 'if ... else' statements use braces" />
<property name="checkSingleIfStmt" type="Boolean" value="true" description="Require that 'if' statements with a single branch use braces" />
<property name="checkWhileStmt" type="Boolean" value="true" description="Require that 'while' loops use braces" />
<property name="checkForStmt" type="Boolean" value="true" description="Require that 'for' loops should use braces" />
<property name="checkDoWhileStmt" type="Boolean" value="true" description="Require that 'do ... while' loops use braces" />
<property name="checkCaseStmt" type="Boolean" value="false" description="Require that cases of a switch have braces"/>
<property name="allowEmptyLoop" type="Boolean" value="false" description="Allow loops with an empty statement, e.g. 'while(true);'" />
<property name="version" value="2.0"/>
<property name="xpath">
<value><![CDATA[
//WhileStatement[$checkWhileStmt and not(Statement/Block) and not($allowEmptyLoop and Statement/EmptyStatement)]
|
//ForStatement[$checkForStmt and not(Statement/Block) and not($allowEmptyLoop and Statement/EmptyStatement)]
|
//DoStatement[$checkDoWhileStmt and not(Statement/Block) and not($allowEmptyLoop and Statement/EmptyStatement)]
|
(: The violation is reported on the sub statement -- not the if statement :)
//Statement[$checkIfElseStmt and parent::IfStatement and not(child::Block or child::IfStatement)
(: Whitelists single if statements :)
and ($checkSingleIfStmt
(: Inside this not(...) is the definition of a "single if statement" :)
or not(count(../Statement) = 1 (: No else stmt :)
(: Not the last branch of an 'if ... else if' chain :)
and not(parent::IfStatement[parent::Statement[parent::IfStatement]])))]
|
(: Reports case labels if one of their subordinate statements is not braced :)
//SwitchLabel[$checkCaseStmt]
[count(following-sibling::BlockStatement except following-sibling::SwitchLabel[1]/following-sibling::BlockStatement) > 1
or (some $stmt (: in only the block statements until the next label :)
in following-sibling::BlockStatement except following-sibling::SwitchLabel[1]/following-sibling::BlockStatement
satisfies not($stmt/Statement/Block))]
]]></value>
</property>
</properties>
<example>
<![CDATA[
while (true) // not recommended
x++;
while (true) { // preferred approach
x++;
}
]]>
</example>
</rule>
<rule name="DefaultPackage"
language="java"
since="3.4"
message="Use explicit scoping instead of the default package private level"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#defaultpackage">
<description>
Use explicit scoping instead of accidental usage of default package private level.
The rule allows methods and fields annotated with Guava's @VisibleForTesting and JUnit 5's annotations.
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration[@Interface= false()]
/ClassOrInterfaceBody
/ClassOrInterfaceBodyDeclaration
[not(Annotation//Name[
pmd-java:typeIs('org.junit.jupiter.api.Test')
or pmd-java:typeIs('org.junit.jupiter.api.RepeatedTest')
or pmd-java:typeIs('org.junit.jupiter.api.ParameterizedTest')
or pmd-java:typeIs('org.junit.jupiter.api.TestFactory')
or pmd-java:typeIs('org.junit.jupiter.api.TestTemplate')
or pmd-java:typeIs('org.junit.jupiter.api.BeforeAll')
or pmd-java:typeIs('org.junit.jupiter.api.AfterAll')
or pmd-java:typeIs('org.junit.jupiter.api.BeforeEach')
or pmd-java:typeIs('org.junit.jupiter.api.AfterEach')
or ends-with(@Image, 'VisibleForTesting')])]
[
FieldDeclaration[@PackagePrivate= true()]
or MethodDeclaration[@PackagePrivate= true()]
]
]]>
</value>
</property>
</properties>
</rule>
<rule name="DontImportJavaLang"
since="0.5"
message="Avoid importing anything from the package 'java.lang'"
class="net.sourceforge.pmd.lang.java.rule.codestyle.DontImportJavaLangRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#dontimportjavalang">
<description>
Avoid importing anything from the package 'java.lang'. These classes are automatically imported (JLS 7.5.3).
</description>
<priority>4</priority>
<example>
<![CDATA[
import java.lang.String; // this is unnecessary
public class Foo {}
// --- in another source code file...
import java.lang.*; // this is bad
public class Foo {}
]]>
</example>
</rule>
<rule name="DuplicateImports"
since="0.5"
message="Avoid duplicate imports such as ''{0}''"
class="net.sourceforge.pmd.lang.java.rule.codestyle.DuplicateImportsRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#duplicateimports">
<description>
Duplicate or overlapping import statements should be avoided.
</description>
<priority>4</priority>
<example>
<![CDATA[
import java.lang.String;
import java.lang.*;
public class Foo {}
]]>
</example>
</rule>
<rule name="EmptyMethodInAbstractClassShouldBeAbstract"
language="java"
since="4.1"
class="net.sourceforge.pmd.lang.rule.XPathRule"
message="An empty method in an abstract class should be abstract instead"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#emptymethodinabstractclassshouldbeabstract">
<description>
Empty or auto-generated methods in an abstract class should be tagged as abstract. This helps to remove their inappropriate
usage by developers who should be implementing their own versions in the concrete subclasses.
</description>
<priority>1</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//ClassOrInterfaceDeclaration[@Abstract = true()]
/ClassOrInterfaceBody
/ClassOrInterfaceBodyDeclaration
/MethodDeclaration[@Abstract = false() and @Native = false()]
[
( boolean(./Block[count(./BlockStatement) = 1]/BlockStatement/Statement/ReturnStatement/Expression/PrimaryExpression/PrimaryPrefix/Literal/NullLiteral) = true() )
or
( boolean(./Block[count(./BlockStatement) = 1]/BlockStatement/Statement/ReturnStatement/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image = '0']) = true() )
or
( boolean(./Block[count(./BlockStatement) = 1]/BlockStatement/Statement/ReturnStatement/Expression/PrimaryExpression/PrimaryPrefix/Literal[string-length(@Image) = 2]) = true() )
or
(./Block[count(./BlockStatement) = 1]/BlockStatement/Statement/EmptyStatement)
or
( not (./Block/*) )
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public abstract class ShouldBeAbstract {
public Object couldBeAbstract() {
// Should be abstract method ?
return null;
}
public void couldBeAbstract() {
}
}
]]>
</example>
</rule>
<rule name="ExtendsObject"
language="java"
since="5.0"
message="No need to explicitly extend Object."
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#extendsobject">
<description>No need to explicitly extend Object.</description>
<priority>4</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//ExtendsList/ClassOrInterfaceType[@Image='Object' or @Image='java.lang.Object']
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo extends Object { // not required
}
]]>
</example>
</rule>
<rule name="FieldDeclarationsShouldBeAtStartOfClass"
language="java"
since="5.0"
message="Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes."
class="net.sourceforge.pmd.lang.java.rule.codestyle.FieldDeclarationsShouldBeAtStartOfClassRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#fielddeclarationsshouldbeatstartofclass">
<description>
Fields should be declared at the top of the class, before any method declarations, constructors, initializers or inner classes.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class HelloWorldBean {
// Field declared before methods / inner classes - OK
private String _thing;
public String getMessage() {
return "Hello World!";
}
// Field declared after methods / inner classes - avoid this
private String _fieldInWrongLocation;
}
]]>
</example>
</rule>
<rule name="FieldNamingConventions"
since="6.7.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.java.rule.codestyle.FieldNamingConventionsRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#fieldnamingconventions">
<description>
Configurable naming conventions for field declarations. This rule reports variable declarations
which do not match the regex that applies to their specific kind ---e.g. constants (static final),
enum constant, final field. Each regex can be configured through properties.
By default this rule uses the standard Java naming convention (Camel case), and uses the ALL_UPPER
convention for constants and enum constants.
</description>
<priority>1</priority>
<example>
<![CDATA[
class Foo {
int myField = 1; // This is in camel case, so it's ok
int my_Field = 1; // This contains an underscore, it's not ok by default
// but you may allow it, or even require the "my_" prefix
final int FinalField = 1; // you may configure a different convention for final fields,
// e.g. here PascalCase: [A-Z][a-zA-Z0-9]*
interface Interface {
double PI = 3.14; // interface "fields" use the constantPattern property
}
enum AnEnum {
ORG, NET, COM; // These use a separate property but are set to ALL_UPPER by default
}
}
]]>
</example>
</rule>
<rule name="ForLoopShouldBeWhileLoop"
language="java"
since="1.02"
message="This for loop could be simplified to a while loop"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#forloopshouldbewhileloop">
<description>
Some for loops can be simplified to while loops, this makes them more concise.
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//ForStatement
[not(LocalVariableDeclaration)]
[not(ForInit)]
[not(ForUpdate)]
[Expression]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
void bar() {
for (;true;) true; // No Init or Update part, may as well be: while (true)
}
}
]]>
</example>
</rule>
<rule name="FormalParameterNamingConventions"
since="6.6.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.java.rule.codestyle.FormalParameterNamingConventionsRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#formalparameternamingconventions">
<description>
Configurable naming conventions for formal parameters of methods and lambdas.
This rule reports formal parameters which do not match the regex that applies to their
specific kind (e.g. lambda parameter, or final formal parameter). Each regex can be
configured through properties.
By default this rule uses the standard Java naming convention (Camel case).
</description>
<priority>1</priority>
<example>
<![CDATA[
class Foo {
abstract void bar(int myInt); // This is Camel case, so it's ok
void bar(int my_i) { // this will be reported
}
void lambdas() {
// lambdas parameters can be configured separately
Consumer<String> lambda1 = s_str -> { };
// lambda parameters with an explicit type can be configured separately
Consumer<String> lambda1 = (String str) -> { };
}
}
]]>
</example>
</rule>
<rule name="GenericsNaming"
language="java"
since="4.2.6"
message="Generics names should be a one letter long and upper case."
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#genericsnaming">
<description>
Names for references to generic values should be limited to a single uppercase letter.
</description>
<priority>4</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//TypeDeclaration/ClassOrInterfaceDeclaration/TypeParameters/TypeParameter[
string-length(@Image) > 1
or
upper-case(@Image) != @Image
]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public interface GenericDao<E extends BaseModel, K extends Serializable> extends BaseDao {
// This is ok...
}
public interface GenericDao<E extends BaseModel, K extends Serializable> {
// Also this
}
public interface GenericDao<e extends BaseModel, K extends Serializable> {
// 'e' should be an 'E'
}
public interface GenericDao<EF extends BaseModel, K extends Serializable> {
// 'EF' is not ok.
}
]]>
</example>
</rule>
<rule name="IdenticalCatchBranches"
language="java"
since="6.4.0"
minimumLanguageVersion="1.7"
message="''catch'' branch identical to ''{0}'' branch"
class="net.sourceforge.pmd.lang.java.rule.codestyle.IdenticalCatchBranchesRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#identicalcatchbranches">
<description>
Identical `catch` branches use up vertical space and increase the complexity of code without
adding functionality. It's better style to collapse identical branches into a single multi-catch
branch.
</description>
<priority>3</priority>
<example>
<![CDATA[
try {
// do something
} catch (IllegalArgumentException e) {
throw e;
} catch (IllegalStateException e) { // Can be collapsed into the previous block
throw e;
}
try {
// do something
} catch (IllegalArgumentException | IllegalStateException e) { // This is better
throw e;
}
]]>
</example>
</rule>
<rule name="LinguisticNaming"
language="java"
since="6.7.0"
message="Linguistics Antipattern - Method name and return type is inconsistent linguistically"
class="net.sourceforge.pmd.lang.java.rule.codestyle.LinguisticNamingRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#linguisticnaming"
typeResolution="true">
<description>
This rule finds Linguistic Naming Antipatterns. It checks for fields, that are named, as if they should
be boolean but have a different type. It also checks for methods, that according to their name, should
return a boolean, but don't. Further, it checks, that getters return something and setters won't.
Finally, it checks that methods, that start with "to" - so called transform methods - actually return
something, since according to their name, they should convert or transform one object into another.
There is additionally an option, to check for methods that contain "To" in their name - which are
also transform methods. However, this is disabled by default, since this detection is prone to
false positives.
For more information, see [Linguistic Antipatterns - What They Are and How
Developers Perceive Them](https://doi.org/10.1007/s10664-014-9350-8).
</description>
<priority>3</priority>
<example>
<![CDATA[
public class LinguisticNaming {
int isValid; // the field name indicates a boolean, but it is an int.
boolean isTrue; // correct type of the field
void myMethod() {
int hasMoneyLocal; // the local variable name indicates a boolean, but it is an int.
boolean hasSalaryLocal; // correct naming and type
}
// the name of the method indicates, it is a boolean, but the method returns an int.
int isValid() {
return 1;
}
// correct naming and return type
boolean isSmall() {
return true;
}
// the name indicates, this is a setter, but it returns something
int setName() {
return 1;
}
// the name indicates, this is a getter, but it doesn't return anything
void getName() {
// nothing to return?
}
// the name indicates, it transforms an object and should return the result
void toDataType() {
// nothing to return?
}
// the name indicates, it transforms an object and should return the result
void grapeToWine() {
// nothing to return?
}
}
]]>
</example>
</rule>
<rule name="LocalVariableCouldBeFinal"
since="2.2"
message="Local variable ''{0}'' could be declared final"
class="net.sourceforge.pmd.lang.java.rule.codestyle.LocalVariableCouldBeFinalRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#localvariablecouldbefinal">
<description>
A local variable assigned only once can be declared final.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Bar {
public void foo () {
String txtA = "a"; // if txtA will not be assigned again it is better to do this:
final String txtB = "b";
}
}
]]>
</example>
</rule>
<rule name="LocalVariableNamingConventions"
since="6.6.0"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.java.rule.codestyle.LocalVariableNamingConventionsRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#localvariablenamingconventions">
<description>
Configurable naming conventions for local variable declarations and other locally-scoped
variables. This rule reports variable declarations which do not match the regex that applies to their
specific kind (e.g. final variable, or catch-clause parameter). Each regex can be configured through
properties.
By default this rule uses the standard Java naming convention (Camel case).
</description>
<priority>1</priority>
<example>
<![CDATA[
class Foo {
void bar() {
int localVariable = 1; // This is in camel case, so it's ok
int local_variable = 1; // This will be reported unless you change the regex
final int i_var = 1; // final local variables can be configured separately
try {
foo();
} catch (IllegalArgumentException e_illegal) {
// exception block parameters can be configured separately
}
}
}
]]>
</example>
</rule>
<rule name="LongVariable"
language="java"
since="0.3"
message="Avoid excessively long variable names like {0}"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#longvariable">
<description>
Fields, formal arguments, or local variable names that are too long can make the code difficult to follow.
</description>
<priority>3</priority>
<properties>
<property name="minimum" type="Integer" description="The variable length reporting threshold" min="1" max="100" value="17"/>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//VariableDeclaratorId[string-length(@Name) > $minimum]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Something {
int reallyLongIntName = -3; // VIOLATION - Field
public static void main( String argumentsList[] ) { // VIOLATION - Formal
int otherReallyLongName = -5; // VIOLATION - Local
for (int interestingIntIndex = 0; // VIOLATION - For
interestingIntIndex < 10;
interestingIntIndex ++ ) {
}
}
]]>
</example>
</rule>
<rule name="MethodArgumentCouldBeFinal"
since="2.2"
message="Parameter ''{0}'' is not assigned and could be declared final"
class="net.sourceforge.pmd.lang.java.rule.codestyle.MethodArgumentCouldBeFinalRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#methodargumentcouldbefinal">
<description>
A method argument that is never re-assigned within the method can be declared final.
</description>
<priority>3</priority>
<example>
<![CDATA[
public void foo1 (String param) { // do stuff with param never assigning it
}
public void foo2 (final String param) { // better, do stuff with param never assigning it
}
]]>
</example>
</rule>
<rule name="MethodNamingConventions"
since="1.2"
message="The {0} name ''{1}'' doesn''t match ''{2}''"
class="net.sourceforge.pmd.lang.java.rule.codestyle.MethodNamingConventionsRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#methodnamingconventions">
<description>
Configurable naming conventions for method declarations. This rule reports
method declarations which do not match the regex that applies to their
specific kind (e.g. JUnit test or native method). Each regex can be
configured through properties.
By default this rule uses the standard Java naming convention (Camel case).
</description>
<priority>1</priority>
<example>
<![CDATA[
public class Foo {
public void fooStuff() {
}
}
]]>
</example>
</rule>
<rule name="MIsLeadingVariableName"
language="java"
since="3.4"
deprecated="true"
message="Avoid naming non-fields with the prefix 'm_'"
class="net.sourceforge.pmd.lang.rule.XPathRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#misleadingvariablename">
<description>
Detects when a non-field has a name starting with 'm_'. This usually denotes a field and could be confusing.
This rule is deprecated and will be removed with PMD 7.0.0. The rule is replaced
by the more general rule
{% rule java/codestyle/LocalVariableNamingConventions %}.
</description>
<priority>3</priority>
<properties>
<property name="version" value="2.0"/>
<property name="xpath">
<value>
<![CDATA[
//VariableDeclaratorId
[starts-with(@Name, 'm_')]
[not (../../../FieldDeclaration)]
]]>
</value>
</property>
</properties>
<example>
<![CDATA[
public class Foo {
private int m_foo; // OK
public void bar(String m_baz) { // Bad
int m_boz = 42; // Bad
}
}
]]>
</example>
</rule>
<rule name="NoPackage"
language="java"
since="3.3"
message="All classes, interfaces, enums and annotations must belong to a named package"