apache / netbeans

Apache NetBeans
https://netbeans.apache.org/
Apache License 2.0
2.64k stars 849 forks source link

CodeGenerator should be able to generate basic code for records #7581

Closed mbien closed 2 months ago

mbien commented 3 months ago

generates a final class as workaround instead of throwing an exception

fixes https://github.com/apache/netbeans/issues/7570 (see for reproducer)

So i tried to implement this properly at first, but there is some magic behind the scenes in the javac impl which kept generating final classes even though everything seemed to be correct. I had this in TreeFactory which works analog to Enum:

    public ClassTree Record(ModifiersTree modifiers, 
             CharSequence simpleName,
             List<? extends Tree> implementsClauses,
             List<? extends Tree> memberDecls) {
        long flags = getBitFlags(modifiers.getFlags()) | Flags.RECORD;
        return Class(flags, (com.sun.tools.javac.util.List<JCAnnotation>) modifiers.getAnnotations(), simpleName, Collections.<TypeParameterTree>emptyList(), null, implementsClauses, memberDecls);
    }

since this had no effect, I decided to change this back to a minimal impact quickfix which simply calls make.Class right away, without introducing public API changes. This would also have to be updated to extend AbstractElementVisitor14 as the todo indicates.

Edit: I might give this a second attempt to implement it properly

dbalek commented 2 months ago

In addition to your changes to the TreeFactory, you have to IMHO change also the org.netbeans.modules.java.source.pretty.VeryPretty.visitClassDef(...) to generate records properly.

mbien commented 2 months ago

In addition to your changes to the TreeFactory, you have to IMHO change also the org.netbeans.modules.java.source.pretty.VeryPretty.visitClassDef(...) to generate records properly.

@dbalek thanks for the hint! I believe this was the missing piece why I could not figure out why it kept generating final classes. I will give this another attempt.

mbien commented 2 months ago

this would be a bit more work, due to the fact that VeryPretty would need access to the members while writing the record header, those are visited later however.

I think we could get this into NB 23 so that is generates the record as final class instead of throwing an exception. It can be still properly implemented later.