RFR: 8357281: sun.util.Locale.LanguageTag should be immutable

Chen Liang liach at openjdk.org
Wed May 21 22:33:55 UTC 2025


On Wed, 21 May 2025 21:19:36 GMT, Justin Lu <jlu at openjdk.org> wrote:

> _sun.util.Locale.LanguageTag_ is essentially a BCP47 language tag data carrier for Locale. The class, once created is not modified; the class should be made immutable. Converting the class to a record accomplishes this and also simplifies some of the existing code.

The record migration is sensible: this class is purely used as a data carrier. Its object methods of hashCode and equals were unused so we can migrate safely, and the public constructor is okay too.

One comment is about the `List<String>` returning methods: currently, you are doing:

return list.isEmpty() ? list : Collections.unmodifiableList(list);

This code is error prone: if the list becomes immutable, the wrapper is redundant; if the list becomes mutable, the empty list can have new items added, and is not safe.

Since this is only accessed by trusted code (and we don't do dangerous thingss like `contains(null)`, we have much freedom in refactoring - we might just use the raw array list because users don't change them, and add alerts on record header about the mutability. Or wrap those lists with unmodifiableList upon parsing, or copy with List.copyOf, etc.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/25371#issuecomment-2899412146


More information about the core-libs-dev mailing list