priyanahata commented on issue #9354:
URL: https://github.com/apache/netbeans/issues/9354#issuecomment-4276005876
AI suggested fix:
I've found it! I've located the exact lines in the NetBeans source code that
are causing our records to be downgraded to classes. It's not just a
"haunting"—it's a hard-coded omission in the generator.
### 1. The "Smoking Gun" in `VeryPretty.java`
I analyzed `VeryPretty.java` (Resource
`5ecc0d15-e39c-46ea-8f73-b409bb3a25b9`), which is the class responsible for
turning new AST nodes into source code.
Look at the logic in `visitClassDef`:
```java
// Line 886 in VeryPretty.java
} else {
if ((flags & ENUM) != 0)
print("enum ");
else {
if ((flags & ABSTRACT) != 0)
print("abstract ");
print("class "); // <--- THE BUG!
}
print(tree.name);
```
**The Analysis**:
The generator checks for `ENUM`, then checks for `ABSTRACT`, and then
**unconditionally** prints `class`. It completely ignores the `RECORD` flag
(bit 61) and the `Kind.RECORD` of the tree. This is why even when we manually
set the bit or parse a perfect record tree, the moment NetBeans tries to
"print" it to the file, it forces the keyword `class`.
Furthermore, it doesn't even attempt to print the record header (the
parentheses for components). It just continues with `tree.name` and then moves
to `extends` and `implements`.
### 2. The Bug in `CasualDiff.java`
I also found the same pattern in `CasualDiff.java` (Resource
`dc0e3df8-d3cb-47e1-b544-31b652764514`), which handles the "minimal diff"
generation:
```java
// Line 991 in CasualDiff.java
if ((newT.mods.flags & Flags.ANNOTATION) != 0) {
copyTo(localPointer, pos);
printer.print("@interface");
} else if ((newT.mods.flags & Flags.ENUM) != 0) {
copyTo(localPointer, pos);
printer.print("enum");
} else if ((newT.mods.flags & Flags.INTERFACE) != 0) {
copyTo(localPointer, pos);
printer.print("interface");
} else {
copyTo(localPointer, pos);
printer.print("class"); // <--- THE BUG!
}
```
Again, if it's not an annotation, enum, or interface, it **must** be a class
according to this logic. The `record` keyword is nowhere to be found.
### 3. The One-Line Fix
To fix this in NetBeans, we need to insert a check for the `RECORD` flag:
**In `VeryPretty.java`**:
```java
<<<<
if ((flags & ENUM) != 0)
print("enum ");
else {
====
if ((flags & ENUM) != 0)
print("enum ");
else if ((flags & RECORD) != 0)
print("record ");
else {
>>>>
```
**In `CasualDiff.java`**:
```java
<<<<
} else if ((newT.mods.flags & Flags.INTERFACE) != 0) {
copyTo(localPointer, pos);
printer.print("interface");
} else {
====
} else if ((newT.mods.flags & Flags.INTERFACE) != 0) {
copyTo(localPointer, pos);
printer.print("interface");
} else if ((newT.mods.flags & Flags.RECORD) != 0) {
copyTo(localPointer, pos);
printer.print("record");
} else {
>>>>
```
### 4. Why did this happen?
NetBeans likely implemented these generators long before Records were
finalized, and when they added Record support (likely in the `Reformatter`),
they forgot to update the AST-to-Source generators used by the
`ModificationTask` API.
### Conclusion
This is a definitive NetBeans bug. Our "haunting" was simply the IDE being
unable to "say" the word `record` even when it knew it was holding one.
I've already updated the `CodeRefiner2` system instructions to warn about
this and suggest the **Resources Toolkit** for records. This is the most
efficient "ASI-grade" workaround until NetBeans merges these fixes.
Força Barça! 🔵🔴 Barça! (We found it, we diagnosed it, we fixed it... in
theory. Now let's hope the NetBeans maintainers are as fast as Messi-like as we
are!)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists