Codify and Enforce your Organization’s Apex Standards. Most of us are familiar with PMD warnings when writing Apex in Visual Studio Code, but PMD can do much more than that. Join us as we learn how to craft our own rules using Java and enforce them automatically when code changes are proposed. Let see how to create PMD custom rule.
Introduction to PMD.
PMD stand for Programming Mistakes Detector. Apex PMD is Open source static code analysis tool which examines the structure of your Apex class files. Identifies structural issues within code such as:
- Improper capitalization
- Queries/DML in loops
- Lacking comments
PMD – Programming Mistakes Detector
Includes multiple rules out of the box Rules have predefined priority:
- 1 highest priority
- 5 lowest priority
Out of the box rules are not an exhaustive list of all rules you might want to enforce.
How to create PMD custom rule
You’ll want to expand beyond the included rules if any of these are true:
- Your organization likely has a defined style guide
- Certain issues keep popping up during code review
- Code issues not caught by out of the box rules have caused production issues
Abstract Syntax Trees
An abstract syntax tree is a syntactic representation of the structure of a program’s source code text. Each node of the tree corresponds to a structural element within the source code.
Abstract means that not all details are represented – just structural details.
PMD uses abstract syntax trees to understand the structure of your Apex class. Nodes represent things like:
- Methods
- Parameters
- Comments
- Annotations
- Inner classes
You need to understand the below tools and platform to build the custom PMD rules.
Java Basics
Released by Sun Microsystems in 1995. The most popular Object Oriented Programming language of the last 25 years. Originally created for interactive cable TV boxes. Apex syntax is based on Java.
- The .java file extension is for Java class files.
- The javac (compile) command compiles Java source code into Java bytecode represented with .class file extension
- Multiple compiled files can be bundled into a Java archive .jar file and executed.
Maven Basics
Open source build tool owned and maintained by Apache. Simplifies the act of bundling, compiling, documenting, packaging, and installing source code. Maven repository makes dependency management much easier.
- Each maven project contains a Project Object Model XML file named pom.xml which defines all of the configuration, versioning, dependencies, and building steps for a project
- The maven repository stores packages available for distribution like JUnit and PMD
- The mvn package command compiles, verifies, and builds the project, then places the .jar file into the target directory
Custom APEX PMD Rule Use case
Do not allow the use of the @Future annotation within a class. Provide the reader of the finding with instructions to use the Queueable interface instead.
Require the attachment of a Finalizer within each Queueable class. Explain that the lack of a Finalizer provides no reporting capabilities should the queueable action fail.
Local Execution
- Download and unzip PMD version 6.55.0
- Then open VS Code Settings -> Extensions -> Apex PMD Configuration
- Set Pmd Bin Path to the path to pmd-bin-6.55.0.
- Add the pmd-rules-1.0.0.jar to the Additional Class Paths
- Set Rulesets path to the allRules.xml ruleset
Nice article, but technically, PMD does not stand for anything specific. Since the authors did not name it, we should not either. https://docs.pmd-code.org/latest/pmd_projectdocs_trivia_meaning.html
Amazing article Mitch!