Java Solaris Communities Sun Store Join SDN My Profile Why Join?
 
Bug Database
Bug Detail
Quick Lists
Top 25 Bugs
Top 25 RFE's
Recently Closed Bugs
Printable Page Printable Page


Bug Database
Bug ID: 6509042
Votes 1
Synopsis javac rejects class literals in enum constructors
Category java:compiler
Reported Against
Release Fixed 7(b26), 6u10(b26) (Bug ID:2163376) , 6-open(b12) (Bug ID:2165585)
State 10-Fix Delivered, bug
Priority: 2-High
Related Bugs
Submit Date 03-JAN-2007
Description
FULL PRODUCT VERSION :
JDK6 release

ADDITIONAL OS VERSION INFORMATION :
All platforms

EXTRA RELEVANT SYSTEM CONFIGURATION :
All configurations

A DESCRIPTION OF THE PROBLEM :
The following code should compile without error, and did compile without error until very late in the JDK6 candidates

frog:~/tmp$ cat -n F.java
     1  enum Foo {
     2    A, B;
     3    Foo() {
     4      Object x = Foo.class;
     5    }
     6  }
frog:~/tmp$ /usr/local/jdk6/bin/java -version
java version "1.6.0-rc"
Java(TM) SE Runtime Environment (build 1.6.0-rc-b104)
Java HotSpot(TM) Client VM (build 1.6.0-rc-b104, mixed mode, sharing)
frog:~/tmp$ /usr/local/jdk6/bin/javac F.java
F.java:4: illegal reference to static field from initializer
    Object x = Foo.class;
                  ^
1 error
frog:~/tmp$


REGRESSION.  Last worked in version mustang

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
See description

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Code should compile without error.
ACTUAL -
javac rejects the code, as shown in the description.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
F.java:4: illegal reference to static field from initializer

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
enum Foo {
  A, B;
  Foo() {
    Object x = Foo.class;
  }
}

---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Use Class.forName instead of a class literal. Unfortunately, the result of Class.forName doesn't have the same generic type as the class literal.
Posted Date : 2007-01-03 04:06:44.0
Work Around
N/A
Evaluation
JLS (8.9) say that "It is a compile-time error to reference a static field of an enum type that is not a compile-time constant from constructors, instance initializer blocks, or instance variable initializer expressions of that type".

During the attribution phase, class literals are handled by javac in the same way as static fields. This means that javac assumes that there is a static field names "class" within another class (Foo in the example above). 

The compiler reports an error since it sees a static field being accessed within an enum constructor; however, since there's no true field access here, the compiler should not reject this code. This problem can be avoided by not applying the checks described in JLS 8.9 to those static fields whose name is "class".
*** (#1 of 1): [ UNSAVED ]   xxxxx@xxxxx  
Posted Date : 2007-12-17 13:30:34.0
Comments
  
  Include a link with my name & email   


PLEASE NOTE: JDK6 is formerly known as Project Mustang