GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — pmd/pmd-java/src/main/resources/category/java/codestyle.xml at main · pmd/pmd Expand file tree/ Copy pathcodestyle.xmlMore file actions 2574 lines (2320 loc) · 96.8 KB / Copy pathcodestyle.xmlFile metadata and controls2574 lines (2320 loc) · 96.8 KB 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"?> <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 name="AtLeastOneConstructor" language="java" since="1.04" message="Each class should declare at least one constructor" class="net.sourceforge.pmd.lang.java.rule.codestyle.AtLeastOneConstructorRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#atleastoneconstructor"> <description> < to detect those. ]]> </description> <priority>3</priority> <example> <![CDATA[ public class Foo { // missing constructor public void doSomething() { ... } public void doOtherThing { ... } } ]]> </example> </rule> <rule name="AvoidDollarSigns" language="java" since="1.5" message="Avoid using dollar signs in variable/method/class/interface names" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" 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> <properties> <property name="xpath"> <value> <![CDATA[ //ClassDeclaration [contains(@SimpleName, '$')] | //EnumDeclaration [contains(@SimpleName, '$')] | //AnnotationTypeDeclaration [contains(@SimpleName, '$')] | //RecordDeclaration [contains(@SimpleName, '$')] | //VariableId [contains(@Name, '$')] | //MethodDeclaration [contains(@Name, '$')] ]]> </value> </property> </properties> <example> <![CDATA[ public class Fo$o { // not a recommended name } ]]> </example> </rule> <rule name="AvoidProtectedFieldInFinalClass" language="java" since="2.1" deprecated="true" message="Avoid protected fields in a final class. Change to private or package access." class="net.sourceforge.pmd.lang.rule.xpath.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. **Deprecated:** This rule is deprecated since PMD 7.27.0 and will be removed with PMD 8.0.0. This rule has been replaced by {% rule java/design/ProtectedMemberInFinalClass %}, which covers all class members. </description> <priority>3</priority> <properties> <property name="xpath"> <value> <![CDATA[ //ClassDeclaration[@Final = true()] /ClassBody /FieldDeclaration[@Visibility = "protected"] ]]> </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" deprecated="true" 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.xpath.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. **Deprecated:** This rule is deprecated since PMD 7.27.0 and will be removed with PMD 8.0.0. This rule has been replaced by {% rule java/design/ProtectedMemberInFinalClass %}, which covers all class members. </description> <priority>3</priority> <properties> <property name="xpath"> <value> <![CDATA[ //ClassDeclaration[@Final= true() and not(ExtendsList)] /ClassBody /MethodDeclaration[@Visibility="protected" 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.xpath.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="xpath"> <value>//MethodCall[TypeExpression/ClassType[pmd-java:typeIs('java.lang.System')] and @MethodName = '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 or Boolean should be named 'isX()'" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#booleangetmethodname"> <description> Methods that return boolean or 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. By default, methods returning the Boolean wrapper type are checked as well. This can be disabled with the includeWrappedType property. </description> <priority>4</priority> <properties> <property name="xpath"> <value> <![CDATA[ //MethodDeclaration [starts-with(@Name, 'get')] [@Arity = 0 or $checkParameterizedMethods = true()] [ (PrimitiveType[@Kind = 'boolean'] or ($includeWrappedType and ClassType[pmd-java:typeIs('java.lang.Boolean')])) and @Override = false() ] ]]> </value> </property> <property name="checkParameterizedMethods" type="Boolean" description="Check parameterized methods" value="false"/> <property name="includeWrappedType" type="Boolean" description="Include methods returning the Boolean wrapper type" value="true"/> </properties> <example> <![CDATA[ public boolean getFoo(); // bad public Boolean getFoo(); // bad if includeWrappedType=true (default) public boolean isFoo(); // ok 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.xpath.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="xpath"> <value> <![CDATA[ //ClassDeclaration[ExtendsList/*] /ClassBody /ConstructorDeclaration[ not(Block/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" language="java" 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). The rule can detect utility classes and enforce a different naming convention on those. E.g. setting the property `utilityClassPattern` to `[A-Z][a-zA-Z0-9]+(Utils?|Helper|Constants)` reports any utility class, whose name does not end in "Util(s)", "Helper" or "Constants". A class is a utility class, if and only if it fulfills ALL the following criteria: * ALL member functions, member variables, nested classes, and initializers are static. * The class has at least one member function, member variable, or nested class that is not private. * The class is a concrete class (neither abstract nor an interface). * The class has no superclasses and implements no interfaces. * The class has no main method. This rule detects test classes using the following convention: Test classes are top-level classes, that either inherit from JUnit 3 TestCase or have at least one method annotated with the Test annotations from JUnit4/Jupiter or TestNG. </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" language="java" since="5.4.0" class="net.sourceforge.pmd.lang.java.rule.codestyle.CommentDefaultAccessModifierRule" message="Missing commented default access modifier on {0} ''{1}''" 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 its 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 or any JUnit Jupiter/TestNG 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" language="java" 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?". Since null checks are commonly considered a positive condition, they are by default ignored by this rule. This means both `a == null` and `a != null` are allowed. If you want to enforce either style and disallow the other, use the property `nullCheckBranch`. </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.xpath.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="xpath"> <value><![CDATA[ //WhileStatement[$checkWhileStmt and not(Block) and not($allowEmptyLoop and EmptyStatement)] | //ForStatement[$checkForStmt and not(Block) and not($allowEmptyLoop and EmptyStatement)] | //ForeachStatement[$checkForStmt and not(Block) and not($allowEmptyLoop and EmptyStatement)] | //DoStatement[$checkDoWhileStmt and not(Block) and not($allowEmptyLoop and EmptyStatement)] | (: The violation is reported on the sub statement -- not the if statement :) //IfStatement[$checkIfElseStmt] /*[position() > 1 and not(self::Block or self::IfStatement)] [ $checkSingleIfStmt (: Inside this (...) is the definition of a "single if statement" :) or not(parent::*/@Else = false() (: No else stmt :) (: Not the last branch of an 'if ... else if' chain :) and not(parent::IfStatement[parent::IfStatement]))] | (: Reports case labels if one of their subordinate statements is not braced :) //SwitchFallthroughBranch[$checkCaseStmt] [count(*) > 1 and (count(*) > 2 or not(child::*[2]/self::Block))] ]]></value> </property> </properties> <example> <![CDATA[ while (true) // not recommended x++; while (true) { // preferred approach x++; } ]]> </example> </rule> <rule name="CStyleArrayDeclaration" language="java" since="7.28.0" message="Use Java-style array declarations instead of C-style array declarations" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#cstylearraydeclaration"> <description><![CDATA[ Use Java-style declarations for arrays, where the brackets follow the type, e.g. `int[] numbers` instead of `int numbers[]`. This applies to fields, local variables, parameters, and methods returning arrays. ]]></description> <priority>3</priority> <properties> <property name="xpath"> <value><![CDATA[ //VariableId[ArrayDimensions] | //MethodDeclaration[ArrayDimensions] ]]></value> </property> </properties> <example> <![CDATA[ class Numbers { double doubles[]; // bad int[] ints; // good double getDoubles()[] { // bad return new double[] { 1.0 }; } int[] getInts() { // good return new int[] { 1 }; } } ]]> </example> </rule> <rule name="EmptyControlStatement" language="java" since="6.46.0" message="This control statement has an empty branch" class="net.sourceforge.pmd.lang.java.rule.codestyle.EmptyControlStatementRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#emptycontrolstatement"> <!-- Note about naming: EmptyCodeBlock does not work because the rule flags `if(true);` --> <!-- EmptyControlStatement is weird because `{}` is not a control statement, and we also flag initializers. --> <!-- EmptyStatement does not work because `;` is called an "empty statement" and is not flagged by this rule, rather, by UnnecessarySemicolon. --> <!-- EmptyCodeConstruct would work but sounds a bit odd (right?). --> <description><![CDATA[ Reports control statements whose body is empty, as well as empty initializers. The checked code constructs are the following: - bodies of `try` statements - `finally` clauses of `try` statements - `switch` statements - `synchronized` statements - `if` statements - loop statements: `while`, `for`, `do .. while` - initializers - blocks used as statements (for scoping) This rule replaces the rules EmptyFinallyBlock, EmptyIfStmt, EmptyInitializer, EmptyStatementBlock, EmptySwitchStatements, EmptySynchronizedBlock, EmptyTryBlock, and EmptyWhileStmt. Notice that {% rule java/errorprone/EmptyCatchBlock %} is still an independent rule. EmptyStatementNotInLoop is replaced by {% rule java/codestyle/UnnecessarySemicolon %}. ]]></description> <priority>3</priority> <example> <![CDATA[ class Foo { { if (true); // empty if statement if (true) { // empty as well } } {} // empty initializer } ]]> </example> </rule> <rule name="EmptyMethodInAbstractClassShouldBeAbstract" language="java" since="4.1" class="net.sourceforge.pmd.lang.rule.xpath.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="xpath"> <value> <![CDATA[ //ClassDeclaration[@RegularClass = true() and pmd-java:modifiers() = "abstract"] /ClassBody /MethodDeclaration [@Final = false()] [Block[ let $size := count(*[not(self::EmptyStatement)]) return $size = 0 or $size = 1 and ReturnStatement[NullLiteral or NumericLiteral[@ValueAsInt = 0] or StringLiteral[@Empty = true()]] ]] ]]> </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.xpath.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="xpath"> <value> <![CDATA[ //ExtendsList/ClassType[pmd-java:typeIsExactly('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" language="java" 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="FinalParameterInAbstractMethod" language="java" since="6.42.0" message="Final parameter in abstract method" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" typeResolution="true" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#finalparameterinabstractmethod"> <description> Declaring a method parameter as final for an interface method is useless because the implementation may choose to not respect it. </description> <priority>1</priority> <properties> <property name="xpath"> <value> <![CDATA[ //MethodDeclaration [FormalParameters/FormalParameter[@Final = true()]] [not(Block)] ]]> </value> </property> </properties> <example> <![CDATA[ public interface MyInterface { void process(final Object arg); // Avoid using final here } ]]> </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.xpath.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="xpath"> <value> <![CDATA[ //ForStatement[not(ForInit | ForUpdate) and count(*) = 2] ]]> </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" language="java" 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" deprecated="true" message="Generics names should be a one letter long and upper case." class="net.sourceforge.pmd.lang.rule.xpath.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. **Deprecated:** This rule is deprecated since PMD 7.17.0 and will be removed with PMD 8.0.0. This rule has been replaced by {% rule TypeParameterNamingConventions %}, which provides more comprehensive and configurable naming conventions for type parameters. </description> <priority>4</priority> <properties> <property name="xpath"> <value> <![CDATA[ //TypeParameter[ string-length(@Name) > 1 or upper-case(@Name) != @Name ] ]]> </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="LambdaCanBeMethodReference" language="java" since="7.1.0" message="Lambda expression could be written as a method reference: `{0}`" class="net.sourceforge.pmd.lang.java.rule.codestyle.LambdaCanBeMethodReferenceRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#lambdacanbemethodreference"> <description> This rule reports lambda expressions that can be written more succinctly as a method reference. This is the case if the lambda is an expression lambda that only calls one method, passing the entire lambda parameter list in order to the method. For instance: ```java x -> Foo.call(x) // can be Foo::call x -> call(x) // can be this::call, if call is an instance method (x, y, z) -> call(x, y, z) // can be this::call () -> foo.get() // can be foo::get x -> x.foo() // can be XType::foo (where XType is the type of x) ``` In some cases rewriting a lambda to a method reference can change the semantics of the code. For instance in `(x) -> someVar.call(x)`, the invocation of the lambda may produce a NullPointerException (NPE) if `someVar` is null. The method reference `someVar::call` will also NPE if `someVar` is null, but it will do so at the point the method reference is created, while the lambda is created without error and its NPE is only thrown if the lambda is invoked (which may be never). Code should probably not rely on this subtle semantic difference, therefore these potentially problematic lambdas are also reported by default. This behavior can be disabled by setting the property `ignoreIfMayNPE` to `true`. The property `ignoreIfMayNPE` is true by default. By default, calls whose receiver is itself a method call are ignored, because they could cause side effects. This may be changed by setting the property `ignoreIfReceiverIsMethod` to `false`. Scope limitations: - This rule will not report lambdas of the form `x -> new CtorCall().something(x)`, because the semantics of the method reference would be to create a single new object, while the lambda creates one object per invocation. - The rule cannot know if the qualifier of a method call performs side effects. This means `(x) -> sideEffectingMethod().foo(x)` will be reported. Suppress the warning in this case. </description> <priority>3</priority> <example> <![CDATA[ import java.util.stream.Stream; public class LambdaCanBeMethodReference { static { Stream.of("abc", "d") .mapToInt(s -> s.length()) // could be String::length .reduce((x, y) -> Integer.sum(x, y)) // could be Integer::sum .getAsInt(); } } ]]> </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"> <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="LocalHomeNamingConvention" language="java" since="4.0" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" message="The Local Home interface of a Session EJB should be suffixed by 'LocalHome'" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#localhomenamingconvention"> <description> The Local Home interface of a Session EJB should be suffixed by 'LocalHome'. </description> <priority>4</priority> <properties> <property name="xpath"> <value> <![CDATA[ //ClassDeclaration [ pmd-java:typeIs('javax.ejb.EJBLocalHome') and not(ends-with(@SimpleName, 'LocalHome')) ] ]]> </value> </property> </properties> <example> <![CDATA[ public interface MyBeautifulLocalHome extends javax.ejb.EJBLocalHome {} // proper name public interface MissingProperSuffix extends javax.ejb.EJBLocalHome {} // non-standard name ]]> </example> </rule> <rule name="LocalInterfaceSessionNamingConvention" language="java" since="4.0" class="net.sourceforge.pmd.lang.rule.xpath.XPathRule" message="The Local Interface of a Session EJB should be suffixed by 'Local'" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_codestyle.html#localinterfacesessionnamingconvention"> <description> The Local Interface of a Session EJB should be suffixed by 'Local'. </description> <priority>4</priority> <properties> <property name="xpath"> <value> <![CDATA[ //ClassDeclaration [ pmd-java:typeIs('javax.ejb.EJBLocalObject') and not(ends-with(@SimpleName, 'Local')) ] ]]> </value> </property> </properties> <example> <![CDATA[ public interface MyLocal extends javax.ejb.EJBLocalObject {} // proper name public interface MissingProperSuffix extends javax.ejb.EJBLocalObject {} // non-standard name |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |