27 March 2022
The PMD team is pleased to announce PMD 6.44.0.
This is a minor release.
This release of PMD brings support for Java 18. There are no new standard language features.
PMD also supports JEP 420: Pattern Matching for switch (Second Preview) as a
preview language feature. In order to analyze a project with PMD that uses these language features,
you’ll need to enable it via the environment variable PMD_JAVA_OPTS and select the new language
version 18-preview:
export PMD_JAVA_OPTS=--enable-preview
./run.sh pmd -language java -version 18-preview ...
Note: Support for Java 16 preview language features have been removed. The version “16-preview” is no longer available.
The new rule class DomXPathRule is intended to replace
usage of the XPathRule for XML rules. This rule executes the XPath query in a different
way, which sticks to the XPath specification. This means the expression is interpreted
the same way in PMD as in all other XPath development tools that stick to the standard.
You can for instance test the expression in an online XPath editor.
Prefer using this class to define XPath rules: replace the value of the class
attribute with net.sourceforge.pmd.lang.xml.rule.DomXPathRule like so:
<rule name="MyXPathRule"
language="xml"
message="A message"
class="net.sourceforge.pmd.lang.xml.rule.DomXPathRule">
<properties>
<property name="xpath">
<value><![CDATA[
/a/b/c[@attr = "5"]
]]></value>
</property>
<!-- Note: the property "version" is ignored, remove it. The query is XPath 2. -->
</properties>
</rule>
The rule is more powerful than XPathRule, as it can now handle XML namespaces,
comments and processing instructions. Please refer to the Javadoc of DomXPathRule
for information about the differences with XPathRule and examples.
XPathRule is still perfectly supported for all other languages, including Apex and Java.
The new XPath functions pmd:startLine, pmd:endLine, pmd:startColumn,
and pmd:endColumn are now available in XPath rules for all languages. They
replace the node attributes @BeginLine, @EndLine and such. These attributes
will be deprecated in a future release.
Please refer to the documentation of these functions for more information, including usage samples.
Note that the function pmd:endColumn returns an exclusive index, while the
attribute @EndColumn is inclusive. This is for forward compatibility with PMD 7,
which uses exclusive end indices.
This release introduces a new programmatic API to replace the inflexible PMD class.
Programmatic execution of PMD should now be done with a PMDConfiguration
and a PmdAnalysis, for instance:
PMDConfiguration config = new PMDConfiguration();
config.setDefaultLanguageVersion(LanguageRegistry.findLanguageByTerseName("java").getVersion("11"));
config.setInputPaths("src/main/java");
config.prependAuxClasspath("target/classes");
config.setMinimumPriority(RulePriority.HIGH);
config.addRuleSet("rulesets/java/quickstart.xml");
config.setReportFormat("xml");
config.setReportFile("target/pmd-report.xml");
try (PmdAnalysis pmd = PmdAnalysis.create(config)) {
// note: don't use `config` once a PmdAnalysis has been created.
// optional: add more rulesets
pmd.addRuleSet(pmd.newRuleSetLoader().loadFromResource("custom-ruleset.xml"));
// optional: add more files
pmd.files().addFile(Paths.get("src", "main", "more-java", "ExtraSource.java"));
// optional: add more renderers
pmd.addRenderer(renderer);
// or just call PMD
pmd.performAnalysis();
}
The PMD class still supports methods related to CLI execution: runPmd and main.
All other members are now deprecated for removal.
The CLI itself remains compatible, if you run PMD via command-line, no action is required on your part.
PMD have been newly deprecated, including:
PMD#EOL: use System#lineSeparator()PMD#SUPPRESS_MARKER: use DEFAULT_SUPPRESS_MARKERPMD#processFiles: use the new programmatic APIPMD#getApplicableFiles: is internalPMDConfiguration#prependClasspath is deprecated
in favour of prependAuxClasspath.PMDConfiguration#setRuleSets and
getRuleSets are deprecated. Use instead
setRuleSets,
addRuleSet,
and getRuleSetPaths.BaseCLITest have been deprecated with replacements.Several members of PMDCommandLineInterface have been explicitly deprecated.
The whole class however was deprecated long ago already with 6.30.0. It is internal API and should
not be used.
AmbiguousResolutionRule
and ConnectUsingNonConnector have been deprecated,
since they didn’t comply to the usual rule class naming conventions yet.
The replacements are in the subpackage bestpractices.Together with the new programmatic API the interface
TextFile has been added as experimental. It intends
to replace DataSource and SourceCode in the long term.
This interface will change in PMD 7 to support read/write operations
and other things. You don’t need to use it in PMD 6, as FileCollector
decouples you from this. A file collector is available through PmdAnalysis#files.