RFR: 8312630: java/security should not create unmodifiable collections with redundant wrapping
Xue-Lei Andrew Fan
xuelei at openjdk.org
Wed Jul 26 19:35:49 UTC 2023
On Wed, 26 Jul 2023 06:11:56 GMT, Xue-Lei Andrew Fan <xuelei at openjdk.org> wrote:
>> Some java/security classes apply the below coding style,
>>
>> Set<T> set = ...;
>> Set<T> unmodifiableSet = Collections.unmodifiableSet(new HashSet<>(set));
>>
>> It may be unnecessary to wrap that `set` with HashSet before creating `unmodifiableSet`.
>> Some usages on `Collections.unmodifiableList` and `Collections.unmodifiableMap` have the same issue.
>
> Note: Please don't backport this update unless [JDK-6323374](https://bugs.openjdk.org/browse/JDK-6323374) is backport as well.
> @XueleiFan Thanks for your review and more infos!
>
> I just dug a bit history and found [JDK-8258514](https://bugs.openjdk.org/browse/JDK-8258514) applied `List::copyOf` instead of `Collections::unmodifiableList`. Now, I also follow this way and use `List/Set/Map::copyOf`.
The use of copyOf for the update is mainly because the Collections::unmodifiableList was not updated yet at that time. It could be simpler to keep the behavior consistent by using Collections::unmodifiableList in this update. Otherwise, more effect may be required to check if the use of copyOf could change the behaviors.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/15008#issuecomment-1652370187
More information about the security-dev
mailing list