You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
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.
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
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.
What problem are you trying to solve?
It would be neat to have a capability to convert a
class
to arecord
What precondition(s) should be checked before applying this recipe?
class
to arecord
)class
from being arecord
, such as:this(...)
in the first lineDescribe 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
getColor()
tocolor()
)equals
/hashCode
/toString
implementations are removed in favor of the defaultHave 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
The text was updated successfully, but these errors were encountered: