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

Arrays.asList() can return a List that throws undocumented ArrayStoreException

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      This issue also occurs on JDK 8, 11, 17 and 19.

      A DESCRIPTION OF THE PROBLEM :
      The javadoc for java.util.List.set(E) indicates that the method will throw a ClassCastException if the class of the specified element prevents it from being added to this list, but Lists returned by Arrays.asList(E...) throw an ArrayStoreException when trying to set an element with a wrong type, which is undocumented.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Compile and run the following code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Throws ClassCastException
      ACTUAL -
      Throws ArrayStoreException

      ---------- BEGIN SOURCE ----------
      public class Main {
          @SuppressWarnings({"rawtypes", "unchecked"})
          public static void main(String[] args) {
              List list = Arrays.asList(new String[1]);
              list.set(0, 0);
          }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: