Skip to content

Issue #18494: Add GoogleNonConstantFieldNameCheck for Google Java Style compliance#18657

Open
vivek-0509 wants to merge 1 commit intocheckstyle:masterfrom
vivek-0509:addingNewGoogleMemberNameCheck
Open

Issue #18494: Add GoogleNonConstantFieldNameCheck for Google Java Style compliance#18657
vivek-0509 wants to merge 1 commit intocheckstyle:masterfrom
vivek-0509:addingNewGoogleMemberNameCheck

Conversation

@vivek-0509
Copy link
Contributor

@vivek-0509 vivek-0509 commented Jan 15, 2026

Adds GoogleNonConstantFieldNameCheck to enforce non-constant field naming per Google Java Style Guide §5.2.5.

Part of #17842
Detailed Issue at #18494

Rules Enforced

  • lowerCamelCase format required
  • Single-character names rejected (f)
  • Hungarian-style prefixes rejected (mValue, sCount)
  • Underscores only between adjacent digits (guava33_4_5 )
  • No leading/trailing/consecutive underscores

Scope

  • This check validates instance fields, while excluding constants (static final), static non-final fields and local variables.

Diff Regression config: https://gist.githubusercontent.com/vivek-0509/36cbe7f3789730951f15e7b6d551c6f3/raw/7ac2050528756f02e9952ae546a9c1e8cfed29c0/base_config.xml

Diff Regression patch config: https://gist.githubusercontent.com/vivek-0509/a979757b15933ce44f9b0f676a038195/raw/77433e7a8e91c9d621be184f4efcb829cb12d868/patch_config.xml

Contirbution repo PR: checkstyle/contribution#1021

@vivek-0509
Copy link
Contributor Author

GitHub, generate report

@vivek-0509
Copy link
Contributor Author

Github, generate site

@github-actions
Copy link
Contributor

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

@vivek-0509 vivek-0509 marked this pull request as draft January 15, 2026 16:13
@vivek-0509 vivek-0509 force-pushed the addingNewGoogleMemberNameCheck branch from 2508be9 to 0621ade Compare January 15, 2026 16:39
@vivek-0509
Copy link
Contributor Author

GitHub, generate report

@github-actions
Copy link
Contributor

@vivek-0509
Copy link
Contributor Author

Hey @romani, I have reviewed the diff report all are genuine violations.
https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/0621ade_2026170816/reports/diff/index.html

@vivek-0509 vivek-0509 marked this pull request as ready for review January 15, 2026 18:10
@vivek-0509 vivek-0509 changed the title Issue #17842: Add GoogleMemberNameCheck for Google Java Style compliance Issue #18494: Add GoogleMemberNameCheck for Google Java Style compliance Jan 15, 2026
@vivek-0509 vivek-0509 force-pushed the addingNewGoogleMemberNameCheck branch from 0621ade to 649199d Compare January 15, 2026 20:41
@romani
Copy link
Member

romani commented Jan 16, 2026

Diff report is very huge hard to get actual diff.
Please generate report in comparison to old module that covered this to catch actual diff. Messages likely should be overriden to be same to avoid diff in wording to be a reason of diff.

@vivek-0509
Copy link
Contributor Author

GitHub, generate report

@github-actions
Copy link
Contributor

@vivek-0509
Copy link
Contributor Author

vivek-0509 commented Jan 16, 2026

Hi @romani , I have generated a comparison diff report. I have overridden the messages to be identical. Here is the report https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/649199d_2026065114/reports/diff/index.html. Please let me know if I have understood your request correctly or if any changes are needed.

@romani
Copy link
Member

romani commented Jan 16, 2026

diff is still wild.
please give same id to both Checks to try avoid diff detection that is purely due to name of Check.

@vivek-0509
Copy link
Contributor Author

GitHub, generate report

@github-actions
Copy link
Contributor

@vivek-0509
Copy link
Contributor Author

@romani I updated the configurations to use the same id for both checks, but they still appear as Red/Green pairs. It seems the report tool uses the full Check class name for the rule name, so MemberName and GoogleMemberName are treated as different rules despite the shared ID. Am I missing something?

@vivek-0509
Copy link
Contributor Author

@romani
I was investigating the report generation tool and i find that currently, the diff report compares violations using the full source string in
https://github.com/checkstyle/contribution/blob/4ac81f9f9928d695869b93c568fced0d98a1e781/patch-diff-report-tool/src/main/java/com/github/checkstyle/data/CheckstyleRecord.java#L249-L252

When an explicit id is set in the config, the ID is appended to the class name in the format ClassName#id (e.g., MemberNameCheck#GoogleMemberNameDiff). This means when two different checks share the same id, their source strings still differ because of the class name prefix (e.g., MemberNameCheck#GoogleMemberNameDiff vs GoogleMemberNameCheck#GoogleMemberNameDiff). As a result, violations with identical line, column, severity, and message are incorrectly reported as "removed" from base and "added" in patch.

the fix can be that, If source contains # (indicating an explicit module ID was set), compare only the portion after # instead of the full string. This would allow proper matching of violations from different checks that share the same ID, and the diff report would show only true behavioral differences.

@romani
Copy link
Member

romani commented Jan 17, 2026

tools is right, we tried to hack it, but tool is right in it behavior to show diff.

