Add Reassignable trait - #8175
Open
steve-aom-elliott wants to merge 3 commits into
Open
Conversation
A syntactic check that an expression could be the target of a plain assignment without introducing a compile error. Handles two shapes: - a J.Identifier referring to a non-final local declared in an enclosing block (method parameters are excluded because reassigning a parameter silently drops the caller's expected mutation). - a J.FieldAccess whose target is a bare J.Identifier and whose field is non-final. Array-element writes, parenthesized targets, method-chain receivers, and multi-hop qualified field access are intentionally not matched. Effectively-final semantics are not modelled.
steve-aom-elliott
marked this pull request as ready for review
July 3, 2026 21:13
2 tasks
sambsnyd
reviewed
Jul 3, 2026
sambsnyd
left a comment
Member
There was a problem hiding this comment.
Can you scan the codebase for places this could be used? I know there's the one in your recipe PR, but before we finalize the API it would be good to make sure this trait is applicable/handles variations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What
A new incubating
Reassignabletrait underorg.openrewrite.java.traitthat answers a syntactic question recipes tend to re-invent locally: could this expression be the target of a plain assignmenttarget = ...without introducing a compile error?Why
x.setFoo(...)shapes intox = x.rebuild()....build()— or any other transformation that needs to reassign a receiver — currently either duplicate the walk-up-the-cursor-and-check-flags logic each time, or silently skip cases where the receiver is a field access. Concrete example: theReassignable-shaped guard that landed inrewrite-jackson#153(MigrateMapperSettersToBuilder#isReassignableReceiver).Extracting the check as a trait gives future recipes a one-liner and gives us one place to sharpen the semantics.
Scope for v1
Deliberately narrow, matching the jackson recipe's semantics:
J.Identifierreferring to a non-final local declared in an enclosing block, and aJ.FieldAccesswhose target is a bareJ.Identifierand whose field is non-final.a.b.c), array-element writes, parenthesized targets, and method-chain receivers.Follow-up: migrate the jackson recipe to use the trait once this lands.
Test plan
./gradlew :rewrite-java:test --tests 'org.openrewrite.java.trait.ReassignableTest'— 5 cases (non-final local ✓, final local ✗, method parameter ✗, non-finalthis.field✓, finalthis.field✗)../gradlew :rewrite-java:test --tests 'org.openrewrite.java.trait.*'— no regression in other trait tests.