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

enum type variable not within its bound

XMLWordPrintable

      FULL PRODUCT VERSION :
      java version " 1.7.0_07 "
      Java(TM) SE Runtime Environment (build 1.7.0_07-b11)
      Java HotSpot(TM) 64-Bit Server VM (build 23.3-b01, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      I have " public interface MyInterface<T extends Enum<T> & OtherInterface> " as the generic type of an interface, and one of the methods of the interface returns type T, which is obviously an enum. However, a separate static method that takes an enum as an argument will not recognize the returned type T as an enum.

      See attached test case.

      The attached test case fails to compile both in JDK 6 and JDK 7. It compiles fine in Eclipse 4.2.1.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      javac BugEnum.java

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The program should compile and produce the string " success " when run.
      ACTUAL -
      Java refuses to compile the class.

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
        BugEnum.java:24: error: method getSerializationName in class BugEnum cannot be applied to given types;
                      System.out.println(getSerializationName(testMessage.getCommand()));
                                         ^
        required: E
        found: INT#1
        reason: inferred type does not conform to declared bound(s)
          inferred: INT#1
          bound(s): Enum<INT#1>
        where E is a type-variable:
          E extends Enum<E> declared in method <E>getSerializationName(E)
        where CAP#1 is a fresh type-variable:
          CAP#1 extends INT#1 from capture of ?
        where INT#1 is an intersection type:
          INT#1 extends Enum<CAP#1>,Command
      1 error

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      /**Illustration of Java Enum-related compile bug.
      *@author Garret Wilson
      */
      public class BugEnum {

      public interface Command {}

      public interface CommandMessage<C extends Enum<C> & Command> {
      public C getCommand();
      }

      public enum TestCommand implements Command {SUCCESS, FAILURE}

      public static class TestCommandMessage implements CommandMessage<TestCommand> {
      @Override public TestCommand getCommand() {return TestCommand.SUCCESS;}
      }

      public static <E extends Enum<E>> String getSerializationName(final E e) {
      return e.name().toString().toLowerCase();
      }

      public static void main(String... args) {
      CommandMessage<?> testMessage=new TestCommandMessage();
      System.out.println(getSerializationName(testMessage.getCommand()));
      }

      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      This bug is hard to work around without removing the use of generics and/or having various (Object) casts and such.

            dlsmith Dan Smith
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: