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

Unmodifiable map entry becomes modifiable if taken from a stream of map entries

    XMLWordPrintable

Details

    • 8
    • b100
    • Verified

    Description

      The problem is reproducible with b95 and b95-lambda promotions.

      Please see the following sample:

      ------------------------------------------------------------------------------------------------------
      import java.util.*;

      public class NowModifiable {

          public static void main(String[] args) {

              final HashMap<String, String> hashMap = new HashMap<>();
              hashMap.put("a", "a");
              final Set<Map.Entry<String, String>> unmodifEntrySet = Collections.unmodifiableMap(hashMap).entrySet();

              System.err.println("Entry type = " + unmodifEntrySet.iterator().next().getClass());
              System.err.println("Entry type from stream = " + unmodifEntrySet.stream().findFirst().get().getClass());

              unmodifEntrySet.stream().findFirst().get().setValue("new value1");
              System.err.println("unexpectedly set a new value using element of a stream");

              unmodifEntrySet.iterator().next().setValue("new value2");
              System.err.println("unexpectedly set a new value using element of iterator");

          }
      }
      ------------------------------------------------------------------------------------------------------

      The output will be like:

      Entry type = class java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry
      Entry type from stream = class java.util.HashMap$Entry
      unexpectedly set a new value using element of a stream
      Exception in thread "main" java.lang.UnsupportedOperationException
      at java.util.Collections$UnmodifiableMap$UnmodifiableEntrySet$UnmodifiableEntry.setValue(Collections.java:1608)


      The following JCK tests fail due to this issue:

      api/java_util/Collections/unmodifiable/index.html#UnmodifiableMapEntrySetStream
      api/java_util/Collections/unmodifiable/index.html#UnmodifiableMapEntrySetParallelStream
      api/java_util/Collections/unmodifiable/index.html#UnmodifiableSortedMapEntrySetParallelStream
      api/java_util/Collections/unmodifiable/index.html#UnmodifiableSortedMapEntrySetStream


      Attachments

        Issue Links

          Activity

            People

              psandoz Paul Sandoz
              dbessono Dmitry Bessonov
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: