GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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

@UncleOwen UncleOwen commented Apr 30, 2026 •
edited by adangel
Loading

Uh oh!

There was an error while loading. Please reload this page.

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

  • Fix [java] UseUtilityClass: False negative for constant only classes #559

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.

All reactions
@UncleOwen
Improve UseUtilityClassRule to trigger also on classes that have only…
6329be0
… static member variables or a mix of static member variables and static methods.
@pmd-actions-helper

pmd-actions-helper Bot commented Apr 30, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

adangel
adangel requested changes May 8, 2026
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

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.

All reactions
Comment thread pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/UseUtilityClassRule.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@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
@UncleOwen
Merge branch 'main' into issue-559-constant-only-classes
c42fb5c
# Conflicts:
#	pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/UseUtilityClass.xml
@UncleOwen
Additional test for my sanity
fe19fc1
@UncleOwen
Consistent variable naming
74b8b9f
@UncleOwen
Update message, description, and example
7f06a87
@UncleOwen
Nested classes exist, and this rule should account for them
3efab61
@UncleOwen
refactor:
7ebebc8
Extract method hasWrongKindOfConstructor
@UncleOwen
refactor:
613ad93
Extract allNonPrivateMembersAreStatic
@UncleOwen

UncleOwen commented May 9, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@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
@UncleOwen
fix variable name
4446f86
@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 reviewed May 14, 2026
View reviewed changes
Comment thread pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/design/xml/UseUtilityClass.xml Resolved

Uh oh!

There was an error while loading. Please reload this page.

@adangel
Update pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule…
c49b909
…/design/xml/UseUtilityClass.xml
adangel
adangel approved these changes May 14, 2026
View reviewed changes

@adangel adangel left a comment

Copy link
Copy Markdown
Member

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
All reactions
  • 👍 1 reaction
@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
[doc] Update release notes (#6638, #559)
ba0d793
@adangel
adangel merged commit 8a8cc33 into pmd:main May 14, 2026
9 checks passed

Uh oh!

There was an error while loading. Please reload this page.

adangel added a commit that referenced this pull request May 14, 2026
@adangel
[java] Fix #559: Improve UseUtilityClassRule to trigger also on stati…
f85fb5a
…c members (#6638)
@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

Reviewers

@adangel adangel adangel approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

7.25.0

Development

Successfully merging this pull request may close these issues.

[java] UseUtilityClass: False negative for constant only classes

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@UncleOwen @adangel
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.