Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class To Record #391

Open
nmck257 opened this issue Jan 12, 2024 · 2 comments
Open

Class To Record #391

nmck257 opened this issue Jan 12, 2024 · 2 comments
Labels
recipe Recipe requested

Comments

@nmck257
Copy link
Contributor

nmck257 commented Jan 12, 2024

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

@timtebeek timtebeek added the recipe Recipe requested label Jan 14, 2024
@timtebeek
Copy link
Contributor

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

@blipper
Copy link

blipper commented Aug 15, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
recipe Recipe requested
Projects
Status: Recipes Wanted
Development

No branches or pull requests

3 participants