Changeset 19133 in josm for trunk/src/org/openstreetmap


Ignore:
Timestamp:
2024-07-09T15:50:36+02:00 (7 months ago)
Author:
taylor.smock
Message:

See #23727, #23748: Re-enable Map.ofEntries for Utils#toUnmodifiableMap

This restores the behavior that was present prior to r19101 without reflection.

In r19100 and earlier, the NPE was caught and rethrown as a reflection exception.
At this point, we then returned a Collections.unmodifiableMap object.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/tools/Utils.java

    r19124 r19133  
    658658     *     How to Prevent Your Java Collections From Wasting Memory</a>
    659659     */
    660     @SuppressWarnings("unchecked")
     660    @SuppressWarnings({"unchecked", "squid:S1696"})
    661661    public static <K, V> Map<K, V> toUnmodifiableMap(Map<K, V> map) {
    662662        if (isEmpty(map)) {
     
    666666            return Collections.singletonMap(entry.getKey(), entry.getValue());
    667667        }
    668         // see #23748 don't use Map.ofEntries as it doesn't allow null keys or values
     668        // see #23748: If the map contains `null`, then Map.ofEntries will throw an NPE.
     669        // We also cannot check the map for `null`, since that may _also_ throw an NPE.
     670        try {
     671            return Map.ofEntries(map.entrySet().toArray(new Map.Entry[0]));
     672        } catch (NullPointerException e) {
     673            Logging.trace(e);
     674        }
    669675        return Collections.unmodifiableMap(map);
    670676    }
Note: See TracChangeset for help on using the changeset viewer.