0

I am running pmd command to fetch the report in html format but every time it gives me error

Following command is used

pmd.bat -d D:\TestSBX02\AllData\force-app\main\default\classes -f html -R D:\pmd-bin-6.16.0\Rules\apexRules.xml -reportfile D:\pmd-bin-6.16.0\Rules.html

New Error received

es.html
Sep 17, 2019 9:29:57 PM net.sourceforge.pmd.rules.RuleFactory buildRule
SEVERE: Error instantiating a rule
java.lang.ClassNotFoundException: net.sourceforge.pmd.lang.apex.rule.complexity.AvoidDeeplyNestedIfStmtsRule
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at net.sourceforge.pmd.rules.RuleBuilder.build(RuleBuilder.java:180)
        at net.sourceforge.pmd.rules.RuleFactory.buildRule(RuleFactory.java:189)
        at net.sourceforge.pmd.RuleSetFactory.parseSingleRuleNode(RuleSetFactory.java:551)
        at net.sourceforge.pmd.RuleSetFactory.parseRuleNode(RuleSetFactory.java:450)
        at net.sourceforge.pmd.RuleSetFactory.parseRuleSetNode(RuleSetFactory.java:367)
        at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:214)
        at net.sourceforge.pmd.RuleSetFactory.createRuleSet(RuleSetFactory.java:209)
        at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:170)
        at net.sourceforge.pmd.RuleSetFactory.createRuleSets(RuleSetFactory.java:154)
        at net.sourceforge.pmd.RulesetsFactoryUtils.getRuleSets(RulesetsFactoryUtils.java:38)
        at net.sourceforge.pmd.RulesetsFactoryUtils.getRuleSetsWithBenchmark(RulesetsFactoryUtils.java:67)
        at net.sourceforge.pmd.PMD.doPMD(PMD.java:206)
        at net.sourceforge.pmd.PMD.run(PMD.java:485)
        at net.sourceforge.pmd.cli.PMDCommandLineInterface.run(PMDCommandLineInterface.java:165)
        at net.sourceforge.pmd.PMD.main(PMD.java:455)

Apexrule used

    <?xml version="1.0"?>
<ruleset name="Apex Rules" 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">

    <rule name="AvoidDeeplyNestedIfStmts" message="Avoid creating deeply nested if-then statements since they are harder to read and error-prone to maintain." class="net.sourceforge.pmd.lang.apex.rule.complexity.AvoidDeeplyNestedIfStmtsRule">
      <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(Integer x, Integer y, Integer z) { if (x>y) {
                        if (y>z) {
                            if (z==x) {
                                // !! too deep
                            }
                        }
                    }
                }
            }
        ]]>
      </example>
    </rule>

    <rule name="ExcessiveParameterList" message="Methods with numerous parameters are a challenge to maintain, especially if most of them share the same datatype. These situations usually denote the need for new objects to wrap the numerous parameters." class="net.sourceforge.pmd.lang.apex.rule.complexity.ExcessiveParameterListRule">
      <description>
      Methods with numerous parameters are a challenge to maintain, especially if most of them share the same datatype. These situations usually denote the need for new objects to wrap the numerous parameters.
      </description>
        <priority>3</priority> 
      <example>
        <![CDATA[ 
// too many arguments liable to be mixed up 
public void addPerson(int birthYear, int birthMonth, int birthDate, int height, int weight, int ssn) 
{ ... } 

// preferred approach public void addPerson(Date birthdate, BodyMeasurements measurements, int ssn) 
{ ... } 
]]>
      </example>
    </rule>

     <rule name="ExcessiveClassLength" message="Excessive class file lengths are usually indications that the class may be burdened with excessive responsibilities that could be provided by external classes or functions. In breaking these methods apart the code becomes more managable and ripe for reuse." class="net.sourceforge.pmd.lang.apex.rule.complexity.ExcessiveClassLengthRule">
      <description>
      Excessive class file lengths are usually indications that the class may be burdened with excessive responsibilities that could be provided by external classes or functions. In breaking these methods apart the code becomes more managable and ripe for reuse.
      </description>
        <priority>3</priority> 
      <example>
        <![CDATA[ 
public class Foo { public void bar(Integer x, Integer y, Integer z) { if (x>y) {
                        if (y>z) {
                            if (z==x) {
                                // !! too deep
                            }
                        }
                    }
                }
            }
        ]]>
      </example>
    </rule>

    <rule name="NcssMethodCount" message="This rule uses the NCSS (Non-Commenting Source Statements) algorithm to determine the number of lines of code for a given method. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one." class="net.sourceforge.pmd.lang.apex.rule.complexity.NcssMethodCountRule">
      <description>
      This rule uses the NCSS (Non-Commenting Source Statements) algorithm to determine the number of lines of code for a given method. NCSS ignores comments, and counts actual statements. Using this algorithm, lines of code that are split are counted as one.
      </description>
        <priority>3</priority> 
      <example>
        <![CDATA[ 
public class Foo extends Bar 
{ 
//this method only has 1 NCSS lines 
public Integer methd() { 
super.methd(); 
return 1; 
} } 
]]>
      </example>
    </rule> 
</ruleset>
4
  • Where is the file apexRules.xml? PMD seems to be saying it cannot find it.
    – David Reed
    Sep 17, 2019 at 15:52
  • Yes i corrected the path now facing different error Sep 17, 2019 at 16:02
  • Okay, please edit your question to clearly describe what you're struggling with.
    – David Reed
    Sep 17, 2019 at 16:07
  • I have edited the question problem is with apex ruleset can you check Sep 17, 2019 at 16:12

1 Answer 1

1

You're using a rule set file that is out of date, presumably designed for an older version of PMD.

The class path for AvoidDeeplyNestedIfStmts, given as net.sourceforge.pmd.lang.apex.rule.complexity.AvoidDeeplyNestedIfStmtsRule, is wrong. That class is implemented at net.sourceforge.pmd.lang.apex.rule.design.AvoidDeeplyNestedIfStmtsRule.

However, you don't need to reference the class path to use it. Use the syntax given in the PMD documentation linked above:

Use this rule with the default properties by just referencing it:

<rule ref="category/apex/design.xml/AvoidDeeplyNestedIfStmts" />

Use this rule and customize it:

<rule ref="category/apex/design.xml/AvoidDeeplyNestedIfStmts">
    <properties>
        <property name="problemDepth" value="3" />
    </properties>
</rule>

Likewise reference the other Apex and Visualforce rules you wish to apply, following the syntax given in the PMD docs.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.