Backend Development 11 min read

Extending Alibaba Java Coding Guidelines (p3c) with Custom Rules: A Step‑by‑Step Guide

This article explains why coding standards are essential, introduces PMD and Alibaba's p3c plugin, and provides a detailed, code‑rich tutorial on designing, implementing, testing, and packaging custom Java static‑analysis rules to enforce @RequestMapping conventions.

Sohu Tech Products
Sohu Tech Products
Sohu Tech Products
Extending Alibaba Java Coding Guidelines (p3c) with Custom Rules: A Step‑by‑Step Guide

Many companies adopt Alibaba's Java coding standards, but without enforcement the rules become ineffective; this guide shows how to extend the p3c plugin with custom rules to automate compliance.

Why enforce standards? Code is ultimately maintained by humans, so readability and consistency are crucial. Tools like PMD and p3c can automatically detect violations.

PMD basics – PMD parses Java source into an abstract syntax tree (AST) using JavaCC, offering built‑in rule sets for bugs, unused code, style, design, performance, and security. Users can also write custom XPath‑based rules.

p3c plugin structure – The plugin consists of three parts: p3c‑pmd (rule implementations based on PMD), idea‑plugin (IntelliJ IDEA integration), and configuration files such as ali‑oop.xml .

Custom rule design – Example: enforce that @RequestMapping annotations specify the method attribute. The rule class RequestMappingRule extends AbstractAliRule and uses XPath expressions to locate imports and annotations, adding violations when the method is missing.

Implementation snippet : public class RequestMappingRule extends AbstractAliRule { private static final String IMPORT_XPATH = "//ImportDeclaration[@ImportedName='org.springframework.web.bind.annotation.RequestMapping']"; private static final String REQUESTMAPPING_XPATH = "//ClassOrInterfaceBody//Annotation[@AnnotationName='RequestMapping']"; private static final String METHOD_XPATH = "MemberValuePairs/MemberValuePair[@MemberName='method']"; @Override public Object visit(ASTCompilationUnit node, Object data) { // locate import and annotation nodes, check for METHOD_XPATH, addViolationWithMessage(...); return super.visit(node, data); } }

Testing – Write PMD test‑data XML files containing sample controller code, specify expected problems and line numbers, and run mvn test to verify the rule behaves as intended.

Packaging – Build the p3c‑pmd module with mvn -DskipTests=true clean install , then package the IDEA plugin via Gradle ( gradle clean buildPlugin ). The resulting Alibaba Java Coding Guidelines-1.0.0.zip can be installed in IntelliJ.

By following these steps developers can quickly create, test, and distribute their own static‑analysis rules, ensuring code adheres to agreed‑upon standards with minimal manual effort.

javaGradlemavencode qualitystatic analysisPMDCustom RulesP3C
Sohu Tech Products
Written by

Sohu Tech Products

A knowledge-sharing platform for Sohu's technology products. As a leading Chinese internet brand with media, video, search, and gaming services and over 700 million users, Sohu continuously drives tech innovation and practice. We’ll share practical insights and tech news here.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.