Repository navigation
[java] Fix #6270: CommentSize skips file header comments - #6948
Merged
Merged
Conversation
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]>
Contributor
|
Compared to main: (comment created at 2026-08-07 18:05:22+00:00 for 4409b44) |
Member
|
The regression tester report is interesting:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
CommentSizenow 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.visitlooped overcUnit.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
#6270 File header comments are not checked: an 8-line header beforepackage(defaultmaxLines=6), expected 0 problems.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.package foo;so the comment remains a non-header, with expected line numbers shifted accordingly.Notes
Fixes #6270