Bug 126087 - [1.5][compiler] Java compiler generates incorrect byte code for empty enums
Summary: [1.5][compiler] Java compiler generates incorrect byte code for empty enums
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 major (vote)
Target Milestone: 3.2 M5   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-02-01 15:22 EST by Norbert Kuck CLA
Modified: 2021-01-05 23:25 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Norbert Kuck CLA 2006-02-01 15:22:42 EST
The Eclipse Java compiler does not generate correct byte code 
for the special case that an enum is empty (i.e. has no values).

In this case, it fails to generate a <clinit> method. As a 
result, the hidden ENUM$VALUES static variable remains null. 
This leads to a NullPointerException when calling the values() 
method (it tries to copy the ENUM$VALUES array and fails).

Correct behavior in this case would be to generate a <clinit>
method that initializes ENUM$VALUES to an array of length 0.

The following test program:
  public class EnumTest {
    enum NoValues {}
    public static void main(String[] args) {
      System.out.println(NoValues.values());
    }
  }

results in the following exception:
  Exception in thread "main" java.lang.NullPointerException
    at EnumTest$NoValues.values(EnumTest.java:1)
    at EnumTest.main(EnumTest.java:4)

but should print something like this (compiled with Sun javac):
  [LEnumTest$NoValues;@45a877

Tested on Eclipse Version: 3.1.2 (Build id: M20060118-1600). 
Earlier versions have the same bug.
Comment 1 Philipe Mulet CLA 2006-02-01 17:32:29 EST
Good find. Added EnumTest#test126.

Need to fix both Clinit creation code, and codegen.
Olivier - we could optimize synth methods codegen to handle zero scenario more efficiently (though likely not a real world scenario).
Comment 2 Philipe Mulet CLA 2006-02-01 17:44:14 EST
Backported to 3.1.x as indeed it looks bad, and fix is quite simple.
Comment 3 Philipe Mulet CLA 2006-02-01 18:27:21 EST
fixed in head
Comment 4 Philipe Mulet CLA 2006-02-02 04:25:50 EST
Olivier - pls check FormatterRegressionTests#test523&525 (both in HEAD and 3.1.x branch). Their behavior seems altered from presence of <clinit> which it shouldn't. Is this a formatter bug surfaced ? (if so need separate bug)
Comment 5 Olivier Thomann CLA 2006-02-02 10:11:42 EST
Entered bug 126191.
Comment 6 Jerome Lanneluc CLA 2006-02-15 07:41:26 EST
Verified for 3.2 M5 using build I20060215-0010