7 Replies Latest reply: Apr 11, 2013 12:13 PM by aksarben RSS

    Generic Enum Casts - Java 6 Bug

    aksarben
      I've stumbled across a Java 6 compiler bug (http://bugs.sun.com/view_bug.do?bug_id=6548436) that's giving me fits. The bug prevents casting a generic enum to a specific enum type. The following example illustrates the problem. It compiles without warnings or errors under Eclipse's incremental compiler (Juno version), but fails under javac with an "inconvertible types" error:
      enum Animal { WHALE, BADGER, CAT, SQUID, WOLVERINE, DOG, CENTAUR, DINOSAUR; }
      
      public class Test {
      public static void main(final String[] args) {
           testEnumCast(Animal.DOG);
      }
      private static void testEnumCast(final Enum<?> property) {
           switch ((Animal)property) {
                case DOG:
                     System.out.println("You chose dog");
                     break;
                default:
                     System.out.println("You chose something else");
                     break;
                }
           }
      }
      Though the bug is reportedly fixed in Java 7, I'm in a corporate environment where the servers & change management processes use Java 6 (for now), so I don't have the luxury of upgrading to Java 7 in the short term.

      My question is this: Has anyone found a way to make generic enum casts work under Java 6? Any bright ideas would be greatly appreciated!