Skip to content

Issue #17841: Add GoogleMethodNameCheck to fix method name false nega…#18312

Open
vivek-0509 wants to merge 2 commits intocheckstyle:masterfrom
vivek-0509:addingNewGoogleMethodNameCheck
Open

Issue #17841: Add GoogleMethodNameCheck to fix method name false nega…#18312
vivek-0509 wants to merge 2 commits intocheckstyle:masterfrom
vivek-0509:addingNewGoogleMethodNameCheck

Conversation

@vivek-0509
Copy link
Contributor

@vivek-0509 vivek-0509 commented Dec 15, 2025

part of Issue: #17841
Issue: #18420

Description

Introduces a new GoogleMethodName check that properly validates method names according to the Google Java Style Guide, fixing false-negatives where underscores between letters and digits were not being flagged.

Problem

The previous MethodName check failed to detect invalid names like:

  • gradle_9_5_1() - underscore between letter 'e' and digit '9'
  • jdk_9_0_392() - underscore between letter 'k' and digit '9'

Solution

Created a dedicated GoogleMethodName check that:

  • Has no configurable properties
  • Uses multi-pattern validation instead of one complex regex
  • Handles @Test methods separately (underscores allowed)
  • Properly validates numbering suffixes (guava33_4_5 is valid)

Validation Rules

Regular Methods:

  • Must be in lowerCamelCase
  • Underscores only between adjacent digits (_4_5)
  • Not between letter and digit (gradle_9 invalid)

Test Methods (@Test, @ParameterizedTest, @RepeatedTest):

  • Underscores allowed to separate components
  • Each segment must be lowerCamelCase
  • Each segment must be at least 2 characters
  • Segments cannot start with single lowercase followed by uppercase (e.g., fO)

Override Methods:

  • Methods with @Override annotation are skipped (no validation)

Breaking Changes

None. This check is backward compatible with the previous MethodName regex behavior.

Diff Regression config: https://gist.githubusercontent.com/vivek-0509/3c0bc74332fbdb3a6ce4aef1c43d2f6b/raw/a7d36ba756226bac0e01e9148dc8bd7f3016ec96/base_config.xml

Diff Regression patch config: https://gist.githubusercontent.com/vivek-0509/a2777cd3e2a86dc84c549268d6651aa0/raw/6490530b3efb4213b7a2421dace6f157c7b264a2/patch_config.xml

Contirbution repo PR: checkstyle/contribution#1000

@vivek-0509
Copy link
Contributor Author

The CI Failures are unrelated

@vivek-0509 vivek-0509 marked this pull request as ready for review December 16, 2025 12:00
@vivek-0509
Copy link
Contributor Author

vivek-0509 commented Dec 16, 2025

Github, generate report for GoogleMethodName/all-examples-in-one

@github-actions
Copy link
Contributor

Failed to download or process the specified configuration(s).
Error details:

Please ensure you've used one of the following commands correctly:

@vivek-0509
Copy link
Contributor Author

GitHub, generate report

2 similar comments
@vivek-0509
Copy link
Contributor Author

GitHub, generate report

@vivek-0509
Copy link
Contributor Author

GitHub, generate report

@github-actions
Copy link
Contributor

Report generation failed on phase "make_report",
step "Generate report".
Link: https://github.com/checkstyle/checkstyle/actions/runs/20292237473

@vivek-0509
Copy link
Contributor Author

GitHub, generate report

@github-actions
Copy link
Contributor

@vivek-0509 vivek-0509 marked this pull request as draft December 17, 2025 06:32
@vivek-0509
Copy link
Contributor Author

vivek-0509 commented Dec 17, 2025

Hey @romani,
Here is the analysis of the report

Bug Found (Will Fix)

Issue: Test methods with numbering suffix like test3_1 are incorrectly rejected.

Cause: The implementation doesn't strip numbering suffix before validating test methods (unlike regular methods).

