0

Am trying do a PMD analysis with a custom rule created. But while doing the same I am getting class not found exception even when class file is present in mentioned location.

Rule xml:

 <?xml version="1.0"?>
<ruleset name="AvoidMethodswithexcessivelength"
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>
              Avoid using methods with more than 60 lines
              </description>
            <rule name="AvoidMethodswithexcessivelength"
            language="apex"
                  message="Avoid using methods with more than 60 lines"
                  class="target.classes.ResponseAspect">
              <description>
              Avoid using methods with more than 60 lines
              </description>
                <priority>3</priority>

              <example>
       public class Foo {
    public void bar(Integer x, Integer y) {
        if (x>y) {
            System.debug('xxxxx');
            System.debug('xxxxx');
            System.debug('xxxxx');
            System.debug('xxxxx');
            System.debug('xxxxx');
            System.debug('xxxxx');
            System.debug('xxxxx');
            System.debug('xxxxx');
            //repeat this 60 times
        }
    }
}
              </example>
            </rule>
        </ruleset>
priya
  • 391
  • 1
  • 5
  • 24
  • The class attribute points to the class implementing the rule, not the class to be analyzed. As an implementation, it should be included as part of the classpath when running PMD, how to do se depends on how you are running PMD. If it's from CLI, simply dropping the jar under the libs folder should suffice. Please, refer to the documentation on writing rules: https://pmd.github.io/pmd-6.24.0/pmd_userdocs_extending_writing_rules_intro.html – Johnco May 27 '20 at 15:48
  • Hello, now am not getting any errors but the pmd analysis report is empty. – priya May 28 '20 at 08:02
  • I have generated the jar file and placed in lib section of pmd folder. Ran the below command -> pmd.bat pathtomysourcefile -f xml -R pathtomyruleset – priya May 28 '20 at 08:03
  • if it does not fail then the rule is being loaded (good!), but if no violations are shown and you expect some, then your rule is probably buggy. You should focus on polishing your rule logic. – Johnco May 28 '20 at 15:14
  • So the issue was , the test file I was giving has wrong extention..it should have been .cls as its an apex code. Fixed it, Thankyou – priya Jun 04 '20 at 12:32

0 Answers0