JDK-8230518 : printSource doesn't handle enums correctly
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Priority: P3
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2019-09-04
  • Updated: 2024-08-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
There appear to be a few related issues here:

* annotations on enum constants are not printed
* the implicit constructor for the enum has an illegal call to `super()`

$ cat A.java 
enum E {
  @Deprecated ONE;
}
$ javac -fullversion -printsource A.java
javac full version "14-ea+12-409"
$ cat E.java
enum E {
    /*public static final*/ ONE /*enum*/ ;
    
    private E() {
        super();
    }
}
$ javac E.java
E.java:6: error: call to super not allowed in enum constructor
        super();
        ^
1 error

* additionally, subclasses of abstract enums include invalid synthetic constructors:

$ cat B.java 
enum E {
  ONE {
    void f() {}
  };

  abstract void f();
}
$ javac -fullversion -printsource B.java
javac full version "14-ea+12-409"
$ cat E.java
enum E {
    /*public static final*/ ONE /*enum*/  {
        
        E() {
            super();
        }
        
        void f() {
        }
    };
    
    private E() {
        super();
    }
    
    abstract void f();
}
$ javac E.java
E.java:5: error: invalid method declaration; return type required
        E() {
        ^
1 error

Comments
(re: better AST nodes: JDK-8024098, JDK-8182769.) That sounds great to me, although I think the fix to Pretty may be simple enough to be worth pursuing in parallel. I ran into this because some kotlin tooling was working around the issue with annotations on enum constants [1], and the work-around was perturbed [2] by the fix for JDK-8152616. [1] https://github.com/JetBrains/kotlin/commit/4c96453a4bb5779e4001f4c1d03a8d91a97e7c4b [2] https://youtrack.jetbrains.com/issue/KT-33052, https://github.com/JetBrains/kotlin/pull/2549
04-09-2019

While we can fix the pretty printer, the better long term solution, albeit a bigger one, is to have enum-specific AST nodes in the tree, to avoid desugaring in the parser and subsequent resugaring in the pretty printer.
04-09-2019