Skip to content

[java] Fix #559: Improve UseUtilityClassRule to trigger also on static members#6638

Merged
adangel merged 10 commits into
pmd:mainfrom
UncleOwen:issue-559-constant-only-classes
May 14, 2026
Merged

[java] Fix #559: Improve UseUtilityClassRule to trigger also on static members#6638
adangel merged 10 commits into
pmd:mainfrom
UncleOwen:issue-559-constant-only-classes

Conversation

@UncleOwen

@UncleOwen UncleOwen commented Apr 30, 2026

Copy link
Copy Markdown
Member

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)

… static member variables or a mix of static member variables and static methods.
@pmd-actions-helper

pmd-actions-helper Bot commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

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)

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

@adangel adangel added the needs:user-input Maintainers are waiting for feedback from author label May 8, 2026
@UncleOwen UncleOwen marked this pull request as draft May 9, 2026 12:09
UncleOwen added 7 commits May 9, 2026 14:13
# Conflicts:
#	pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/UseUtilityClass.xml
Extract method hasWrongKindOfConstructor
Extract allNonPrivateMembersAreStatic
@UncleOwen

UncleOwen commented May 9, 2026

Copy link
Copy Markdown
Member Author

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.

@UncleOwen UncleOwen marked this pull request as ready for review May 9, 2026 14:01
@UncleOwen 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 adangel removed the needs:user-input Maintainers are waiting for feedback from author label May 14, 2026
@adangel 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

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

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.

@adangel adangel added this to the 7.25.0 milestone May 14, 2026
adangel added a commit that referenced this pull request May 14, 2026
@adangel adangel merged commit 8a8cc33 into pmd:main May 14, 2026
9 checks passed
adangel added a commit that referenced this pull request May 14, 2026
@UncleOwen UncleOwen deleted the issue-559-constant-only-classes branch May 14, 2026 19:02
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] UseUtilityClass: False negative for constant only classes

2 participants