Community
Participate
Working Groups
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.
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).
Backported to 3.1.x as indeed it looks bad, and fix is quite simple.
fixed in head
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)
Entered bug 126191.
Verified for 3.2 M5 using build I20060215-0010