Skip to content

[java] Fix #6450: DanglingJavadoc should ignore markdown javadoc (///) before Java 23 - #6951

Merged
UncleOwen merged 4 commits into
pmd:mainfrom
fudianchn:fix/danglingjavadoc-markdown-java-version-6450
Sep 10, 2026
Merged

UncleOwen merged 4 commits into
pmd:mainfrom
fudianchn:fix/danglingjavadoc-markdown-java-version-6450

Conversation

@fudianchn

@fudianchn fudianchn commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

AI disclosure: this change was prepared with AI coding agents, reviewed and revised line by line by me.

What

/// comments are now only treated as markdown javadoc (JEP 467) when the language version is >= Java 23. On older versions they stay ordinary single line comments, which fixes the DanglingJavadoc false positive from #6450.

Why

Since Java 23, /// starts a markdown javadoc comment (JEP 467). Before that, it is an ordinary comment, so flagging it as dangling javadoc is a false positive. This supersedes the "keep it simple for now" decision from #5916 (discussion in #6450), per @adangel's guidance: "it would be more consistent, if the rule only sees the JavadocComments, that are actual Javadoc comments".

Root cause

ASTCompilationUnit#setComments collapsed consecutive /// comments into JavadocComments on all language versions, so the comment model itself classified /// as javadoc even for Java < 23.

How

The parser now passes jdkVersion >= 23 (new grammar helper isMarkdownJavadocSupported) into ASTCompilationUnit#setComments, which collapses markdown comments only when markdown javadoc is supported. DanglingJavadocRule is unchanged: it now only sees actual JavadocComments. Other comment consumers (CommentRequired via getJavadocComment(), UnnecessaryImport, the comment-on XPath function, CommentSize) get the same consistent model.

Testing

  • New cases in DanglingJavadoc.xml: //// under java 21 -> 0 violations (was 1); under java 23 -> 1 violation (unchanged).
  • New model-level test JavaCommentTest#markdownCommentsAreJavadocOnlySinceJava23: at java 21 two /// lines stay two ordinary comments and getJavadocComment() is null; at java 23 they collapse into one JavadocComment owned by the class.
  • ./mvnw -pl pmd-java -am test -Dtest='DanglingJavadocTest,JavaCommentTest,Java23TreeDumpTest' -> 15 passed; full net.sourceforge.pmd.lang.java.ast.** suite -> 2880 passed.

Fixes #6450

@zbynek
zbynek requested a review from adangel August 7, 2026 15:48
Comment thread docs/pages/release_notes.md Resolved
@zbynek

zbynek commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

whether this belongs to this rule specifically or whether /// handling should change in general.

@adangel thoughts?

fudianchn added a commit to fudianchn/pmd that referenced this pull request Aug 7, 2026
Apply @zbynek's review suggestions on pmd#6951: describe the canonical
markdown javadoc marker as `///` rather than `////` in the release
note and the two new test-case descriptions. The test sources keep `////`
to exercise that an extra leading slash still counts as markdown javadoc.

Signed-off-by: 付典 <[email protected]>
@zbynek zbynek changed the title [java] Fix #6450: DanglingJavadoc ignores markdown javadoc (////) before Java 23 [java] Fix #6450: DanglingJavadoc ignores markdown javadoc (///) before Java 23 Aug 7, 2026
@fudianchn
fudianchn force-pushed the fix/danglingjavadoc-markdown-java-version-6450 branch from ef8099a to b06b539 Compare August 8, 2026 03:24
@pmd-actions-helper

pmd-actions-helper Bot commented Aug 8, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 0 errors and 0 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-09-10 11:35:45+00:00 for b0ce7c2)

@fudianchn
fudianchn marked this pull request as draft August 12, 2026 00:37
@fudianchn
fudianchn marked this pull request as ready for review August 12, 2026 16:33
@adangel adangel changed the title [java] Fix #6450: DanglingJavadoc ignores markdown javadoc (///) before Java 23 [java] Fix #6450: DanglingJavadoc should ignore markdown javadoc (///) before Java 23 Aug 14, 2026

@adangel adangel left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whether this belongs to this rule specifically or whether /// handling should change in general.

@adangel thoughts?

Indeed, it would be more consistent, if the rule only sees the JavadocComments, that are actual Javadoc comments.

@fudianchn Can you give it a try? The implementation is in ASTCompilationUnit#setComments. There you also can check the language version.

Comment thread docs/pages/release_notes.md Resolved
@fudianchn
fudianchn marked this pull request as draft August 15, 2026 01:22
…e Java 23

Markdown documentation comments (JEP 467) are available since Java 23.
ASTCompilationUnit#setComments now collapses consecutive /// comments
into JavadocComments only when the language version is >= 23, so on
older versions they stay ordinary single line comments and are no
longer reported by DanglingJavadoc. Other comment consumers see the
same consistent model.

This supersedes the "keep it simple for now" decision from pmd#5916, see
the discussion in pmd#6450 and pmd#6951.

Signed-off-by: 付典 <[email protected]>
@fudianchn
fudianchn force-pushed the fix/danglingjavadoc-markdown-java-version-6450 branch from b06b539 to 5927caf Compare August 15, 2026 01:34
@fudianchn
fudianchn marked this pull request as ready for review August 15, 2026 01:34
@UncleOwen UncleOwen added this to the 7.28.0 milestone Sep 10, 2026

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@UncleOwen
UncleOwen merged commit 73aa712 into pmd:main Sep 10, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] DanglingJavadoc: False positive on /// comments for Java < 23

4 participants