GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [java] Fix #6270: CommentSize skips file header comments by fudianchn · Pull Request #6948 · pmd/pmd

Conversation

@fudianchn

@fudianchn fudianchn commented Aug 6, 2026

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@fudianchn
[java] CommentSize: skip file header comments (pmd#6270)
b3ce846
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 approved these changes Aug 7, 2026
View reviewed changes

@zbynek zbynek left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@pmd-actions-helper

pmd-actions-helper Bot commented Aug 7, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

UncleOwen added 2 commits August 7, 2026 19:46
@UncleOwen
Merge branch 'main' into fork/fudianchn/fix/commentsize-header-6270
c607952
@UncleOwen
keep release notes ordered alphabetically
4409b44
@UncleOwen

UncleOwen commented Aug 7, 2026

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.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 7, 2026
UncleOwen
UncleOwen approved these changes Aug 7, 2026
View reviewed changes

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@UncleOwen
UncleOwen merged commit 71f85f0 into pmd:main Aug 7, 2026
13 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@fudianchn fudianchn mentioned this pull request Aug 8, 2026
[java] Why CommentSize? #2265
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@zbynek zbynek zbynek approved these changes
@UncleOwen UncleOwen UncleOwen approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

7.27.0

Development

Successfully merging this pull request may close these issues.

[java] CommentSize: False positive with file header comments

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@fudianchn @UncleOwen @zbynek
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.