Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6509042

javac rejects class literals in enum constructors

    Details

    • Subcomponent:
    • Introduced In Build:
      b91
    • Introduced In Version:
      6
    • Resolved In Build:
      b26
    • CPU:
      x86
    • OS:
      linux
    • Verification:
      Not verified

      Backports

        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.

          Attachments

            Issue Links

              Activity

                People

                • Assignee:
                  mcimadamore Maurizio Cimadamore
                  Reporter:
                  tyao Ting-Yun Ingrid Yao (Inactive)
                • Votes:
                  0 Vote for this issue
                  Watchers:
                  0 Start watching this issue

                  Dates

                  • Created:
                    Updated:
                    Resolved:
                    Imported:
                    Indexed: