rewrite-migrate-java icon indicating copy to clipboard operation
rewrite-migrate-java copied to clipboard

Class To Record

Open nmck257 opened this issue 1 year ago • 2 comments

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.

  • https://github.com/openrewrite/rewrite-migrate-java/issues/141

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

nmck257 avatar Jan 12 '24 14:01 nmck257

Thanks! Would indeed be nice to have, although we should be careful not to change this in libraries, as that would require that consumers are also updated. There had been a similar earlier issue logged as well, with a first implementation outline

  • https://github.com/openrewrite/rewrite/issues/887
  • https://github.com/openrewrite/rewrite/compare/main...JensKnipper:rewrite:main

timtebeek avatar Jan 14 '24 14:01 timtebeek

For the nice-to-haves Lombok can give you the signal you need. e.g. @Value, @Equals, etc. are all well known patterns to signal that those methods can be removed, etc.

blipper avatar Aug 15 '24 00:08 blipper