GeezerVison is showing github.com — [java] Fix #559: Improve UseUtilityClassRule to trigger also on static members by UncleOwen · Pull Request #6638 · pmd/pmd
Conversation
UncleOwen
commented
Apr 30, 2026
•
edited by adangel
Loading
Uh oh!
There was an error while loading. Please reload this page.
Long title: [java] Fix #559: Improve UseUtilityClassRule to trigger also on classes that have only static member variables, only static nested classes, or any mix of static member variables, static methods and static nested classes.
Describe the PR
While the issue asked for a new rule, I think this fits very well into the existing rule. Also did some minor refactoring to the existing code.
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)
- [n/a] Added (in-code) documentation (if needed)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
… static member variables or a mix of static member variables and static methods.
|
Documentation Preview
Compared to main:
This changeset changes 329 violations,
introduces 186 new violations, 0 new errors and 0 new configuration errors,
removes 18 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-05-14 14:55:43+00:00 for c49b909)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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 for the PR!
I generally agree with you, that it makes sense to improve the rule UseUtilityClass.
However, we should then also do the following:
- Update title and description of #559 to make this: "UseUtilityClass: False negative for constant only classes"
- Update the rule documentation to reflect this change (it only talks about static methods currently...)
- Consider providing different rule violation messages, depending on the case. Currently, we issue "All methods are static. Consider adding a private no-args constructor to prevent instantiation." for the new violation, which not always makes sense. For the new case, better would be something like "All fields are static. Consider ...". Maybe we could even add the class name in the message.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
# Conflicts:
# pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/UseUtilityClass.xml
Extract method hasWrongKindOfConstructor
Extract allNonPrivateMembersAreStatic
UncleOwen
commented
May 9, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks for the review!
I changed the rule documentation, examples, and error message to talk about "members" instead of "methods". I remembered that nested classes are a thing, and added support for them, even if they are rare. But since a class with any combination of static variables/static methods/static nested classes can now trigger this, I opted to keep the error message the same in all cases.
Oh, and you'll have to do the changes to #559 yourself, I'm not allowed to do that.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
UncleOwen
changed the title
[java] Fix #559: Improve UseUtilityClassRule to trigger also on classes that have only static member variables or a mix of static member variables and static methods.
[java] Fix #559: Improve UseUtilityClassRule to trigger also on classes that have only static member variables, only static nested classes, or any mix of static member variables, static methods and static nested classes.
May 9, 2026
adangel
changed the title
[java] Fix #559: Improve UseUtilityClassRule to trigger also on classes that have only static member variables, only static nested classes, or any mix of static member variables, static methods and static nested classes.
[java] Fix #559: Improve UseUtilityClassRule to trigger also on static members
May 14, 2026
Uh oh!
There was an error while loading. Please reload this page.
…/design/xml/UseUtilityClass.xml
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!
FYI: I've updated #559 to remove the label "new:rule" and add the label "a:false-negative". I've also added a note to the description of that issue. I hope, you have all the permissions to do that yourself in the future - if not, we need to check github roles/permissions.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
UncleOwen reacted with thumbs up emoji
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
adangel
adangel approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
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.
|