openrewrite / openrewrite/rewrite-migrate-java

Class To Record

Open
#391 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

recipe
Dominant language
Java
Stars
156
Forks
130
Avg merge
20h 57m
Merged PRs (30d)
21

Description

What problem are you trying to solve?

It would be neat to have a capability to convert a class to a record

What precondition(s) should be checked before applying this recipe?

  • Java 17+
  • Should probably have an arg to specify the type to transform (versus rampantly changing every eligible class to a record)
  • Check for things that would disqualify the class from being a record, such as:
    • type must not participate in inheritance
    • type must be immutable
      • type must not have setters (or maybe make an extra-fancy version of this recipe which can replaces usages of setters with a constructor call) (and/or maybe include a force-override option for this no-setters rule)
      • non-static fields must be final or effectively final
    • any custom constructor(s) must delegate to another constructor via this(...) in the first line

Describe the situation before applying the recipe

See Java's specification for Records for context:
https://docs.oracle.com/en/java/javase/17/language/records.html#GUID-6699E26F-4A9B-4393-A08B-1E47D4B2D263

Describe the situation after applying the recipe

  • The targeted class should be restructured as a record
  • Method invocations of the type's getters should be replaced with record property accessors (eg getColor() to color())
  • (bonus points) "canonical" equals/hashCode/toString implementations are removed in favor of the default

Have you considered any alternatives or workarounds?

Any additional context

The implementation for Lombok value to record likely has some useful code to reference/reuse.

This feels like a problem with many weird edge cases (even beyond what's written above), but even a naive implementation which only handles some of those scenarios could probably still be valuable, and the recipe could grow iteratively from there.

Are you interested in contributing this recipe to OpenRewrite?

Maybe; priority TBD

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 with the Java 17 Records specification linked in the issue and inspect the Lombok value-to-record implementation referenced through issue #141. Define the supported class preconditions and affected transformations before implementing them; done should include restructuring eligible classes, updating getter calls, and removing canonical methods where applicable.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
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.