Skip to content

[java] Fix #6460: Fix false negative for overridden methods in PublicMemberInNonPublicType - #6796

Merged
adangel merged 5 commits into
pmd:mainfrom
UncleOwen:issue-6460-PublicMemberInNonPublicType
Jun 23, 2026
Merged

adangel merged 5 commits into
pmd:mainfrom
UncleOwen:issue-6460-PublicMemberInNonPublicType

Conversation

@UncleOwen

Copy link
Copy Markdown
Member

Describe the PR

This PR...

  • rewrites the XPath rule PublicMemberInNonPublicType in Java
  • Changes how the rule treats methods overriding other methods
    • Old behavior: Overrides never trigger a violation
    • New behavior: Overrides only trigger a violation if the visibility can be reduced. If the overridden method is public, the overriding methods needs to be public too, so no violation is triggered.

Related issues

Ready?

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

Don't exclude all methods that override another method.
Just those, where visibility cannot be lowered,
because the method in the parent class is public.
@pmd-actions-helper

pmd-actions-helper Bot commented Jun 21, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 1182 violations,
introduces 262 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-06-22 16:30:22+00:00 for f631994)

@UncleOwen

Copy link
Copy Markdown
Member Author

regression tester report is larger than expected. I'll look into it.

@UncleOwen
UncleOwen marked this pull request as draft June 21, 2026 15:11
…ers.

Example:
class Wrong {
    public int field = 0;
               ^^^^^^^^^ old version would violate this part
               ^^^^^ new version only violates this part
}
@UncleOwen

UncleOwen commented Jun 22, 2026 •

Copy link
Copy Markdown
Member Author

Ok, I had a look.

All the 1182 changed violations seem to stem from the fact that the new implementation shortens the violation for fields with initializers. Example:

class Wrong {
    public int field = 0;
               ^^^^^^^^^ old version would violate this part
               ^^^^^ new version only violates this part
}

That wasn't intentional, but I'd argue it's a good change.

The new violations AFAICT are all as described in the PR description, so they are expected.

@UncleOwen
UncleOwen marked this pull request as ready for review June 22, 2026 17:04
@adangel adangel added this to the 7.26.0 milestone Jun 23, 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.

Thanks!

@adangel
adangel merged commit 68a7f83 into pmd:main Jun 23, 2026
13 checks passed
@UncleOwen
UncleOwen deleted the issue-6460-PublicMemberInNonPublicType branch June 23, 2026 11:56
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] PublicMemberInNonPublicType: False negative for overridden methods

2 participants