Repository navigation
Conversation
| import org.openide.util.NbBundle.Messages; | ||
|
|
||
| /** | ||
| * Suggests converting a record declaration into a class. |
There was a problem hiding this comment.
- I am glad to see this "backwards conversion"
- often I start with
recordand then need to "open up" - this kind of conversion will be useful
There was a problem hiding this comment.
there is a chance that java will have compact constructors for regular classes too one day. Transition would be more fluent then.
| * The record components become {@code private final} fields, the record's | ||
| * implicit finality is preserved by a {@code final} class modifier, and an | ||
| * explicit canonical constructor is generated when the record does not | ||
| * declare one. |
There was a problem hiding this comment.
Record is more, I guess:
- there is a special
java.io.Serializablesupport (usually I don't care)- who needs Java serialization these days? Maybe Enso ;-?
- moreover it is probably quite complicated to implement
- there is special handling of
equalsandhashCode(I guess I do care often) - there is default
toString()implementation (useful I
Shall the conversion add compatible equals, hashCode and toString implementations?
There was a problem hiding this comment.
It certainly is part of the convenience of a record to have good automated impls for hash/identity/tostring. I thought about removing those in the class -> record direction at the very least. But this might be overreach if the class has well thought out methods of that kind already. The other direction could call the pre-existing generator actions.. but dev can do that too in a second pass so nothing is really lost.
This is already 1.4k lines for the minimal impl, smarter solutions often get complex quick. Maybe it is better to use this a release or two first before doing a followup. (when something is too-smart, it can also lead to bad UX since it becomes unpredictable. e.g "why did it not remove the toString here when it removed it over there").
There was a problem hiding this comment.
i actually thought about a second entry point for the action. "force record conversion". That one would lead to error badges in many cases. E.g remove default constructors, boilerplate methods and things like that.
|
First, thank you for looking into this! Regarding the class -> record conversion, unless I misread the code, it will convert classes with mutable fields, but it won't convert classes that have any explicit constructor. To me, personally, it feels like a class that is conceptually very far from a record: it basically relies on being constructed "empty" and then the fields are set later. Which cannot be done with records. E.g., I didn't try, but I think the code will convert this: to me, this class is very unsuitable to become a record. OTOH, for classes that would be very suitable to become records, like: will not be converted, as they have the constructor and accessor (despite neither the constructor, nor the accessor do anything funny). Do I miss something? For the record->class conversion, I think I would second to Jaroslav's sentiment around (I think I am mostly fine with ignoring the serialization aspect - in the worst case, denying the conversion or warning the user serialization may change for serializable records. I hope truly (binary) serializable records are/will be rare, and we can't detect other kinds of "serialization" anyway.) |
|
i can do this but this, in fact i already did start with it but stopped since this would end up being a lot more code. Probably 2x for the hint. But I agree it should be able to convert the snippet above. |
…gine Assisted-by: qwen3.8-27b
Assisted-by: qwen3.8-27b
Accept record-shaped classes: a canonical constructor that only assigns the parameters to the fields and record-style accessors are dropped in favor of the implicit ones, everything else (including equals/hashCode/toString) remains in the record body. Classes with an explicit super(...) call, local and anonymous classes are rejected. Add an option to also drop equals(), hashCode() and toString() methods in favor of the ones provided by java.lang.Record; the value is read when the fix is applied, so the preference can be changed after the hint was shown. Build TreePaths by direct linking instead of rescanning with Trees.getPath(). Assisted-by: qwen3.8-27b
aaf3185 to
a5e463b
Compare
(rebased, changes are in tmp third commit - since i want to test this all in one with git and osgi changes) |
first commit as preparation:
TreeMaker.Recordand record support to pretty printer and diff enginesecond commit:
class -> recordis a global hint by default,record -> classis current-line onlyAssisted-by: qwen3.8-27b
fixes #7762