RFR: 8282819: Deprecate Locale class constructors

Stuart Marks smarks at openjdk.java.net
Fri Mar 25 00:25:47 UTC 2022


On Thu, 24 Mar 2022 22:01:30 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.

Specs looks good, with minor modifications. I'm not so sure about replacement of the constructors with string concatenation with ternary operators; see my other comment.

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

> 250:  * <p>The {@code Locale} class provides a number of convenient constants
> 251:  * that you can use to obtain {@code Locale} objects for commonly used
> 252:  * locales. For example, the following obtains a {@code Locale} object

I'd replace this part (and the example below) with something like,

    For example, {@code Locale.US} is the {@code Locale} object for the United States.

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

> 2509:      * constructors, the {@code Builder} checks if a value configured by a
> 2510:      * setter satisfies the syntax requirements defined by the {@code Locale}
> 2511:      * class.  A {@code Locale} object obtained by a {@code Builder} is

...obtained from a Builder...

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

> 2524:      *
> 2525:      * <p>The following example shows how to obtain a {@code Locale} object
> 2526:      * with the {@code Builder}.

...shows how to obtain a Locale object using a Builder.

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

> 2658:          *
> 2659:          * <p>The country value in the {@code Locale} obtained by the
> 2660:          * {@code Builder} is always normalized to upper case.

...obtained from a Builder...

src/java.base/share/classes/sun/util/locale/provider/LocaleServiceProviderPool.java line 375:

> 373:                         (locale.getLanguage().isEmpty() ? "und" : locale.getLanguage()) +
> 374:                         (locale.getCountry().isEmpty() ? "" : "-" + locale.getCountry()) +
> 375:                         (locale.getVariant().isEmpty() ? "" : "-x-lvariant-" + locale.getVariant()));

It seems like this snippet (and ones very similar to it) are repeated several times throughout the JDK code as replacements for the two- and three-arg constructors. This seems like a fair increase in complexity, and the use of "und" and "-x-lvariant-" are quite non-obvious. Would we recommend that third party code that uses the Locale constructors replace them with this snippet? Is there something better that we can provide?

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

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



More information about the security-dev mailing list