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

Possible to insert elements/keys of wrong enum type into EnumSet/Map

    Details

    • Subcomponent:
    • Resolved In Build:
      b56
    • CPU:
      generic
    • OS:
      generic

      Description

      As of now it is possible to construct EnumMap from Map containing Keys from different Enums.

      Is it possible to enumerate the keys in the Map and if their Class is not same
      user is intimated of the same.

      Please see the code below:-

      ***Test7.java*********
      import java.util.EnumMap;
      import java.util.Map;
      import java.util.Hashtable;

      /* @author : Seetharama avadhanam
       * This test case to test Every Enum map must come from single Enum..... FAILED TEST CASE
       */
      public class Test7 {
          public static void main (String args[]) {
              Map map;
              Hashtable hs = new Hashtable();
              EnumMap<Season, Integer> emap;
              try{
                  
                  hs.put(Season.FALL,2);
                  hs.put(Temp.FALL,3);
                  hs.put(Season.WINTER,4);
                  hs.put(Temp.SUMMER,5);
                  map = (Map)hs;
                  emap = new EnumMap(map);
                  
                  System.out.println(map);
                  System.out.println(emap);
                  
              }catch(Exception ex){
                  ex.printStackTrace();
              }
              
          }
      }

      *******Temp.java*************
      enum Temp{
                WINTER(456), SPRING(+77), SUMMER(-2222), FALL(-222);

          Temp(int value){
              this.value=value;
          }
          Temp(double value){
              this((int)value);
          }
          private int value;

          public int getValue(){
              return value;
          }
      }

      ********Season.java**************
      /* This Enum is written to test EnumSet and EnumMap.
       */

      public enum Season {
          WINTER(-9999+20.98,"No Sting"), SPRING(+77), SUMMER(-2222), FALL("String",-222);
          Season(int value,String second) { this.value = value; this.second = second; }
          Season(double value,String second) { this.value = (int)value; this.second = second; }
          Season(double value) {this.value = (int)value; this.second = "double value";}
          Season(int value) {this.value = value; this.second = "int Value";}
          Season(String second,int value){ this.value = value; this.second = second; }
          public int value;
          public String second;
          public int getValue() { return value; }
          public String getString() { return second; }
          public void setString(int value, String second){
              this.value = value;
              this.second =second;
          }
       }
       
      ************************************************************************

      The problem is far wider in scope, as demonstrated by this program:

      import java.util.*;

      public class Bug {
          public static void main(String[] args) {
              EnumSet s = EnumSet.noneOf(Cat.class);
              s.add(Dog.DACHSHUND);
              System.out.println(s); // Prints "[CALICO]"
              s.add(Dog.BEAGLE);
              System.out.println(s); // Throws ArrayIndexOutOfBoundsException
          }

          enum Cat { CALICO, TABBY }
          enum Dog { DACHSHUND, SCHNAUZER, BEAGLE }
      }

      ###@###.### 2004-05-28

        Attachments

          Activity

            People

            • Assignee:
              jjb Josh Bloch
              Reporter:
              jsinghsunw Jit Singh (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: