Map.of() derived view collection mutators should throw UnsupportedOperationException

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: P4
    • tbd
    • Affects Version/s: 11
    • Component/s: core-libs
    • None

      My colleague Sorin Basca reports:

      A DESCRIPTION OF THE PROBLEM:

      Calling Map.of().clear() results in an UnsupportedOperationException. However, calling Map.of().entrySet().clear() just returns doing nothing. Both calls should behave in the same way.

      ROOT CAUSE:

      Calling Map.of() returns an ImmutableCollections.EMPTY_MAP which returns a MapN object. MapN.entrySet() returns an object that does not override clear(), which means the default clear is used - it iterates and removes items until iterator.hasNext() is false - so the function just returns without removing anything. However, if calling MapN.clear() directly, this will throw an UnsupportedOperationException.

      POSSIBLE SOLUTION:

      Override clear() inside the entrySet class returned from MapN to also throw UnsupportedOperationException.

            Assignee:
            Liam Miller-Cushon
            Reporter:
            Liam Miller-Cushon
            Votes:
            1 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated: