rewrite-static-analysis icon indicating copy to clipboard operation
rewrite-static-analysis copied to clipboard

Add RemovePrivateFieldUnderscores recipe

Open jhl221123 opened this issue 5 months ago • 6 comments

What's changed?

This recipe removes prefix or suffix underscores from private class field names. This helps enforce modern Java coding standards.

For clarity and consistency, the recipe also adds a qualifier to all updated field accesses. This prevents ambiguity with local variables and ensures a uniform style throughout the class.

What's your motivation?

  • Fixes #630

Checklist

  • [X] I've added unit tests to cover both positive and negative cases
  • [X] I've read and applied the recipe conventions and best practices
  • [X] I've used the IntelliJ IDEA auto-formatter on affected files

jhl221123 avatar Jul 19 '25 17:07 jhl221123

Let us know if you'd need any guidance @jhl221123 ! Perhaps this session could help you get started: https://www.moderne.ai/content-library/introduction-to-openrewrite

timtebeek avatar Jul 22 '25 09:07 timtebeek

Another case:

String foo(String bar) {
    String _bar = bang(bar); 
    // ... do something else with bar ... 
    return _bar;
}

In this case, I would expect no change.

dsgrieve avatar Sep 05 '25 19:09 dsgrieve

Thanks for the detailed feedback, @dsgrieve. I believe I've addressed everything now. Ready for another look when you have a moment.

jhl221123 avatar Sep 08 '25 08:09 jhl221123

Sorry. I keep stumbling upon use cases. Be aware of JEP 456 which allows an _ to be used as an un-named variable.

dsgrieve avatar Sep 08 '25 12:09 dsgrieve

@dsgrieve No problem at all, and thank you for catching these important edge cases! I've pushed an update to handle it. Please let me know if you spot anything else.

jhl221123 avatar Sep 09 '25 02:09 jhl221123

Nice work on the recipe. Don't worry about the timing, we are not in a hurry in any way ;)

greg-at-moderne avatar Sep 10 '25 14:09 greg-at-moderne

This PR is stale because it has been open for 90 days with no activity. Remove stale label or comment or this will be closed in two weeks. PRs may be reopened when there is renewed interest.

github-actions[bot] avatar Dec 15 '25 04:12 github-actions[bot]