GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add RemovePrivateFieldUnderscores recipe by jhl221123 · Pull Request #643 · openrewrite/rewrite-static-analysis

Conversation

@jhl221123

@jhl221123 jhl221123 commented Jul 19, 2025 •
edited by timtebeek
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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 Recipe to remove underscores in private field names #630

Checklist

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@moderne-meeseeks moderne-meeseeks Bot added this to OpenRewrite Jul 19, 2025
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jul 19, 2025
@greg-at-moderne greg-at-moderne mentioned this pull request Jul 22, 2025
Recipe to remove underscores in private field names #630
Closed
@timtebeek timtebeek assigned jhl221123 Jul 22, 2025
@timtebeek timtebeek added the recipe label Jul 22, 2025
@timtebeek

timtebeek commented Jul 22, 2025

Copy link
Copy Markdown
Member

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

jhl221123 reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jhl221123
Add RemovePrivateFieldUnderscores recipe
0e9ede6
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.

Fixes openrewrite#630
@jhl221123
jhl221123 force-pushed the remove-private-field-underscores branch from 264ca2a to 0e9ede6 Compare August 4, 2025 10:23
@jhl221123
jhl221123 marked this pull request as ready for review August 4, 2025 10:25
@timtebeek
Merge branch 'main' into remove-private-field-underscores
d3f317b
timtebeek
timtebeek reviewed Aug 22, 2025
View reviewed changes
Comment thread src/test/java/org/openrewrite/staticanalysis/RemovePrivateFieldUnderscoresTest.java
java(
"""
public class ParseLocation {
private String _ruleName;

@timtebeek timtebeek Aug 22, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think it would be good to add a test showing either class_ or _class to show we do not end up renaming to restricted keywords.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jhl221123 reacted with heart emoji
All reactions
  • ❤️ 1 reaction

@jhl221123 jhl221123 Aug 29, 2025

Copy link
Copy Markdown
Author

There was a problem hiding this 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. I've added the test case!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
github-actions[bot]
github-actions Bot reviewed Aug 22, 2025
View reviewed changes
Comment thread src/main/java/org/openrewrite/staticanalysis/RemovePrivateFieldUnderscores.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

timtebeek and others added 2 commits August 22, 2025 21:11
@timtebeek @github-actions
Update src/main/java/org/openrewrite/staticanalysis/RemovePrivateFiel…
49f319d
…dUnderscores.java

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@jhl221123
Add test case for reserved keyword handling
4f72198
dsgrieve
dsgrieve reviewed Sep 5, 2025
View reviewed changes
Comment thread src/test/java/org/openrewrite/staticanalysis/RemovePrivateFieldUnderscoresTest.java Outdated
private String ruleName;

public String getRuleName() {
return this.ruleName;

@dsgrieve dsgrieve Sep 5, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

this is unnecessary where there is no ambiguity. I think a recipe should do one thing which, in this case, is remove the underscore. IMO, this behavior should be removed from the visitor.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jhl221123 jhl221123 Sep 5, 2025

Copy link
Copy Markdown
Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks for the feedback! Your point about recipes having a single responsibility makes a lot of sense. I'll get this updated shortly.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@dsgrieve

dsgrieve commented Sep 5, 2025

Copy link
Copy Markdown

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jhl221123
Refine recipe behavior based on feedback
ccfaec0
- Qualify with this. only when a field is shadowed by a local variable or parameter.
- Align tests with the new behavior and improve their clarity.
@jhl221123

jhl221123 commented Sep 8, 2025

Copy link
Copy Markdown
Author

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@dsgrieve

dsgrieve commented Sep 8, 2025

Copy link
Copy Markdown

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jhl221123
Prevent renaming fields to the unnamed variable
ed84a1c
- Skips renaming if the resulting name would be empty, _, or still start/end with an underscore.
@jhl221123

jhl221123 commented Sep 9, 2025

Copy link
Copy Markdown
Author

@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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

dsgrieve
dsgrieve reviewed Sep 9, 2025
View reviewed changes

@dsgrieve dsgrieve left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM, but I defer to @timtebeek for approval.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jhl221123 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
@timtebeek
timtebeek self-requested a review September 9, 2025 16:11
greg-at-moderne
greg-at-moderne reviewed Sep 10, 2025
View reviewed changes

@greg-at-moderne greg-at-moderne left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Another test case worth adding is using underscore in the middle of the name like:
String first_name;. No change expected then.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
greg-at-moderne
greg-at-moderne reviewed Sep 10, 2025
View reviewed changes
Comment thread src/main/java/org/openrewrite/staticanalysis/RemovePrivateFieldUnderscores.java
return id;
}

private boolean isAmbiguous() {

@greg-at-moderne greg-at-moderne Sep 10, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I am wondering if it wouldn't be better to re-use the existing logic of org.openrewrite.java.VariableNameUtils#generateVariableName. I'd call it with the newName as the baseName and refrain from any changes if the result is different.

This way:

  • the code is shorter here
  • and likely more thoroughly tested / better coverage of the naming logic

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@greg-at-moderne

greg-at-moderne commented Sep 10, 2025

Copy link
Copy Markdown
Contributor

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Dec 15, 2025

Copy link
Copy Markdown
Contributor

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the Stale label Dec 15, 2025
@jhl221123 jhl221123 mentioned this pull request Dec 28, 2025
📢오픈소스 기여모임 - 운영진 소개 opensource-contributors-group/opensource-contributors#2
Open
@github-actions github-actions Bot closed this Dec 29, 2025
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Dec 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@github-actions github-actions[bot] github-actions[bot] left review comments
@greg-at-moderne greg-at-moderne greg-at-moderne left review comments
@timtebeek timtebeek Awaiting requested review from timtebeek
+1 more reviewer
@dsgrieve dsgrieve dsgrieve left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@jhl221123 jhl221123

Labels

recipe Stale

Projects

OpenRewrite
Archived in project

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Recipe to remove underscores in private field names

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@jhl221123 @timtebeek @dsgrieve @greg-at-moderne
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.