Skip to content

Conversation

@motlin
Copy link

@motlin motlin commented Nov 25, 2025

What's changed?

Added a new ExplicitThis recipe that automatically adds the explicit this. prefix to instance field accesses and method invocations.

What's your motivation?

This recipe improves code clarity by making it immediately obvious when a field or method belongs to the current instance versus being a local variable, parameter, or static member.
The explicit this. prefix follows a common coding style preference in Java codebases.

Anything in particular you'd like reviewers to focus on?

I wound up writing quite a bit of code to determine whether we're looking at a non-static field that's not already prefixed with this. Is there a better way?

Should I register the recipe in any suite? Should I add the recipe to common-static-analysis.yml (commented out initially)?

Anyone you would like to review specifically?

Have you considered any alternatives or workarounds?

An alternative would be to make this configurable (e.g., only apply to fields, or only to methods), but starting with a simple "always add this." approach keeps the recipe straightforward.

Any additional context

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

@greg-at-moderne
Copy link
Contributor

Can you add a test with a typical setter implementation?
This is probably the most common example where of variable name shadowing. One being a field and another being a local.

Test(String value) {
super(value);
// Shadowed parameter: looks like a bug but replacing would change semantics
value = value;
Copy link
Author

Choose a reason for hiding this comment

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

Can you add a test with a typical setter implementation? This is probably the most common example where of variable name shadowing. One being a field and another being a local.

@greg-at-moderne I added tests for value = value in this constructor, and field = field in a setter. While these are "obvious" bugs - setting a parameter to itself - it was a deliberate choice not to replace these and the test shows they do not get replaced. The replacement would change semantics. It's not a bad idea to find and fix these but my preference is to separate recipes that may change semantics from recipes that perform pure refactorings and don't need much scrutiny. What do you think?

@motlin motlin force-pushed the explicit-this branch 2 times, most recently from a556fe4 to d379d75 Compare November 26, 2025 14:09
@timtebeek timtebeek self-requested a review January 12, 2026 12:27
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Jan 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Ready to Review

Development

Successfully merging this pull request may close these issues.

3 participants