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

Arrays.asList methods needs better documentation

    Details

    • Type: CSR
    • Status: Closed
    • Priority: P4
    • Resolution: Approved
    • Fix Version/s: 12
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Compatibility Kind:
      behavioral
    • Compatibility Risk:
      minimal
    • Compatibility Risk Description:
      Spec change only. No behavior changes to the implemenation. Behavior is specified more tightly.
    • Interface Kind:
      Java API
    • Scope:
      SE

      Description

      Summary

      Enhance the specification of Arrays.asList to provide tighter assertions and to improve clarity of informative sections.

      Problem

      There are several minor issues with the Arrays.asList specification. It says that changes to the list "write through" to the array, without explaining this. It also doesn't say that changes to the array are visible in the list. In includes some informative text ("This method acts as a bridge...") mixed in with normative text. The examples should show that either an array or a variable number of arguments can be passed to Arrays.asList. Finally, it should emphasize that the list returned from Arrays.asList is modifiable, since it seems to be a common error to assume that the returned list is unmodifiable.

      Solution

      See revised specification text below. The method's signature and implementation are unchanged.

      Specification

      Patch:

       # HG changeset patch
       # User smarks
       # Date 1535208403 -19800
       #      Sat Aug 25 20:16:43 2018 +0530
       # Node ID 502a4a3a8053dc80590785ce722b0bd7e681b8d6
       # Parent  e777e997e7c10defa64230dc3cabe10fc021f769
      7033681: Arrays.asList methods needs better documentation
      Reviewed-by: smarks
      Contributed-by: Jaikiran Pai <jaikiran.pai@gmail.com>
      
      diff -r e777e997e7c1 -r 502a4a3a8053 src/java.base/share/classes/java/util/Arrays.java
      --- a/src/java.base/share/classes/java/util/Arrays.java Thu Sep 20 21:14:38 2018 +0200
      +++ b/src/java.base/share/classes/java/util/Arrays.java Sat Aug 25 20:16:43 2018 +0530
      @@ -28,6 +28,7 @@
       import jdk.internal.HotSpotIntrinsicCandidate;
       import jdk.internal.util.ArraysSupport;
      
      +import java.io.Serializable;
       import java.lang.reflect.Array;
       import java.util.concurrent.ForkJoinPool;
       import java.util.function.BinaryOperator;
      @@ -4288,21 +4289,41 @@
           // Misc
      
           /**
      -     * Returns a fixed-size list backed by the specified array.  (Changes to
      -     * the returned list "write through" to the array.)  This method acts
      -     * as bridge between array-based and collection-based APIs, in
      -     * combination with {@link Collection#toArray}.  The returned list is
      -     * serializable and implements {@link RandomAccess}.
      +     * Returns a fixed-size list backed by the specified array. Changes made to
      +     * the array will be visible in the returned list, and changes made to the
      +     * list will be visible in the array. The returned list is
      +     * {@link Serializable} and implements {@link RandomAccess}.
      +     *
      +     * <p>The returned list implements the optional {@code Collection} methods, except
      +     * those that would change the size of the returned list. Those methods leave
      +     * the list unchanged and throw {@link UnsupportedOperationException}.
      +     *
      +     * @apiNote
      +     * This method acts as bridge between array-based and collection-based
      +     * APIs, in combination with {@link Collection#toArray}.
      +     *
      +     * <p>This method provides a way to wrap an existing array:
      +     * <pre>{@code
      +     *     Integer[] numbers = ...
      +     *     ...
      +     *     List<Integer> values = Arrays.asList(numbers);
      +     * }</pre>
            *
            * <p>This method also provides a convenient way to create a fixed-size
            * list initialized to contain several elements:
      -     * <pre>
      -     *     List&lt;String&gt; stooges = Arrays.asList("Larry", "Moe", "Curly");
      -     * </pre>
      +     * <pre>{@code
      +     *     List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
      +     * }</pre>
      +     *
      +     * <p>The list returned by this method <em>is modifiable.</em>
      +     * To create an unmodifiable list, use
      +     * {@link Collections#unmodifiableList Collections.unmodifiableList}
      +     * or <a href="List.html#unmodifiable">Unmodifiable Lists</a>.
            *
            * @param <T> the class of the objects in the array
            * @param a the array by which the list will be backed
            * @return a list view of the specified array
      +     * @throws NullPointerException if the specified array is {@code null}
            */
           @SafeVarargs
           @SuppressWarnings("varargs")
      

      New specification:

          /**
           * Returns a fixed-size list backed by the specified array. Changes made to
           * the array will be visible in the returned list, and changes made to the
           * list will be visible in the array. The returned list is
           * {@link Serializable} and implements {@link RandomAccess}.
           *
           * <p>The returned list implements the optional {@code Collection} methods, except
           * those that would change the size of the returned list. Those methods leave
           * the list unchanged and throw {@link UnsupportedOperationException}.
           *
           * @apiNote
           * This method acts as bridge between array-based and collection-based
           * APIs, in combination with {@link Collection#toArray}.
           *
           * <p>This method provides a way to wrap an existing array:
           * <pre>{@code
           *     Integer[] numbers = ...
           *     ...
           *     List<Integer> values = Arrays.asList(numbers);
           * }</pre>
           *
           * <p>This method also provides a convenient way to create a fixed-size
           * list initialized to contain several elements:
           * <pre>{@code
           *     List<String> stooges = Arrays.asList("Larry", "Moe", "Curly");
           * }</pre>
           *
           * <p>The list returned by this method <em>is modifiable.</em>
           * To create an unmodifiable list, use
           * {@link Collections#unmodifiableList Collections.unmodifiableList}
           * or <a href="List.html#unmodifiable">Unmodifiable Lists</a>.
           *
           * @param <T> the class of the objects in the array
           * @param a the array by which the list will be backed
           * @return a list view of the specified array
           * @throws NullPointerException if the specified array is {@code null}
           */
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                smarks Stuart Marks
                Reporter:
                webbuggrp Webbug Group
                Reviewed By:
                Brian Burkhalter, Martin Buchholz
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: