Skip to content

Commit

Permalink
Reflects review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
naotoj committed Dec 6, 2023
1 parent 09b33a7 commit 3539610
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 142 deletions.
32 changes: 11 additions & 21 deletions src/java.base/share/classes/java/util/Locale.java
Original file line number Diff line number Diff line change
Expand Up @@ -1052,16 +1052,11 @@ private static synchronized Locale getFormatLocale() {
return loc;
}

@SuppressWarnings("removal")
private static Locale initDefault() {
String language, region, script, country, variant;
var sm = System.getSecurityManager();
if (sm != null) {
sm.checkPropertiesAccess();
}
language = StaticProperty.userLanguage(0);
language = StaticProperty.USER_LANGUAGE;
// for compatibility, check for old user.region property
region = StaticProperty.userRegion();
region = StaticProperty.USER_REGION;
if (!region.isEmpty()) {
// region can be of form country, country_variant, or _variant
int i = region.indexOf('_');
Expand All @@ -1074,29 +1069,24 @@ private static Locale initDefault() {
}
script = "";
} else {
script = StaticProperty.userScript(0);
country = StaticProperty.userCountry(0);
variant = StaticProperty.userVariant(0);
script = StaticProperty.USER_SCRIPT;
country = StaticProperty.USER_COUNTRY;
variant = StaticProperty.USER_VARIANT;
}

return getInstance(language, script, country, variant,
getDefaultExtensions(StaticProperty.userExtensions(0))
getDefaultExtensions(StaticProperty.USER_EXTENSIONS)
.orElse(null));
}

@SuppressWarnings("removal")
private static Locale initDefault(Locale.Category category) {
Locale locale = Locale.defaultLocale;
var sm = System.getSecurityManager();
if (sm != null) {
sm.checkPropertiesAccess();
}
return getInstance(
StaticProperty.userLanguage(category.ordinal() + 1),
StaticProperty.userScript(category.ordinal() + 1),
StaticProperty.userCountry(category.ordinal() + 1),
StaticProperty.userVariant(category.ordinal() + 1),
getDefaultExtensions(StaticProperty.userExtensions(category.ordinal() + 1))
category == Category.DISPLAY ? StaticProperty.USER_LANGUAGE_DISPLAY : StaticProperty.USER_LANGUAGE_FORMAT,
category == Category.DISPLAY ? StaticProperty.USER_SCRIPT_DISPLAY : StaticProperty.USER_SCRIPT_FORMAT,
category == Category.DISPLAY ? StaticProperty.USER_COUNTRY_DISPLAY : StaticProperty.USER_COUNTRY_FORMAT,
category == Category.DISPLAY ? StaticProperty.USER_VARIANT_DISPLAY : StaticProperty.USER_VARIANT_FORMAT,
getDefaultExtensions(category == Category.DISPLAY ? StaticProperty.USER_EXTENSIONS_DISPLAY : StaticProperty.USER_EXTENSIONS_FORMAT)
.orElse(locale.getLocaleExtensions()));
}

Expand Down
137 changes: 16 additions & 121 deletions src/java.base/share/classes/jdk/internal/util/StaticProperty.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,22 @@ public final class StaticProperty {
private static final String OS_NAME;
private static final String OS_ARCH;
private static final String OS_VERSION;
private static final String USER_LANGUAGE;
private static final String USER_LANGUAGE_DISPLAY;
private static final String USER_LANGUAGE_FORMAT;
private static final String USER_SCRIPT;
private static final String USER_SCRIPT_DISPLAY;
private static final String USER_SCRIPT_FORMAT;
private static final String USER_COUNTRY;
private static final String USER_COUNTRY_DISPLAY;
private static final String USER_COUNTRY_FORMAT;
private static final String USER_VARIANT;
private static final String USER_VARIANT_DISPLAY;
private static final String USER_VARIANT_FORMAT;
private static final String USER_EXTENSIONS;
private static final String USER_EXTENSIONS_DISPLAY;
private static final String USER_EXTENSIONS_FORMAT;
private static final String USER_REGION;
public static final String USER_LANGUAGE;
public static final String USER_LANGUAGE_DISPLAY;
public static final String USER_LANGUAGE_FORMAT;
public static final String USER_SCRIPT;
public static final String USER_SCRIPT_DISPLAY;
public static final String USER_SCRIPT_FORMAT;
public static final String USER_COUNTRY;
public static final String USER_COUNTRY_DISPLAY;
public static final String USER_COUNTRY_FORMAT;
public static final String USER_VARIANT;
public static final String USER_VARIANT_DISPLAY;
public static final String USER_VARIANT_FORMAT;
public static final String USER_EXTENSIONS;
public static final String USER_EXTENSIONS_DISPLAY;
public static final String USER_EXTENSIONS_FORMAT;
public static final String USER_REGION;

private StaticProperty() {}

Expand Down Expand Up @@ -308,109 +308,4 @@ public static String osArch() {
public static String osVersion() {
return OS_VERSION;
}

/**
* {@return the {@code user.language} 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>
*
* @param category locale category. 0 for the base property,
* {@code Locale.Category.ordinal() + 1} for the category
* specific property
*/
public static String userLanguage(int category) {
return switch (category) {
case 0 -> USER_LANGUAGE;
case 1 -> USER_LANGUAGE_DISPLAY;
case 2 -> USER_LANGUAGE_FORMAT;
default -> throw new InternalError();
};
}

/**
* {@return the {@code user.script} 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>
*
* @param category locale category. 0 for the base property,
* {@code Locale.Category.ordinal() + 1} for the category
* specific property
*/
public static String userScript(int category) {
return switch (category) {
case 0 -> USER_SCRIPT;
case 1 -> USER_SCRIPT_DISPLAY;
case 2 -> USER_SCRIPT_FORMAT;
default -> throw new InternalError();
};
}

/**
* {@return the {@code user.country} 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>
*
* @param category locale category. 0 for the base property,
* {@code Locale.Category.ordinal() + 1} for the category
* specific property
*/
public static String userCountry(int category) {
return switch (category) {
case 0 -> USER_COUNTRY;
case 1 -> USER_COUNTRY_DISPLAY;
case 2 -> USER_COUNTRY_FORMAT;
default -> throw new InternalError();
};
}

/**
* {@return the {@code user.variant} 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>
*
* @param category locale category. 0 for the base property,
* {@code Locale.Category.ordinal() + 1} for the category
* specific property
*/
public static String userVariant(int category) {
return switch (category) {
case 0 -> USER_VARIANT;
case 1 -> USER_VARIANT_DISPLAY;
case 2 -> USER_VARIANT_FORMAT;
default -> throw new InternalError();
};
}

/**
* {@return the {@code user.extensions} 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>
*
* @param category locale category. 0 for the base property,
* {@code Locale.Category.ordinal() + 1} for the category
* specific property
*/
public static String userExtensions(int category) {
return switch (category) {
case 0 -> USER_EXTENSIONS;
case 1 -> USER_EXTENSIONS_DISPLAY;
case 2 -> USER_EXTENSIONS_FORMAT;
default -> throw new InternalError();
};
}

/**
* {@return the {@code user.region} 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 userRegion() {
return USER_REGION;
}
}

0 comments on commit 3539610

Please sign in to comment.