Examples:

  • test3_1Rejected (Should be valid - 3_1 = adjacent numbers)
  • testFoo_1_2Rejected (correctly invalid)

Fix: Will add numbering suffix stripping + INVALID_UNDERSCORE_PATTERN check for test methods.


Design Questions - Need Your Input

1. Minimum 2-character segment requirement for test methods

Currently rejects single lowercase character segments:

Examples:

  • t_nameRejected (segment t = 1 char)
  • test_aRejected (segment a = 1 char)
  • test_fooValid (both segments ≥ 2 chars)

Real example:

@Test
void can_be_used_in_a_collector() {} //  Rejected (segment a fails)

Question: Should we keep the current 2-character minimum requirement, or should we make it stricter by requiring proper lowerCamelCase (at least 2 words joined with capitalization) for each segment in test method names?

2. Test methods without @test annotation

Examples:

  • void test_ctor_shouldThrow_whenPidIsMissing()REJECTED (no annotation)
  • @Test void test_ctor_shouldThrow_whenPidIsMissing()VALID

Supported annotations: @Test, @ParameterizedTest, @RepeatedTest

Question: Should test-style method names without annotations be rejected, as Google Style only mentions JUnit tests?

@vivek-0509 vivek-0509 marked this pull request as ready for review December 17, 2025 08:44
@vivek-0509
Copy link
Contributor Author

@romani ping

@romani romani requested a review from Zopsss December 23, 2025 16:11
@romani
Copy link
Member

romani commented Dec 23, 2025

@Zopsss , can you help me to review this PR?
while I am busy with general stream of PRs.

@vivek-0509
Copy link
Contributor Author

vivek-0509 commented Dec 23, 2025

Will fix the CI asap , failures caused due to recent pr merge method access level for getPackageLocation() changed to pubilc

@vivek-0509
Copy link
Contributor Author

Hi @Zopsss ,

Before you review the code, I'd like to clarify my understanding of the expected rules for GoogleMethodNameCheck , as Google Style Guide is somewhat ambiguous in places.

For regular methods (without @Test), my implementation requires lowerCamelCase names where underscores are only allowed between adjacent numbers (like guava33_4_5). Names like gradle_9_5_1 with letter_digit underscores are flagged as invalid. Single letter names like f and patterns like fO are also rejected.

For test methods (with @Test, @ParameterizedTest, @RepeatedTest), underscores are allowed to separate logical components, with each component being lowerCamelCase. So transferMoney_deductsFromSource is valid, but test_1 is invalid because 1 is not lowerCamelCase and also it violates the no underscore between just number and digit. The numbering suffix rule also applies, so test3_1 is valid (adjacent numbers), but testFoo_1_2 is invalid (letter_digit pattern). For test methods like solve6x6_returnsTrue and solve6x6_noSolution_returnsFalse, my implementation treats them as valid since each component (solve6x6, returnsTrue, noSolution, returnsFalse) is lowerCamelCase and the underscore separates these components rather than being between a digit and letter within a component.

One thing I'm unclear about: Google Style says test components should be "lowerCamelCase". Strictly speaking, lowerCamelCase means multiple words joined with capitalization (like transferMoney). Should single-word components like test_foo be valid, or should we require multi-word lowerCamelCase like transferMoney_deductsFromSource?

If any of my expectations don't align with what's actually required, please let me know and I'll adjust my implementation before you do a full code review. This should save time .

@@ -1,4 +1,5 @@
abbreviation.as.word=Abbreviation in name ''{0}'' must contain no more than ''{1}'' consecutive capital letters.
google.method.name=Method name ''{0}'' is not valid per Google Java Style Guide.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this message does not explain why the method name is invalid. Instead we should keep different messages for the different conditions we hit to easily explain reason behind violation to the user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please create separate messages for all the logs which should explain the reason behind the violation.

Copy link
Contributor Author

@vivek-0509 vivek-0509 Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the separate violation messages, I'm planning to add 4 messages (one per log point):

