New PMD Rules

Updated 1 month ago by Copado Solutions

Copado now supports PMD version 6.25. As part of this upgrade, the following rules have been added to the default RuleSet:

  • ApexAssertionsShouldIncludeMessage
  • ApexUnitTestMethodShouldHaveIsTestAnnotation
  • DebugsShouldUseLoggingLevel
  • UnusedLocalVariable
  • FieldDeclarationsShouldBeAtStart
  • CognitiveComplexity
  • TestMethodsMustBeInTestClasses
  • AvoidSoslInLoops

In order to ensure these new rules are available, follow the steps below:

  1. Navigate to Setup > Object Manager > Static Code Analysis Rule.
  2. Go to Fields & Relationships and click on Rule Name.
  3. Scroll down to Values and click on New to add the new rules.
  4. Add the new rule names as picklist values (note that this name is case sensitive).
  5. Once you have added all the new picklist values, go to the Static Code Analysis Setting record and create static code analysis rules for the new values added. To do this, choose one of the options below:
    1. Manually create the static code analysis rules:
      1. Select the name of the rule you have added in the Rule Name field.
      2. Add a priority. You can use the one defined by PMD in their documentation or define a custom priority that suits you better.
      3. Enter a message.
      4. Repeat this process for all the other new rules.
    2. Leverage the Generate Default RuleSet button on the Static Code Analysis Settings record. If you want to use this option, be aware that any configurations you have made to your PMD rules will be overwritten. To avoid losing the existing configuration, follow the steps below:
      1. Export your current ruleset (Static Code Analysis Rule records) with the relevant fields:
        Message (copado__Message__c)
        Rule Name (copado__Rule_Name__c)
        Priority (copado__Priority__c)
      2. Use the Generate Default RuleSet button to recreate the ruleset.
      3. Once the ruleset is generated, export the Id and the Rule Name (copado__Rule_Name__c) of the generated rules.
      4. In a spreadsheet, use the Rule Name to match rules (you can use the Vlookup formula) and retrieve from your ruleset’s backup (step 1) the corresponding Message and Priority values. 
      5. Update the resulting file.


How did we do?