@romani
Copy link
Member

romani commented Jan 17, 2026

so, we can look at diff and see why we have more violations that it used to be.

https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/649199d_2026150917/reports/diff/elasticsearch/index.html#A1
is good example, previosly we did not do violation, now we do.
https://google.github.io/styleguide/javaguide.html#s5.2.4-constant-names

google state that type immutability if critical point in decision. Checkstyle is not type aware, we can not make decision if it is immutable or not. So better to not give violation.
False positive will make users be frustrated, as it will block them, but false-negative (absence of violation) will not make users angry.

@vivek-0509
Copy link
Contributor Author

Understood, will change the scope to skip all static fields and update the issue accordingly.

@vivek-0509 vivek-0509 force-pushed the addingNewGoogleMemberNameCheck branch from 649199d to f342244 Compare January 17, 2026 10:03
@vivek-0509
Copy link
Contributor Author

GitHub, generate report

@github-actions
Copy link
Contributor

@vivek-0509
Copy link
Contributor Author

Copy link
Member

@romani romani left a comment

Choose a reason for hiding this comment

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

items:

* for word boundaries.</li>
* <li>Underscores may be used to separate adjacent numbers (e.g., version
* numbers like {@code guava33_4_5}), but NOT between letters and digits.</li>
* </ul>
Copy link
Member

Choose a reason for hiding this comment

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

we need to mention that static fields are skipped, as Chckstyle not able to make sure that used type is immutable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

* @since 13.1.0
*/
@StatelessCheck
public class GoogleMemberNameCheck extends AbstractCheck {
Copy link
Member

Choose a reason for hiding this comment

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

Checks that member names conform to the

Google Java Style Guide
for non-constant field naming.

hmm, why we named it GoogleMemberNameCheck ?

as it should be GoogleNonConstantFieldNameCheck.
and it will make it clear why we have limitation on static.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@vivek-0509 vivek-0509 force-pushed the addingNewGoogleMemberNameCheck branch from f342244 to 4024f27 Compare January 18, 2026 12:19
@vivek-0509
Copy link
Contributor Author

@romani all the changes done pr is ready for review

@vivek-0509
Copy link
Contributor Author

GitHub, generate report

@github-actions
Copy link
Contributor

@vivek-0509
Copy link
Contributor Author

GitHub, generate website

@vivek-0509 vivek-0509 changed the title Issue #18494: Add GoogleMemberNameCheck for Google Java Style compliance Issue #18494: Add GoogleNonConstantFieldNameCheck for Google Java Style compliance Jan 18, 2026
@vivek-0509 vivek-0509 force-pushed the addingNewGoogleMemberNameCheck branch from 4024f27 to ba427e9 Compare January 18, 2026 15:33
@vivek-0509
Copy link
Contributor Author

GitHub, generate website

@vivek-0509 vivek-0509 requested a review from romani January 19, 2026 21:20
@vivek-0509
Copy link
Contributor Author

@romani
I am considering improving the violation message for this check also to better guide users when special characters are used in field names.

Current message:
Non-constant field name ''{0}'' must start with a lowercase letter, min 2 chars, avoid single lowercase letter followed by uppercase.

Updated message:
Non-constant field name ''{0}'' must start with a lowercase letter, min 2 chars, avoid single lowercase letter followed by uppercase, and contain only letters, digits, or underscores.

For cases like $foo or foo$bar, the current message does not clearly explain why the name is invalid. The Google Style Guide 5.1 explicitly states:

"Identifiers use only ASCII letters and digits, and, in a small number of cases noted below, underscores. Thus each valid identifier name is matched by the regular expression \w+."

Does this violation message looks good to you??

@vivek-0509 vivek-0509 force-pushed the addingNewGoogleMemberNameCheck branch from ba427e9 to 0314c7f Compare January 21, 2026 21:14
@vivek-0509
Copy link
Contributor Author

vivek-0509 commented Jan 21, 2026

@romani I have updated the violation messages since this is the same case as we have done for the GoogleMethodNameCheck as discussed with @Zopsss in that PR, please let me know if any other changes are needed.

@vivek-0509
Copy link
Contributor Author

@Zopsss Do you want me to keep the full violation message for this check's Example1 also? I have not updated it since I have covered both the violation messages in the examples.

@vivek-0509
Copy link
Contributor Author

@romani Can you please review this PR when you have time

@Zopsss
Copy link
Member

Zopsss commented Feb 2, 2026

Do you want me to keep the full violation message for this check's Example1 also? I have not updated it since I have covered both the violation messages in the examples.

@vivek-0509 sorry for the delay, yes please do that.

@vivek-0509 vivek-0509 force-pushed the addingNewGoogleMemberNameCheck branch from 0314c7f to 0bb782c Compare February 2, 2026 11:25
@vivek-0509
Copy link
Contributor Author

GitHub, generate website

@vivek-0509
Copy link
Contributor Author

@Zopsss Done, please review

@vivek-0509
Copy link
Contributor Author

GitHub, generate report

@vivek-0509
Copy link
Contributor Author

CI Failure is unrelated

@github-actions
Copy link
Contributor

github-actions bot commented Feb 2, 2026

@vivek-0509
Copy link
Contributor Author

@Zopsss ping

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.

3 participants