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

14.11: spec for switch doesn't allow wildcard of wrapper or enum

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 6
    • specification
    • Fix Understood
    • x86
    • windows_xp

      FULL PRODUCT VERSION :
      java version "1.6.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.6.0-beta2-b73)
      Java HotSpot(TM) Client VM (build 1.6.0-beta2-b73, mixed mode, sharing)


      A DESCRIPTION OF THE PROBLEM :
      The JLS 3 only allow to swicth on
      char, byte, short, int, Character, Byte, Short, Integer, or an enum type (see section 14.11)
      and not on type assignable without canversion in
      char, byte, short, int, Character, Byte, Short, Integer, or an enum type.

      With variance wilcard introduced in JDK 1.5), you can now create
      subtype of final type but you can't switch on it, the code below don't
      compile according to the spec.

      List<? extends Integer> list=Arrays.asList(1,2);
      switch(list.get(0)) {
          case 1:
          case 2:
      }

      I think the spec could be changed to let this code compile.

      Rémi Forax


      REPRODUCIBILITY :
      This bug can be reproduced always.

      CUSTOMER SUBMITTED WORKAROUND :
      use a temporary variable :

      List<? extends Integer> list=Arrays.asList(1,2);
      int value=list.get(0);
      switch() {
        case 1:
        case 2:
      }

            abuckley Alex Buckley
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: