Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static MethodCount #14695

Open
xenoterracide opened this issue Mar 20, 2024 · 1 comment
Open

Static MethodCount #14695

xenoterracide opened this issue Mar 20, 2024 · 1 comment

Comments

@xenoterracide
Copy link

My head hurts trying to describe this... I want a way to define how many static methods can exist on a utility class vs an instance class or other token as listed on https://checkstyle.org/checks/sizes/methodcount.html

Unfortunately MethodCount didn't choose differentiate between static and non static, or between utility classes and instance classes. This is hard to describe because I think What I want is a Token called UTIL_CLASS_DEF which probably doesn't work since it's not a real language construct (dear java).

Motivation for this is the number of static methods for a utility class is high, but for a regular class if you have more than a couple of public static (probably factory) methods, then your class is probably misdesigned.

Adding these properties and that token could work

name description type default value since
maxStaticPackage Specify the maximum number of package methods allowed. int 100 5.3
maxStaticPrivate Specify the maximum number of private methods allowed. int 100 5.3
maxStaticProtected Specify the maximum number of protected methods allowed. int 100 5.3
maxStaticPublic Specify the maximum number of public methods allowed. int 100 5.3
maxStaticTotal Specify the maximum number of public methods allowed. int 100 5.3
maxInstancePackage Specify the maximum number of package methods allowed. int 100 5.3
maxInstancePrivate Specify the maximum number of private methods allowed. int 100 5.3
maxInstanceProtected Specify the maximum number of protected methods allowed. int 100 5.3
maxInstancePublic Specify the maximum number of public methods allowed. int 100 5.3
maxInstanceTotal Specify the maximum number of public methods allowed. int 100 5.3
type utility or not selector list utility,instance

A new check to replace the existing check could also work and at least simplify naming. Maybe 2 checks would work as a utility class should have 0 public and protected instance methods, possibly 0 entirely. The problem is that no matter what way I think to solve this it's a cartesian product of what's already there, you could add more checks, explode the properties.

@nrmancuso
Copy link
Member

I think What I want is a Token called UTIL_CLASS_DEF which probably doesn't work since it's not a real language construct (dear java).

We will not add this token, we try to follow the JLS as closely as possible (without too many breaking changes for past weirdness).

This sounds like a good candidate for MatchXPathCheck. @xenoterracide have you considered using this check? A good start might be to create an expression like "if a given class name does not contain "Util" and this class has more than 5 static methods, place a violation on the class".

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

No branches or pull requests

2 participants