google.method.name.underscore.regular=Method name ''{0}'' has invalid underscore usage, underscores only allowed between adjacent digits.

google.method.name.underscore.test=Method name ''{0}'' has invalid underscore usage, underscore only allowed between letters or between digits.

google.method.name.format.regular=Method name ''{0}'' must be lowerCamelCase: start with lowercase, at least 2 characters, and avoid single lowercase followed by uppercase.

google.method.name.format.test=Method name ''{0}'' is not valid: each segment must be lowerCamelCase: start lowercase, min 2 chars, avoid single lowercase followed by uppercase.

Does this approach work for you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

google.method.name.underscore.test=Method name ''{0}'' has invalid underscore usage, underscore only allowed between letters or between digits.

this one is for the test methods, so it should mention that it is for test methods.

google.method.name.format.regular=Method name ''{0}'' must be lowerCamelCase: start with lowercase, at least 2 characters, and avoid single lowercase followed by uppercase.

google.method.name.format.test=Method name ''{0}'' is not valid: each segment must be lowerCamelCase: start lowercase, min 2 chars, avoid single lowercase followed by uppercase.

it should end with: avoid single lowercase followed by uppercase or underscore.

Copy link
Contributor Author

@vivek-0509 vivek-0509 Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should end with: avoid single lowercase followed by uppercase or underscore.

but this still falls under invalid underscore usage, for example, f_ is not permitted because valid underscore usage is restricted to letter–letter or digit–digit only.
and for this case test_a message already mentions the min 2-chars required for each segment.

@vivek-0509
Copy link
Contributor Author

vivek-0509 commented Dec 25, 2025

Hi @Zopsss, thanks for the review
I’ve created an issue to clarify the expected scope and rules for this check: #18420

Would it be acceptable if I apply all the requested changes once we align on what exactly this check should cover and the set of rules to be enforced for this check?

@romani
Copy link
Member

romani commented Dec 26, 2025

GitHub, generate report

@github-actions
Copy link
Contributor

@romani
Copy link
Member

romani commented Dec 27, 2025

GitHub, generate report

@github-actions
Copy link
Contributor

@vivek-0509
Copy link
Contributor Author

@Zopsss ping

@Zopsss
Copy link
Member

Zopsss commented Jan 28, 2026

GitHub, generate report

@Zopsss
Copy link
Member

Zopsss commented Jan 28, 2026

GitHub, generate site

@Zopsss
Copy link
Member

Zopsss commented Jan 28, 2026

@vivek-0509 sorry for the delay, please resolve conflicts

@vivek-0509 vivek-0509 force-pushed the addingNewGoogleMethodNameCheck branch from 7bb0b01 to b025006 Compare January 28, 2026 05:35
@vivek-0509
Copy link
Contributor Author

@Zopsss Done, Could you please take a look at this comment and review the PR when you get a chance, Thanks

@vivek-0509
Copy link
Contributor Author

GitHub, generate site

@vivek-0509
Copy link
Contributor Author

GitHub, generate report

@vivek-0509
Copy link
Contributor Author

I have generated the Diff report in comparison to the Method Name Check which was used earlier in google checks to clearly see new and removed violation .

@github-actions
Copy link
Contributor

@vivek-0509
Copy link
Contributor Author

@Zopsss ping

Copy link
Member

@Zopsss Zopsss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay, the diff report and documentation looks good! Thanks a lot for all the hard work!!!!

@vivek-0509 vivek-0509 force-pushed the addingNewGoogleMethodNameCheck branch from b025006 to 19d0d1b Compare February 2, 2026 10:11
@vivek-0509
Copy link
Contributor Author

@romani Updated the since to 13.2.0, and rebased the branch, PR is ready to merge, waiting for your approval.

@vivek-0509 vivek-0509 force-pushed the addingNewGoogleMethodNameCheck branch from 19d0d1b to 6be181b Compare February 4, 2026 01:03
@vivek-0509
Copy link
Contributor Author

