PMD detects source code issues in Apex and other languages and looks for unexpected problems like Possible bugs, Dead code, Suboptimal code, Over complicated expressions, and Duplicate code.
Install the “Eclipse IDE” software from Google.
Eclipse version 4.4 and later versions.
1) Once the “Eclipse” is installed, we must install “PMD” latest version, Eclipse [Symbol] Help [Symbol] Install new software
2) Enable PMD using “Add Repository” and also add the location URL for PMD (it will be available in PMD website).
3) Choose the current PMD version for “Available software”, click the “PMD for eclipse 4” check box, and finally click the “Finish” button.
Once we have installed the PMD in Eclipse, PMD folder will be automatically created in our local system. The folder has rules, Sample and report files. We can store Apex classes and controllers in the Sample folder.
4) Choose the Apex class and right click select PMD[Symbol]Check Code, it will show the violation view.
5) Standard Rule Customization:
PMD provides some standard rule sets (ApexUnit, Braces, Complexity, Performance, Security, Style). The ruleset can check the code and show the error messages. We can run the ruleset using “Command Prompt”.
Example Standard rule set:
The following XML ruleset is used to find the deeply nested “if” statements in our Apex code.
<?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 </description> <priority>3</priority> <properties> <property name="problemDepth" value="3"/> <property name="cc_categories" value="Complexity"/> <property name="cc_remediation_points_multiplier" value="150"/> <property name="cc_block_highlighting" value="false"/> </properties> </rule> </ruleset>
Here, we have changed(Customize) the problemDepth, cc_categories, cc_remediation_points_multiplier and cc_block_highlighting values based on our requirements.
Once we have run the command prompt, the error messages will be shown in our browser, and create error file in report folder.
This error message for the Apex classes will be shown as below:
6) CPD (Copy Paste Detector):
If any developer uses same code in multiple classes, then CPD will detect it. Eclipse [Symbol] Window [Symbol] Preferences [Symbol] PMD [Symbol] CPD Preferences [Symbol] Launch CPD
CPD also shows the classes which use same code from other classes.
- Run many rulesets at once.
- Execute with ANT tasks, maven Plugins, IDE plugins.
- Run on Files, Directories, jars and zip files.
- All rulesets on a single file.
- Cannot find runtime issues.
- Does not detect synchronization and threading issues.
This article illustrates the use of PMD tool and how to apply these things in our projects to analyze code and find the errors.