--- old/src/java.base/share/classes/sun/util/locale/provider/LocaleProviderAdapter.java 2020-05-21 11:12:48.000000000 -0700 +++ new/src/java.base/share/classes/sun/util/locale/provider/LocaleProviderAdapter.java 2020-05-21 11:12:47.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2012, 2020, 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 @@ -25,6 +25,7 @@ package sun.util.locale.provider; +import java.lang.reflect.InvocationTargetException; import java.text.spi.BreakIteratorProvider; import java.text.spi.CollatorProvider; import java.text.spi.DateFormatProvider; @@ -37,6 +38,7 @@ import java.util.Locale; import java.util.Map; import java.util.ResourceBundle; +import java.util.ServiceConfigurationError; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; @@ -50,6 +52,8 @@ import sun.text.spi.JavaTimeDateTimePatternProvider; import sun.util.spi.CalendarProvider; +import static java.lang.System.*; + /** * The LocaleProviderAdapter abstract class. * @@ -60,7 +64,7 @@ /** * Adapter type. */ - public static enum Type { + public enum Type { JRE("sun.util.locale.provider.JRELocaleProviderAdapter", "sun.util.resources", "sun.text.resources"), CLDR("sun.util.cldr.CLDRLocaleProviderAdapter", "sun.util.resources.cldr", "sun.text.resources.cldr"), SPI("sun.util.locale.provider.SPILocaleProviderAdapter"), @@ -71,11 +75,11 @@ private final String UTIL_RESOURCES_PACKAGE; private final String TEXT_RESOURCES_PACKAGE; - private Type(String className) { + Type(String className) { this(className, null, null); } - private Type(String className, String util, String text) { + Type(String className, String util, String text) { CLASSNAME = className; UTIL_RESOURCES_PACKAGE = util; TEXT_RESOURCES_PACKAGE = text; @@ -113,12 +117,13 @@ /** * Adapter lookup cache. */ - private static ConcurrentMap, ConcurrentMap> + private static final ConcurrentMap, ConcurrentMap> adapterCache = new ConcurrentHashMap<>(); static { String order = GetPropertyAction.privilegedGetProperty("java.locale.providers"); - List typeList = new ArrayList<>(); + ArrayList typeList = new ArrayList<>(); + String invalidTypeMessage = null; // Check user specified adapter preference if (order != null && !order.isEmpty()) { @@ -133,10 +138,9 @@ if (!typeList.contains(aType)) { typeList.add(aType); } - } catch (IllegalArgumentException | UnsupportedOperationException e) { - // could be caused by the user specifying wrong - // provider name or format in the system property - LocaleServiceProviderPool.config(LocaleProviderAdapter.class, e.toString()); + } catch (IllegalArgumentException e) { + // construct a log message. + invalidTypeMessage = "Invalid locale provider adapter \"" + type + "\" ignored."; } } } @@ -144,7 +148,7 @@ defaultLocaleProviderAdapter = Type.CLDR; if (!typeList.isEmpty()) { // bona fide preference exists - if (!(typeList.contains(Type.CLDR) || (typeList.contains(Type.JRE)))) { + if (!(typeList.contains(Type.CLDR) || typeList.contains(Type.JRE))) { // Append FALLBACK as the last resort when no ResourceBundleBasedAdapter is available. typeList.add(Type.FALLBACK); defaultLocaleProviderAdapter = Type.FALLBACK; @@ -155,6 +159,15 @@ typeList.add(Type.JRE); } adapterPreference = Collections.unmodifiableList(typeList); + + // Emit logs, if any, after 'adapterPreference' is initialized which is needed + // for logging. + if (invalidTypeMessage != null) { + // could be caused by the user specifying wrong + // provider name or format in the system property + getLogger(LocaleProviderAdapter.class.getCanonicalName()) + .log(Logger.Level.INFO, invalidTypeMessage); + } } /** @@ -167,30 +180,25 @@ case SPI: case HOST: case FALLBACK: - LocaleProviderAdapter adapter = null; - LocaleProviderAdapter cached = adapterInstances.get(type); - if (cached == null) { + LocaleProviderAdapter adapter = adapterInstances.get(type); + if (adapter == null) { try { // lazily load adapters here - @SuppressWarnings("deprecation") - Object tmp = Class.forName(type.getAdapterClassName()).newInstance(); - adapter = (LocaleProviderAdapter)tmp; - cached = adapterInstances.putIfAbsent(type, adapter); + adapter = (LocaleProviderAdapter)Class.forName(type.getAdapterClassName()) + .getDeclaredConstructor().newInstance(); + LocaleProviderAdapter cached = adapterInstances.putIfAbsent(type, adapter); if (cached != null) { adapter = cached; } - } catch (ClassNotFoundException | + } catch (NoSuchMethodException | + InvocationTargetException | + ClassNotFoundException | IllegalAccessException | InstantiationException | UnsupportedOperationException e) { - LocaleServiceProviderPool.config(LocaleProviderAdapter.class, e.toString()); - adapterInstances.putIfAbsent(type, NONEXISTENT_ADAPTER); - if (defaultLocaleProviderAdapter == type) { - defaultLocaleProviderAdapter = Type.FALLBACK; - } + throw new ServiceConfigurationError("Locale provider adapter \"" + + type + "\"cannot be instantiated.", e); } - } else if (cached != NONEXISTENT_ADAPTER) { - adapter = cached; } return adapter; default: @@ -440,14 +448,4 @@ public abstract LocaleResources getLocaleResources(Locale locale); public abstract Locale[] getAvailableLocales(); - - private static final LocaleProviderAdapter NONEXISTENT_ADAPTER = new NonExistentAdapter(); - private static final class NonExistentAdapter extends FallbackLocaleProviderAdapter { - @Override - public LocaleProviderAdapter.Type getAdapterType() { - return null; - } - - private NonExistentAdapter() {}; - } }