@romani Rebased the branch because recently a new check is merged, so need to update the XmlMetaReaderTest

@romani
Copy link
Member

romani commented Feb 4, 2026

With such massive diff report, this update will be a bomb.
And we can can not reliably verify that there is no regression, false positives.

If will be awesome to find some project that uses Google style, and send them updates to test their reaction, before we pour this on heads of whole world.

Is there any ideas?

Maybe comunda?

This is also interesting case https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/b025006_2026062507/reports/diff/protonpack/index.html#A1
aggregate_on_bi_element_predicate Google style allow phrases to be combined, not a simple words. No capital letters hints us that there is single word.

Are we going to use this wording
https://google.github.io/styleguide/javaguide.html#s5.2.2-class-names

Underscores may appear in JUnit test method names to separate logical components of the name, with each component written in lowerCamelCase, for example transferMoney_deductsFromSource. There is no One Correct Way to name test methods.

@romani
Copy link
Member

romani commented Feb 4, 2026

After thinking more on aggregate_on_bi_element_predicate , we should not allow this in first version of this Check. It is clear snake case naming.
Please add it to examples, even it test, no excuses.
We can change our mind based on user feedback.

@vivek-0509
Copy link
Contributor Author

vivek-0509 commented Feb 4, 2026

Are we going to use this wording
https://google.github.io/styleguide/javaguide.html#s5.2.2-class-names

@romani yes i was also worried about this type of cases while implementing this check due to the wording
I will update the test method regex to enforce that each segment must contain at least one uppercase letter (lowerCamelCase), which rejects snake_case patterns

Valid: testFoo, testCheck_loginFails, login (single segment without underscore)
Invalid: test_login, aggregate_on_bi_element_predicate

Regarding testing with Camunda, I checked their configuration at camunda/.checkstyle.xml and they use the default MethodNameCheck with the default regex, not google_checks.xml. Since they're not following Google style conventions for method naming, i think they would not be a good candidate to test this change

i found this project google-cloudevents-java that uses checkstyle google_checks.xml but it seems to be inactive
https://github.com/googleapis/google-cloudevents-java/blob/f23e048750ad5666a71b786da8f01952c9037de1/pom.xml#L92-L105

Do you know about any other project that enforces google_checks.xml ???

}

@Override
public void visitToken(DetailAST ast) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reply for comment #18804 (comment).
Using NullUtil.notNull(ast.findFirstToken(TokenTypes.IDENT)) here looks totally correct to me. A small nit: since this method only accepts METHOD_DEF, naming the parameter methodAst would be a bit more explicit. That said, ast is also fine—this is clear from getRequiredTokens, and other checkers do the same. Renaming or not renaming are both OK.

@romani
Copy link
Member

romani commented Feb 5, 2026

Do you know about any other project that enforces google_checks.xml ?

No, users try to customize a bit config, and I understand it, demand for javadoc on all is too expensive.
Majority immediately remove all about javadoc. Javadoc is applicable only for libraries, so it is very low percentage from general java code base

@vivek-0509
Copy link
Contributor Author

@romani
i think we need to update the violation message also to not confuse users

Test method name ''{0}'' is not valid. Each segment must start lowercase, contain at least one uppercase letter for multi-segment names, min 2 chars, avoid single lowercase letter followed by uppercase, and contain only letters, digits, or underscores.

does this work ??

@romani
Copy link
Member

romani commented Feb 5, 2026

@lrozenblyum , I know you use Google style.
Can you do us favor to provide feedback on this Check implementation?
We can release it, without activation in Google style config and we can probably us you to test it of your codebase.
If you confirm that no bad side effects appears, we can update config and be more confident in success.

@vivek-0509 vivek-0509 force-pushed the addingNewGoogleMethodNameCheck branch from 6be181b to d9a2727 Compare February 6, 2026 14:50
@vivek-0509
Copy link
Contributor Author

GitHub, generate report

@vivek-0509
Copy link
Contributor Author

GitHub, generate website

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.

4 participants