openrewrite / openrewrite/rewrite

Introduce record components to support working with deconstruction patterns

Open
#5,755 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement java parser
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.