openrewrite / openrewrite/rewrite
Introduce record components to support working with deconstruction patterns
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 3.7k
- Forks
- 570
- Avg merge
- 13h 12m
- Merged PRs (30d)
- 261
Description
What problem are you trying to solve?
While working on replacing a deconstruction pattern in a switch expression, I got to the point where I had to match the nested components of the pattern to their corresponding record accessors. Using the members of the JavaType.Class is not sufficient, as these are not ordered.
Describe the solution you'd like
Currently, when creating a JavaType.Class, only the members_field of ClassSymbol is used. To not break any compatibility by ordering the members for records, I'd suggest introducing a recordComponents field on JavaType.Class, or a new JavaType.Record extends JavaType.Class. The latter seems more appropriate.
Additional context
Original slack message:
It seems JavaType.Class#getMembers() is not correctly sorted for records. This makes it impossible to map deconstruction patterns to its record component. Given record FooBar<T>(T data, String data2) {} and a case FooBar(var obj, String data2), deconstructionPattern.getNested() returns [var obj, String data2], but the members of deconstructionPattern.getDeconstructor() (through getType, cast to Identifier, unpack the Parameterized to get to the JavaType.Class ) are [Test$FooBar{name=data2,type=java.lang.String}, Test$FooBar{name=data,type=Generic{T}}].
Note that obj does not match the record accessor data() , so a name match is not possible depending on user code.
Are you interested in contributing this feature to OpenRewrite?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by tracing how JavaType.Class is created from ClassSymbol.members_field and how its members are consumed when matching deconstruction patterns. Compare the proposed recordComponents field with a JavaType.Record subtype, then add coverage for the FooBar(T data, String data2) nested-pattern case and verify that component order is preserved without changing ordinary class member ordering.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- devtools
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100