On Wed, 1 Jul 2026 16:56:19 GMT, Justin Lu <[email protected]> wrote:
>> This PR corrects the special case handling in `Locale.Builder.setLocale`.
>> The current handling always assumes a `Locale` matching the `ja/JP/JP` and
>> `th/TH/TH` special cases will contain the correct compatibility extensions.
>> A user can remove the extension or create a Locale in the form of the
>> special case without the correct compatibility extension.
>>
>> For example, the current implementation does the following,
>>
>>> jshell> loc = new Builder().setLocale(Locale.of("ja", "JP",
>>> "JP").stripExtensions()).build()
>>> loc ==> ja_JP
>>>
>>> jshell> loc = new
>>> Builder().setLocale(Locale.forLanguageTag("ja-JP-u-ca-foobar-x-lvariant-JP")).build()
>>> loc ==> ja_JP_#u-ca-foobar
>>
>> That is, it silently discards the ill-formed JP variant.
>>
>> With assertions on, this instead becomes a `NullPointerException` when the
>> extensions are not present, or an `AssertionError` when the extension has
>> the wrong value.
>>
>> For the example above, one would expect an `IllFormedLocaleException` to be
>> thrown, since without a correct compatibility extension, JP is merely an
>> invalid variant and `setLocale(Locale)` throws on ill-formed fields.
>>
>> The changes in this PR ensure that these cases throw as expected.
>>
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> Justin Lu has updated the pull request incrementally with one additional
> commit since the last revision:
>
> Review - add empty script checking for no_NO_NY case as well
Adding the `script.isEmpty()` check for all three cases looks good. I also
noticed a couple of pre-existing typos.
src/java.base/share/classes/sun/util/locale/InternalLocaleBuilder.java line 405:
> 403: else if (language.equals("no") && region.equals("NO") &&
> variant.equals("NY")) {
> 404: // no_NO_NY is a valid locale and used by Java 6 or
> older versions.
> 405: // The build ignores the variant "NY" and change the
> language to "nn".
Nit: typo: "build" -> "builder", "change" -> "changes"
-------------
PR Review: https://git.openjdk.org/jdk/pull/31728#pullrequestreview-4611787664
PR Review Comment: https://git.openjdk.org/jdk/pull/31728#discussion_r3508104580