Skip to content

Commit

Permalink
8295232: "java.locale.useOldISOCodes" property is read lazily
Browse files Browse the repository at this point in the history
Reviewed-by: lancea, bpb, iris, rriggs, coffeys
  • Loading branch information
naotoj committed Oct 13, 2022
1 parent 3b34387 commit 4224d45
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/java.base/share/classes/jdk/internal/util/StaticProperty.java
Expand Up @@ -55,6 +55,7 @@ public final class StaticProperty {
private static final String JAVA_PROPERTIES_DATE;
private static final String SUN_JNU_ENCODING;
private static final Charset jnuCharset;
private static final String JAVA_LOCALE_USE_OLD_ISO_CODES;

private StaticProperty() {}

Expand All @@ -74,6 +75,7 @@ private StaticProperty() {}
JAVA_PROPERTIES_DATE = getProperty(props, "java.properties.date", null);
SUN_JNU_ENCODING = getProperty(props, "sun.jnu.encoding");
jnuCharset = Charset.forName(SUN_JNU_ENCODING, Charset.defaultCharset());
JAVA_LOCALE_USE_OLD_ISO_CODES = getProperty(props, "java.locale.useOldISOCodes", "");
}

private static String getProperty(Properties props, String key) {
Expand Down Expand Up @@ -243,4 +245,15 @@ public static String jnuEncoding() {
public static Charset jnuCharset() {
return jnuCharset;
}

/**
* {@return the {@code java.locale.useOldISOCodes} system property}
*
* <strong>{@link SecurityManager#checkPropertyAccess} is NOT checked
* in this method. The caller of this method should take care to ensure
* that the returned property is not made accessible to untrusted code.</strong>
*/
public static String javaLocaleUseOldISOCodes() {
return JAVA_LOCALE_USE_OLD_ISO_CODES;
}
}
7 changes: 4 additions & 3 deletions src/java.base/share/classes/sun/util/locale/BaseLocale.java
Expand Up @@ -33,8 +33,8 @@
package sun.util.locale;

import jdk.internal.misc.CDS;
import jdk.internal.util.StaticProperty;
import jdk.internal.vm.annotation.Stable;
import sun.security.action.GetPropertyAction;

import java.lang.ref.SoftReference;
import java.util.StringJoiner;
Expand Down Expand Up @@ -101,9 +101,10 @@ public final class BaseLocale {

/**
* Boolean for the old ISO language code compatibility.
* The system property "java.locale.useOldISOCodes" is not security sensitive,
* so no need to ensure privileged access here.
*/
private static final boolean OLD_ISO_CODES = GetPropertyAction.privilegedGetProperties()
.getProperty("java.locale.useOldISOCodes", "false")
private static final boolean OLD_ISO_CODES = StaticProperty.javaLocaleUseOldISOCodes()
.equalsIgnoreCase("true");

// This method must be called with normalize = false only when creating the
Expand Down
53 changes: 53 additions & 0 deletions test/jdk/java/util/Locale/UseOldISOCodesTest.java
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 8295232
* @summary Ensures java.locale.useOldISOCodes is statically initialized
* @library /test/lib
* @run main UseOldISOCodesTest
*/
import java.util.Locale;
import jdk.test.lib.process.ProcessTools;

public class UseOldISOCodesTest {
public static void main(String[] args) throws Exception {
ProcessTools.executeTestJvm("-Djava.locale.useOldISOCodes=true", "UseOldISOCodesTest$Runner")
.outputTo(System.out)
.errorTo(System.err)
.shouldHaveExitValue(0);
}

static class Runner {
private static final String obsoleteCode = "iw";
private static final String newCode = "he";

public static void main(String[] args) {
System.setProperty("java.locale.useOldISOCodes", "false");
Locale locale = Locale.of(newCode);
if(!obsoleteCode.equals(locale.getLanguage())){
throw new RuntimeException("Expected that newcode mapped to old ");
}
}
}
}

0 comments on commit 4224d45

Please sign in to comment.