How to create PMD custom rule

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

PMDProgramming 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
Abstract Syntax Trees
Abstract Syntax Trees

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.

YouTube video

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

Reference Material

Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 463

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *