Skip to content

[java] CommentRequired: add packageMethodCommentRequirement property - #6880

Merged
UncleOwen merged 1 commit into
pmd:mainfrom
legacycode-forks:feature/comment-required-package-visibility
Jul 24, 2026
Merged

UncleOwen merged 1 commit into
pmd:mainfrom
legacycode-forks:feature/comment-required-package-visibility

Conversation

@legacynode

@legacynode legacynode commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Describe the PR

Rule: CommentRequired

This PR adds a new property packageMethodCommentRequirement to the CommentRequired rule. It controls whether Javadoc comments are required (or unwanted) for package-private methods and constructors.

Previously, method/constructor comment requirements could only be configured for public (publicMethodCommentRequirement) and protected (protectedMethodCommentRequirement) visibility. Package-private members were silently ignored, with no way to enforce documentation on them.

Implementation notes:

  • Uses ASTExecutableDeclaration.getVisibility() == V_PACKAGE, which correctly distinguishes genuinely package-private members from public/protected members that merely reside in a package-private class.
  • Reuses the existing getJavadocComment() mechanism, consistent with how the other visibility levels are handled.
  • The new property defaults to Ignored, so existing rule configurations are completely unaffected and no new violations appear for current users.

Note: this is distinct from the package-level Javadoc idea discussed in #1683 — that concerned package-info.java, whereas this targets package-private methods and constructors.

Related issues

No dedicated issue yet; happy to open one or start a discussion if preferred.

Ready?

  • Added unit tests for fixed bug/feature
  • Passing all unit tests
  • Complete build ./mvnw clean verify passes
  • Added (in-code) documentation (if needed)

Add a new property 'packageMethodCommentRequirement' to the CommentRequired
rule that controls whether Javadoc comments are required (or unwanted) for
package-private methods and constructors. Previously only public and protected
methods could be configured via publicMethodCommentRequirement and
protectedMethodCommentRequirement.

The new property defaults to Ignored to keep existing configurations backwards
compatible. It uses ASTExecutableDeclaration.getVisibility() == V_PACKAGE, which
correctly detects package-private visibility and reliably resolves the attached
Javadoc via node.getJavadocComment().
@legacynode
legacynode force-pushed the feature/comment-required-package-visibility branch from fc40396 to 8a54e3b Compare July 15, 2026 09:53
@pmd-actions-helper

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-07-15 15:50:04+00:00 for 8a54e3b)

@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 0be2359 into pmd:main Jul 24, 2026
13 checks passed
@UncleOwen UncleOwen added this to the 7.27.0 milestone Jul 25, 2026
@adangel adangel added the an:enhancement An improvement on existing features / rules label Aug 20, 2026
UncleOwen pushed a commit that referenced this pull request Aug 20, 2026
- add miscellaneous section
- add #2974, #6880, #6625
- consistent issue title for false positives/negatives
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

an:enhancement An improvement on existing features / rules

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants