Skip to content

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

Merged
romani merged 1 commit intocheckstyle:masterfrom
vivek-0509:addingNewGoogleMemberNameCheck
Feb 26, 2026
Merged

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

Conversation

@vivek-0509
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

GitHub, generate report

@vivek-0509
Copy link
Copy Markdown
Contributor Author

Github, generate site

@github-actions
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

GitHub, generate report

@github-actions
Copy link
Copy Markdown
Contributor

@vivek-0509
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

GitHub, generate report

@github-actions
Copy link
Copy Markdown
Contributor

@vivek-0509
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

GitHub, generate report

@github-actions
Copy link
Copy Markdown
Contributor

@vivek-0509
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

GitHub, generate report

@github-actions
Copy link
Copy Markdown
Contributor

@vivek-0509
Copy link
Copy Markdown
Contributor Author

Copy link
Copy Markdown
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:

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

@Zopsss
https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/60f0289_2026020714/reports/diff/openjdk25/index.html#A493
one example of such new file which was not there in the old report

@vivek-0509
Copy link
Copy Markdown
Contributor Author

vivek-0509 commented Feb 13, 2026

TestPhaseIRMatching.java
https://checkstyle-diff-reports.s3.us-east-2.amazonaws.com/60f0289_2026020714/reports/diff/openjdk25/index.html#A15910
Also, this file shows an increased number of violations. We can see that even the old violations now appear on different line numbers, which indicates that the file was modified between the generation of the old and the new reports. so we catched more vioaltions that the new changes in TestPhaseIRMatching.java have introduced

In Old report
Screenshot 2026-02-13 at 11 56 48 PM

In New report
Screenshot 2026-02-13 at 11 57 06 PM

Copy link
Copy Markdown
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.

Really sorry for all the delays, everything looks fine now. Thanks a lot for all the hardwork!!!!

@vivek-0509
Copy link
Copy Markdown
Contributor Author

@Zopsss Thankyou very much for reviewing

@vivek-0509
Copy link
Copy Markdown
Contributor Author

@romani PR is waiting for your approval.

@vivek-0509
Copy link
Copy Markdown
Contributor Author

GitHub, generate report

@vivek-0509
Copy link
Copy Markdown
Contributor Author

GitHub, generate website

@github-actions
Copy link
Copy Markdown
Contributor

@vivek-0509
Copy link
Copy Markdown
Contributor Author

@romani Let me know if any changes are needed ...

Copy link
Copy Markdown
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:

Comment thread config/checkstyle-examples-suppressions.xml Outdated
* @return true if this variable should be checked
*/
private static boolean shouldCheckFieldName(DetailAST ast) {
final DetailAST modifiersAST = NullUtil.notNull(ast.findFirstToken(TokenTypes.MODIFIERS));
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.

why we think that modifiers always exists?
they defineterly not present always in code, is this our AST feature to have always this node , even it is empty ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, modifiers is always present in the AST for VARIABLE_DEF nodes, even when no modifiers are explicitly written in code (e.g., int x; still has an empty MODIFIERS node). This is a Checkstyle AST feature. Existing checks like MemberNameCheck, ConstantNameCheck all use ast.findFirstToken(TokenTypes.MODIFIERS) directly without null checks.
https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#MODIFIERS

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.

I thinks awesome knowledge, please create special method getModifiers(ast) in this Check and put this init javadoc, eventually this method will be moved to some util, no need to do this now.

Copy link
Copy Markdown
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 60f0289 to 40d2c54 Compare February 25, 2026 17:52
@vivek-0509
Copy link
Copy Markdown
Contributor Author

GitHub, generate report

@vivek-0509
Copy link
Copy Markdown
Contributor Author

GitHub, generate website

@github-actions
Copy link
Copy Markdown
Contributor

@vivek-0509
Copy link
Copy Markdown
Contributor Author

@romani All the changes done PR is ready for review

Copy link
Copy Markdown
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.

absolutely last:

final List<String> expectedXpathQueries = List.of(
"/COMPILATION_UNIT/CLASS_DEF"
+ "[./IDENT[@text='InputXpathGoogleNonConstantFieldNameSingleChar']]"
+ "/OBJBLOCK/VARIABLE_DEF/IDENT[@text='a']"
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.

All 3 examples should be different by Xpath structure.

Please make: as filed of class, as field of inner interface, as filed of inner record or anonymous class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Used class inside interface for input3 since interface fields are implicitly public static final and can't produce violations and used anonymous class for input3.


public final int httpClient = 0;
public final int mValue = 0;
// violation above, 'avoid single lowercase letter followed by uppercase'
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.

public final int myValue = 0;
public final int mValue = 0;

line above shows a suggested fix for line with violation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

private final int f = 0; // violation, 'be at least 2 chars'

protected final int fooBar1_1 = 0;
protected final int foo_bar = 0;
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.

protected final int fooBar = 0;
protected final int foo_bar = 0;

apply same for all others.

Copy link
Copy Markdown
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 40d2c54 to d049a65 Compare February 26, 2026 15:13
@vivek-0509
Copy link
Copy Markdown
Contributor Author

GitHub, generate website

@vivek-0509 vivek-0509 force-pushed the addingNewGoogleMemberNameCheck branch from d049a65 to 024ed2e Compare February 26, 2026 16:13
@vivek-0509
Copy link
Copy Markdown
Contributor Author

GitHub, generate website

@vivek-0509
Copy link
Copy Markdown
Contributor Author

@romani PR is ready for review

Copy link
Copy Markdown
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.

Thanks a lot for hard work on this module

Please start activation Google style of it

@romani romani merged commit 9259fc6 into checkstyle:master Feb 26, 2026
123 checks passed
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