Skip to content

[java] New rule: ProtectedMemberInFinalClass - #6964

Merged
UncleOwen merged 4 commits into
pmd:mainfrom
zbynek:protected-members
Aug 19, 2026
Merged

UncleOwen merged 4 commits into
pmd:mainfrom
zbynek:protected-members

Conversation

@zbynek

@zbynek zbynek commented Aug 9, 2026

Copy link
Copy Markdown
Contributor

Describe the PR

Two existing rules for protected members in final classes are merged to one. The new rule also covers nested type declarations where the outer one is final and the inner one is protected.

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)

@zbynek
zbynek force-pushed the protected-members branch 2 times, most recently from 970bebd to 9fcc56c Compare August 9, 2026 23:29
@pmd-actions-helper

pmd-actions-helper Bot commented Aug 9, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 138 new violations, 0 new errors and 0 new configuration errors,
removes 113 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-08-19 19:21:06+00:00 for bf7ce80)

@oowekyala

Copy link
Copy Markdown
Member

I wonder if this is not already subsumed by unnecessarymodifier?

@zbynek

zbynek commented Aug 10, 2026

Copy link
Copy Markdown
Contributor Author

@oowekyala currently not; should we merge it into that one?

The semantic is slightly different -- UnnecessaryModifier finds fields where removing the modifier has no effect at all, in case of this rule there might be small consequences (e.g. code iterating protected method through reflection?), somewhat similar to https://pmd.github.io/pmd/pmd_rules_java_design.html#publicmemberinnonpublictype .

@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.

Please also add something to the "new rules" section of the release notes.

@zbynek
zbynek force-pushed the protected-members branch from 4aaecaa to 62119c1 Compare August 18, 2026 11:01
@UncleOwen UncleOwen added this to the 7.27.0 milestone Aug 19, 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.

And I think this is sufficiently different from UnnecessaryModifier to keep as its own rule.

@UncleOwen
UncleOwen merged commit e74d847 into pmd:main Aug 19, 2026
13 checks passed
@adangel adangel added a:new-rule Proposal to add a new built-in rule is:deprecation The main focus is deprecating public APIs or rules, eg to make them internal, or removing them labels Aug 20, 2026
@adangel

adangel commented Aug 20, 2026

Copy link
Copy Markdown
Member

@zbynek @UncleOwen Should we move this new rule into the design category? The original issue suggested that.

@UncleOwen

Copy link
Copy Markdown
Member

Now that you mention it... yes. This isn't a style issue.

Do not use protected fields in final classes since they cannot be subclassed.
Clarify your intent by using private or package access modifiers instead.

**Deprecated:** This rule is deprecated since PMD 7.27.0 and will be removed with PMD 8.0.0. This rule has been replaced by {% rule ProtectedMemberInFinalField %}, which covers all class members.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a:new-rule Proposal to add a new built-in rule is:deprecation The main focus is deprecating public APIs or rules, eg to make them internal, or removing them

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[java] Merge rules about protected in final class (AvoidProtectedFieldInFinalClass, AvoidProtectedMethodInFinalClassNotExtending)

5 participants