Skip to content

[java] Fix #6270: CommentSize skips file header comments - #6948

Merged
UncleOwen merged 3 commits into
pmd:mainfrom
fudianchn:fix/commentsize-header-6270
Aug 7, 2026
Merged

UncleOwen merged 3 commits into
pmd:mainfrom
fudianchn:fix/commentsize-header-6270

Conversation

@fudianchn

Copy link
Copy Markdown
Contributor

What

CommentSize now skips file header comments — comments that appear before the first declaration (package / import / type / module) of the compilation unit — so that license/copyright headers are no longer flagged.

Why

The rule documentation states it checks "non-header comments", but the implementation iterated over every comment, producing false positives for typical license headers (e.g. ASLv2, which exceeds the default 6-line limit). Closes #6270.

Root cause

CommentSizeRule.visit looped over cUnit.getComments() without distinguishing header comments, contradicting its own documented scope.

How

Determine the start position of the first code token (the first package/import/type/module declaration) and skip any comment whose end position is at or before it. This uses the standard Java-ecosystem meaning of "file header" (the comments preceding the first declaration) and matches the issue's ASLv2-before-package example; it also holds for default-package files (no package — the header sits before the first type). Empty files keep the old behavior (nothing skipped).

Testing

  • New case #6270 File header comments are not checked: an 8-line header before package (default maxLines=6), expected 0 problems.
  • Verified it fails on the old code (CommentSize: Too many lines) and passes after the fix.
  • ./mvnw -pl pmd-java -am test -Dtest=CommentSizeTest -Dsurefire.failIfNoSpecifiedTests=false -DfailIfNoTests=false → Tests run: 5, Failures: 0, Errors: 0.
  • Two existing cases whose sample comment sat at the top of a default-package file (now correctly a header) were adjusted to add package foo; so the comment remains a non-header, with expected line numbers shifted accordingly.
  • Also fixed the bad documentation example flagged in the issue (the old example was a 1-non-empty-line header that would never trigger the rule).

Notes

  • Behavior change: comments before the first declaration are no longer checked — this is exactly the correction requested, and it is called out in the release note.

Fixes #6270

The rule documents that "non-header comments" are checked, but the
implementation checked every comment, producing false positives for
typical multi-line license/copyright headers (e.g. ASLv2).

A comment is now considered part of the file header when it ends before
the first code token of the compilation unit (the package/import/type/
module declaration) and is then skipped. The documentation example is
also corrected: the previous one had a single non-empty line only (so
the line count would not flag it) and would be a header comment.

Fixes pmd#6270

Signed-off-by: 付典 <[email protected]>

@zbynek zbynek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good.

There's some discussion about the usefulness of the rule in general: #2265 , but this improvement can be implemented regardless.

@pmd-actions-helper

pmd-actions-helper Bot commented Aug 7, 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 11592 violations, 16 errors and 9 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-08-07 18:05:22+00:00 for 4409b44)

@UncleOwen

Copy link
Copy Markdown
Member

The regression tester report is interesting:

  • For openjdk-11 and spring-boot a lot of violation are removed. As far as I can tell, all license headers, as expected.
  • For checkstyle, also a lot of violation removed. But those aren't license headers, they seem to be config for some kind of auto-formatter? Or for checkstyle itself? In any case, seems like a good idea not to report those as well.

@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 7, 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 71f85f0 into pmd:main Aug 7, 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] CommentSize: False positive with file header comments

3 participants