<i18n dev> RFR: 8282819: Deprecate Locale class constructors [v3]

ExE Boss duke at openjdk.java.net
Sun Mar 27 08:51:41 UTC 2022


On Fri, 25 Mar 2022 22:51:23 GMT, Naoto Sato <naoto at openjdk.org> wrote:

>> Proposing to deprecate the constructors in the `java.util.Locale` class. There is already a factory method and a builder to return singletons, so there is no need to have constructors anymore unless one purposefully wants to create `ill-formed` Locale objects, which is discouraged. We cannot terminally deprecate those constructors for the compatibility to serialized objects created with older JDKs. Please see the draft CSR for more detail.
>
> Naoto Sato has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Fixed a build failure

src/java.base/share/classes/java/util/Locale.java line 819:

> 817:      * @since 19
> 818:      */
> 819:     public static Locale of(String... fields) {

Arguably, there should be `Locale.of` overloads taking 0 to 4 arguments, so that it’s not necessary to box the fields in a `String` array.

src/java.base/share/classes/java/util/Locale.java line 825:

> 823:             case 2 -> getInstance(fields[0], "", fields[1], "", null);
> 824:             case 3 -> getInstance(fields[0], "", fields[1], fields[2], null);
> 825:             default -> getInstance(fields[0], fields[3], fields[1], fields[2], null);

This should probably throw `IllegalArgumentException` when more than 4 fields are passed:
Suggestion:

            case 4 -> getInstance(fields[0], fields[3], fields[1], fields[2], null);
            default -> throw new IllegalArgumentException(/* TODO: message */);

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

PR: https://git.openjdk.java.net/jdk/pull/7947


More information about the i18n-dev mailing list