Backend Development 9 min read

Static and Dynamic Code Inspection for Reducing Java Backend Code Corruption

This article explains how to use static analysis tools like IDEA and PMD together with dynamic coverage tools such as JaCoCo to identify, clean up, and refactor unused, duplicated, or dead Java code in large backend projects, improving maintainability and stability.

JD Retail Technology
JD Retail Technology
JD Retail Technology
Static and Dynamic Code Inspection for Reducing Java Backend Code Corruption

The article introduces the problem of code bloat in growing Java backend projects, where useful, unused, low‑frequency, and experimental code accumulate, increasing maintenance cost and risking corruption that may eventually require a full rewrite.

It outlines a three‑step static code inspection process: (1) using IntelliJ IDEA's built‑in inspection to find unused classes, variables, and methods; (2) installing and configuring the PMD plugin with custom rulesets to detect empty blocks, unused imports, and other code smells; (3) employing CPD for duplicate‑code detection. Advantages and drawbacks of each tool are discussed, including false positives from Spring @Value, Lombok @Data, and constructors.

The article provides an example PMD <?xml version="1.0"?> <ruleset name="myruleset" 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 https://pmd.sourceforge.io/ruleset_2_0_0.xsd"> <description>My ruleset</description> <!-- Empty catch block --> <rule ref="category/java/errorprone.xml/EmptyCatchBlock"/> <!-- Empty finally block --> <rule ref="category/java/errorprone.xml/EmptyFinallyBlock"/> <!-- Empty if statement --> <rule ref="category/java/errorprone.xml/EmptyIfStmt"/> <!-- Unused local variable --> <rule ref="category/java/bestpractices.xml/UnusedLocalVariable"/> <!-- Unused private field --> <rule ref="category/java/bestpractices.xml/UnusedPrivateField"/> <!-- Unused private method --> <rule ref="category/java/bestpractices.xml/UnusedPrivateMethod"/> <!-- Avoid importing java.lang --> <rule ref="category/java/codestyle.xml/DontImportJavaLang"/> <!-- Duplicate imports --> <rule ref="category/java/codestyle.xml/DuplicateImports"/> <!-- Unused imports --> <rule ref="category/java/bestpractices.xml/UnusedImports"/> </ruleset> showing how to configure checks for empty blocks, unused variables, and import issues.

For dynamic analysis, the article describes setting up a JaCoCo pipeline on JD’s CI system: downloading the JaCoCo agent, adding it to JVM options, running the pipeline, and reviewing coverage reports to identify code that is never executed and can be safely removed.

Conclusions recommend combining PMD + CPD for static inspection and JaCoCo for dynamic coverage, noting that while static tools can automatically flag many dead code fragments, final decisions still require manual review, especially for public APIs and complex logic.

Javacode qualityStatic AnalysisJaCoCoDynamic AnalysisPMD
JD Retail Technology
Written by

JD Retail Technology

Official platform of JD Retail Technology, delivering insightful R&D news and a deep look into the lives and work of technologists.

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.