src/java.base/share/classes/java/util/ResourceBundle.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58288 48e480e56aad
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
     2
 * Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The original version of this source code and documentation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * is copyrighted and owned by Taligent, Inc., a wholly-owned
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * subsidiary of IBM. These materials are provided under terms
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * of a License Agreement between Taligent and Sun. This technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * is protected by multiple US and International patents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * This notice and attribution to Taligent may not be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Taligent is a registered trademark of Taligent, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
package java.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.io.InputStream;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
    45
import java.io.UncheckedIOException;
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
    46
import java.lang.ref.Reference;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.lang.ref.ReferenceQueue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.lang.ref.SoftReference;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.lang.ref.WeakReference;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
    50
import java.lang.reflect.Constructor;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
    51
import java.lang.reflect.InvocationTargetException;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
    52
import java.lang.reflect.Modifier;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import java.net.JarURLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.net.URL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
import java.net.URLConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import java.security.PrivilegedActionException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
import java.security.PrivilegedExceptionAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import java.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import java.util.concurrent.ConcurrentMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import java.util.jar.JarEntry;
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
    63
import java.util.spi.ResourceBundleControlProvider;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
    64
import java.util.spi.ResourceBundleProvider;
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
    65
import java.util.stream.Collectors;
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
    66
import java.util.stream.Stream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
    68
import jdk.internal.loader.BootLoader;
52427
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 49433
diff changeset
    69
import jdk.internal.access.JavaUtilResourceBundleAccess;
3c6aa484536c 8211122: Reduce the number of internal classes made accessible to jdk.unsupported
mchung
parents: 49433
diff changeset
    70
import jdk.internal.access.SharedSecrets;
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    71
import jdk.internal.reflect.CallerSensitive;
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 36511
diff changeset
    72
import jdk.internal.reflect.Reflection;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
    73
import sun.security.action.GetPropertyAction;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    74
import sun.util.locale.BaseLocale;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    75
import sun.util.locale.LocaleObjectCache;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
    76
import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    77
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    81
 * Resource bundles contain locale-specific objects.  When your program needs a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    82
 * locale-specific resource, a {@code String} for example, your program can
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    83
 * load it from the resource bundle that is appropriate for the current user's
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    84
 * locale. In this way, you can write program code that is largely independent
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
    85
 * of the user's locale isolating most, if not all, of the locale-specific
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 * information in resource bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * This allows you to write programs that can:
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
    90
 * <UL>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * <LI> be easily localized, or translated, into different languages
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * <LI> handle multiple locales at once
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <LI> be easily modified later to support even more locales
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * </UL>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * Resource bundles belong to families whose members share a common base
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * name, but whose names also have additional components that identify
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * their locales. For example, the base name of a family of resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * bundles might be "MyResources". The family should have a default
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * resource bundle which simply has the same name as its family -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 * "MyResources" - and will be used as the bundle of last resort if a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * specific locale is not supported. The family can then provide as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * many locale-specific members as needed, for example a German one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * named "MyResources_de".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * Each resource bundle in a family contains the same items, but the items have
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * been translated for the locale represented by that resource bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 * For example, both "MyResources" and "MyResources_de" may have a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   111
 * {@code String} that's used on a button for canceling operations.
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   112
 * In "MyResources" the {@code String} may contain "Cancel" and in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * "MyResources_de" it may contain "Abbrechen".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * If there are different resources for different countries, you
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 * can make specializations: for example, "MyResources_de_CH" contains objects for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 * the German language (de) in Switzerland (CH). If you want to only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * modify some of the resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * in the specialization, you can do so.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
 * When your program needs a locale-specific object, it loads
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   124
 * the {@code ResourceBundle} class using the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * {@link #getBundle(java.lang.String, java.util.Locale) getBundle}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * method:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * ResourceBundle myResources =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *      ResourceBundle.getBundle("MyResources", currentLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * Resource bundles contain key/value pairs. The keys uniquely
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * identify a locale-specific object in the bundle. Here's an
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   137
 * example of a {@code ListResourceBundle} that contains
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 * two key/value pairs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * public class MyResources extends ListResourceBundle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *     protected Object[][] getContents() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *         return new Object[][] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *             // LOCALIZE THE SECOND STRING OF EACH ARRAY (e.g., "OK")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 *             {"OkKey", "OK"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *             {"CancelKey", "Cancel"},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 *             // END OF MATERIAL TO LOCALIZE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 *        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 * </blockquote>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   153
 * Keys are always {@code String}s.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 * In this example, the keys are "OkKey" and "CancelKey".
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 * In the above example, the values
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   156
 * are also {@code String}s--"OK" and "Cancel"--but
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 * they don't have to be. The values can be any type of object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
 * <P>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
 * You retrieve an object from resource bundle using the appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 * getter method. Because "OkKey" and "CancelKey"
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   162
 * are both strings, you would use {@code getString} to retrieve them:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 * button1 = new Button(myResources.getString("OkKey"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
 * button2 = new Button(myResources.getString("CancelKey"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 * The getter methods all require the key as an argument and return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 * the object if found. If the object is not found, the getter method
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   171
 * throws a {@code MissingResourceException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 * <P>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   174
 * Besides {@code getString}, {@code ResourceBundle} also provides
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   175
 * a method for getting string arrays, {@code getStringArray},
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   176
 * as well as a generic {@code getObject} method for any other
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   177
 * type of object. When using {@code getObject}, you'll
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * have to cast the result to the appropriate type. For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 * int[] myIntegers = (int[]) myResources.getObject("intList");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
 * <P>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   186
 * The Java Platform provides two subclasses of {@code ResourceBundle},
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   187
 * {@code ListResourceBundle} and {@code PropertyResourceBundle},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 * that provide a fairly simple way to create resources.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   189
 * As you saw briefly in a previous example, {@code ListResourceBundle}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 * manages its resource as a list of key/value pairs.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   191
 * {@code PropertyResourceBundle} uses a properties file to manage
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 * its resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
 * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   195
 * If {@code ListResourceBundle} or {@code PropertyResourceBundle}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   196
 * do not suit your needs, you can write your own {@code ResourceBundle}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   197
 * subclass.  Your subclasses must override two methods: {@code handleGetObject}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   198
 * and {@code getKeys()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
 *
21949
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 21428
diff changeset
   200
 * <p>
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 21428
diff changeset
   201
 * The implementation of a {@code ResourceBundle} subclass must be thread-safe
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 21428
diff changeset
   202
 * if it's simultaneously used by multiple threads. The default implementations
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 21428
diff changeset
   203
 * of the non-abstract methods in this class, and the methods in the direct
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 21428
diff changeset
   204
 * known concrete subclasses {@code ListResourceBundle} and
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 21428
diff changeset
   205
 * {@code PropertyResourceBundle} are thread-safe.
d97fd8123cbe 8028368: There is no description whether or not java.util.ResourceBundle is thread-safe
naoto
parents: 21428
diff changeset
   206
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   207
 * <h2><a id="resource-bundle-modules">Resource Bundles and Named Modules</a></h2>
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   208
 *
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   209
 * Resource bundles can be deployed in modules in the following ways:
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   210
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   211
 * <h3>Resource bundles together with an application</h3>
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   212
 *
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   213
 * Resource bundles can be deployed together with an application in the same
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   214
 * module.  In that case, the resource bundles are loaded
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   215
 * by code in the module by calling the {@link #getBundle(String)}
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   216
 * or {@link #getBundle(String, Locale)} method.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   217
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   218
 * <h3><a id="service-providers">Resource bundles as service providers</a></h3>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   219
 *
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   220
 * Resource bundles can be deployed in one or more <em>service provider modules</em>
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   221
 * and they can be located using {@link ServiceLoader}.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   222
 * A {@linkplain ResourceBundleProvider service} interface or class must be
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   223
 * defined. The caller module declares that it uses the service, the service
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   224
 * provider modules declare that they provide implementations of the service.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   225
 * Refer to {@link ResourceBundleProvider} for developing resource bundle
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   226
 * services and deploying resource bundle providers.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   227
 * The module obtaining the resource bundle can be a resource bundle
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   228
 * provider itself; in which case this module only locates the resource bundle
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   229
 * via service provider mechanism.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   230
 *
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   231
 * <p>A {@linkplain ResourceBundleProvider resource bundle provider} can
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   232
 * provide resource bundles in any format such XML which replaces the need
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   233
 * of {@link Control ResourceBundle.Control}.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   234
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   235
 * <h3><a id="other-modules">Resource bundles in other modules and class path</a></h3>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   236
 *
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   237
 * Resource bundles in a named module may be <em>encapsulated</em> so that
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   238
 * it cannot be located by code in other modules.  Resource bundles
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   239
 * in unnamed modules and class path are open for any module to access.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   240
 * Resource bundle follows the resource encapsulation rules as specified
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   241
 * in {@link Module#getResourceAsStream(String)}.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   242
 *
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   243
 * <p>The {@code getBundle} factory methods with no {@code Control} parameter
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   244
 * locate and load resource bundles from
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   245
 * {@linkplain ResourceBundleProvider service providers}.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   246
 * It may continue the search as if calling {@link Module#getResourceAsStream(String)}
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   247
 * to find the named resource from a given module and calling
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   248
 * {@link ClassLoader#getResourceAsStream(String)}; refer to
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   249
 * the specification of the {@code getBundle} method for details.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   250
 * Only non-encapsulated resource bundles of "{@code java.class}"
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   251
 * or "{@code java.properties}" format are searched.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   252
 *
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   253
 * <p>If the caller module is a
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48591
diff changeset
   254
 * <a href="{@docRoot}/java.base/java/util/spi/ResourceBundleProvider.html#obtain-resource-bundle">
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   255
 * resource bundle provider</a>, it does not fall back to the
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   256
 * class loader search.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   257
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   258
 * <h3>Resource bundles in automatic modules</h3>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   259
 *
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   260
 * A common format of resource bundles is in {@linkplain PropertyResourceBundle
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   261
 * .properties} file format.  Typically {@code .properties} resource bundles
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   262
 * are packaged in a JAR file.  Resource bundle only JAR file can be readily
49433
b6671a111395 8199465: {@docRoot} references need to be updated to reflect new module/package structure
jjg
parents: 48591
diff changeset
   263
 * deployed as an <a href="{@docRoot}/java.base/java/lang/module/ModuleFinder.html#automatic-modules">
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   264
 * automatic module</a>.  For example, if the JAR file contains the
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   265
 * entry "{@code p/q/Foo_ja.properties}" and no {@code .class} entry,
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   266
 * when resolved and defined as an automatic module, no package is derived
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   267
 * for this module.  This allows resource bundles in {@code .properties}
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   268
 * format packaged in one or more JAR files that may contain entries
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   269
 * in the same directory and can be resolved successfully as
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   270
 * automatic modules.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   271
 *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
   272
 * <h3>ResourceBundle.Control</h3>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 * The {@link ResourceBundle.Control} class provides information necessary
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   275
 * to perform the bundle loading process by the {@code getBundle}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   276
 * factory methods that take a {@code ResourceBundle.Control}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 * instance. You can implement your own subclass in order to enable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 * non-standard resource bundle formats, change the search strategy, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * define caching parameters. Refer to the descriptions of the class and the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * {@link #getBundle(String, Locale, ClassLoader, Control) getBundle}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * factory method for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 *
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   283
 * <p> {@link ResourceBundle.Control} is designed for an application deployed
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   284
 * in an unnamed module, for example to support resource bundles in
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   285
 * non-standard formats or package localized resources in a non-traditional
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   286
 * convention. {@link ResourceBundleProvider} is the replacement for
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   287
 * {@code ResourceBundle.Control} when migrating to modules.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   288
 * {@code UnsupportedOperationException} will be thrown when a factory
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   289
 * method that takes the {@code ResourceBundle.Control} parameter is called.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   290
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44546
diff changeset
   291
 * <p><a id="modify_default_behavior">For the {@code getBundle} factory</a>
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   292
 * methods that take no {@link Control} instance, their <a
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   293
 * href="#default_behavior"> default behavior</a> of resource bundle loading
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   294
 * can be modified with custom {@link
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   295
 * ResourceBundleControlProvider} implementations.
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   296
 * If any of the
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   297
 * providers provides a {@link Control} for the given base name, that {@link
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   298
 * Control} will be used instead of the default {@link Control}. If there is
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   299
 * more than one service provider for supporting the same base name,
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   300
 * the first one returned from {@link ServiceLoader} will be used.
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   301
 * A custom {@link Control} implementation is ignored by named modules.
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   302
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   303
 * <h2>Cache Management</h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   305
 * Resource bundle instances created by the {@code getBundle} factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
 * methods are cached by default, and the factory methods return the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
 * resource bundle instance multiple times if it has been
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   308
 * cached. {@code getBundle} clients may clear the cache, manage the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
 * lifetime of cached resource bundle instances using time-to-live values,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
 * or specify not to cache resource bundle instances. Refer to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
 * descriptions of the {@linkplain #getBundle(String, Locale, ClassLoader,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   312
 * Control) {@code getBundle} factory method}, {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
 * #clearCache(ClassLoader) clearCache}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
 * Control#getTimeToLive(String, Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
 * ResourceBundle.Control.getTimeToLive}, and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
 * Control#needsReload(String, Locale, String, ClassLoader, ResourceBundle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
 * long) ResourceBundle.Control.needsReload} for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
 *
54206
003cc64366da 8220249: fix headings in java.compiler
jjg
parents: 53018
diff changeset
   319
 * <h2>Example</h2>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
 *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   321
 * The following is a very simple example of a {@code ResourceBundle}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   322
 * subclass, {@code MyResources}, that manages two resources (for a larger number of
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   323
 * resources you would probably use a {@code Map}).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
 * Notice that you don't need to supply a value if
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   325
 * a "parent-level" {@code ResourceBundle} handles the same
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
 * key with the same value (as for the okKey below).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
 * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
 * // default (English language, United States)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
 * public class MyResources extends ResourceBundle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
 *     public Object handleGetObject(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
 *         if (key.equals("okKey")) return "Ok";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
 *         if (key.equals("cancelKey")) return "Cancel";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
 *         return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
 *     public Enumeration&lt;String&gt; getKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
 *         return Collections.enumeration(keySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
 *     // Overrides handleKeySet() so that the getKeys() implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
 *     // can rely on the keySet() value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
 *     protected Set&lt;String&gt; handleKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
 *         return new HashSet&lt;String&gt;(Arrays.asList("okKey", "cancelKey"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
 * // German language
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
 * public class MyResources_de extends MyResources {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
 *     public Object handleGetObject(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
 *         // don't need okKey, since parent level handles it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
 *         if (key.equals("cancelKey")) return "Abbrechen";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
 *         return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
 *     protected Set&lt;String&gt; handleKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
 *         return new HashSet&lt;String&gt;(Arrays.asList("cancelKey"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
 * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
 * You do not have to restrict yourself to using a single family of
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   363
 * {@code ResourceBundle}s. For example, you could have a set of bundles for
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   364
 * exception messages, {@code ExceptionResources}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   365
 * ({@code ExceptionResources_fr}, {@code ExceptionResources_de}, ...),
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   366
 * and one for widgets, {@code WidgetResource} ({@code WidgetResources_fr},
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   367
 * {@code WidgetResources_de}, ...); breaking up the resources however you like.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
 * @see ListResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
 * @see PropertyResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
 * @see MissingResourceException
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   372
 * @see ResourceBundleProvider
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   373
 * @since 1.1
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
   374
 * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
   375
 * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
public abstract class ResourceBundle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /** initial size of the bundle cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    private static final int INITIAL_CACHE_SIZE = 32;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   382
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   383
        SharedSecrets.setJavaUtilResourceBundleAccess(
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   384
            new JavaUtilResourceBundleAccess() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   385
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   386
                public void setParent(ResourceBundle bundle,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   387
                                      ResourceBundle parent) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   388
                    bundle.setParent(parent);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   389
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   390
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   391
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   392
                public ResourceBundle getParent(ResourceBundle bundle) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   393
                    return bundle.parent;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   394
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   395
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   396
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   397
                public void setLocale(ResourceBundle bundle, Locale locale) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   398
                    bundle.locale = locale;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   399
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   400
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   401
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   402
                public void setName(ResourceBundle bundle, String name) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   403
                    bundle.name = name;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   404
                }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   405
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   406
                @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   407
                public ResourceBundle getBundle(String baseName, Locale locale, Module module) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   408
                    // use the given module as the caller to bypass the access check
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   409
                    return getBundleImpl(module, module,
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   410
                                         baseName, locale,
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   411
                                         getDefaultControl(module, baseName));
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   412
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   413
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   414
                @Override
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   415
                public ResourceBundle newResourceBundle(Class<? extends ResourceBundle> bundleClass) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   416
                    return ResourceBundleProviderHelper.newResourceBundle(bundleClass);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   417
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   418
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   419
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   420
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /** constant indicating that no resource bundle exists */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    private static final ResourceBundle NONEXISTENT_BUNDLE = new ResourceBundle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            public Enumeration<String> getKeys() { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            protected Object handleGetObject(String key) { return null; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            public String toString() { return "NONEXISTENT_BUNDLE"; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * The cache is a map from cache keys (with bundle base name, locale, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * class loader) to either a resource bundle or NONEXISTENT_BUNDLE wrapped by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * BundleReference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * The cache is a ConcurrentMap, allowing the cache to be searched
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * concurrently by multiple threads.  This will also allow the cache keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * to be reclaimed along with the ClassLoaders they reference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * This variable would be better named "cache", but we keep the old
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * name for compatibility with some workarounds for bug 4212439.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    private static final ConcurrentMap<CacheKey, BundleReference> cacheList
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
   442
        = new ConcurrentHashMap<>(INITIAL_CACHE_SIZE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Queue for reference objects referring to class loaders or bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     */
13029
dfd934f86768 6380549: (rb) ResourceBundle.Control global binding support
okutsu
parents: 12448
diff changeset
   447
    private static final ReferenceQueue<Object> referenceQueue = new ReferenceQueue<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    /**
20865
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   450
     * Returns the base name of this bundle, if known, or {@code null} if unknown.
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   451
     *
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   452
     * If not null, then this is the value of the {@code baseName} parameter
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   453
     * that was passed to the {@code ResourceBundle.getBundle(...)} method
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   454
     * when the resource bundle was loaded.
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   455
     *
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   456
     * @return The base name of the resource bundle, as provided to and expected
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   457
     * by the {@code ResourceBundle.getBundle(...)} methods.
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   458
     *
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   459
     * @see #getBundle(java.lang.String, java.util.Locale, java.lang.ClassLoader)
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   460
     *
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   461
     * @since 1.8
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   462
     */
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   463
    public String getBaseBundleName() {
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   464
        return name;
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   465
    }
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   466
164ba3f3484b 8013839: Enhance Logger API for handling of resource bundles
dfuchs
parents: 18829
diff changeset
   467
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * The parent bundle of this bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * The parent bundle is searched by {@link #getObject getObject}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * when this bundle does not contain a particular resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    protected ResourceBundle parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * The locale for this bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    private Locale locale = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * The base bundle name for this bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    private String name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * The flag indicating this bundle has expired in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    private volatile boolean expired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * The back link to the cache key. null if this bundle isn't in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * the cache (yet) or has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    private volatile CacheKey cacheKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * A Set of the keys contained only in this ResourceBundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    private volatile Set<String> keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * Sole constructor.  (For invocation by subclass constructors, typically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    public ResourceBundle() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * Gets a string for the given key from this resource bundle or one of its parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     * Calling this method is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * <code>(String) {@link #getObject(java.lang.String) getObject}(key)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     * @param key the key for the desired string
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   515
     * @throws    NullPointerException if {@code key} is {@code null}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
   516
     * @throws    MissingResourceException if no object for the given key can be found
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
   517
     * @throws    ClassCastException if the object found for the given key is not a string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * @return the string for the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public final String getString(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        return (String) getObject(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Gets a string array for the given key from this resource bundle or one of its parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * Calling this method is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * <code>(String[]) {@link #getObject(java.lang.String) getObject}(key)</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @param key the key for the desired string array
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   532
     * @throws    NullPointerException if {@code key} is {@code null}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
   533
     * @throws    MissingResourceException if no object for the given key can be found
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
   534
     * @throws    ClassCastException if the object found for the given key is not a string array
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @return the string array for the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
    public final String[] getStringArray(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        return (String[]) getObject(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Gets an object for the given key from this resource bundle or one of its parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * This method first tries to obtain the object from this resource bundle using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * {@link #handleGetObject(java.lang.String) handleGetObject}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * If not successful, and the parent resource bundle is not null,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   546
     * it calls the parent's {@code getObject} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * If still not successful, it throws a MissingResourceException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * @param key the key for the desired object
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   550
     * @throws    NullPointerException if {@code key} is {@code null}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
   551
     * @throws    MissingResourceException if no object for the given key can be found
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * @return the object for the given key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    public final Object getObject(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        Object obj = handleGetObject(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        if (obj == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            if (parent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                obj = parent.getObject(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            }
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
   560
            if (obj == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                throw new MissingResourceException("Can't find resource for bundle "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                                                   +this.getClass().getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                                                   +", key "+key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                                                   this.getClass().getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                                                   key);
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
   566
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        return obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Returns the locale of this resource bundle. This method can be used after a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * call to getBundle() to determine whether the resource bundle returned really
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * corresponds to the requested locale or is a fallback.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * @return the locale of this resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    public Locale getLocale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        return locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   582
    private static ClassLoader getLoader(Module module) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   583
        PrivilegedAction<ClassLoader> pa = module::getClassLoader;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   584
        return AccessController.doPrivileged(pa);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   585
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   586
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
    /**
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   588
     * @param module a non-null-screened module form the {@link CacheKey#getModule()}.
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   589
     * @return the ClassLoader to use in {@link Control#needsReload}
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   590
     *         and {@link Control#newBundle}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     */
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   592
    private static ClassLoader getLoaderForControl(Module module) {
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   593
        ClassLoader loader = getLoader(module);
48570
9baae459d58e 8182601: Improve usage messages
naoto
parents: 47478
diff changeset
   594
        return loader == null ? ClassLoader.getPlatformClassLoader() : loader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * Sets the parent bundle of this bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * The parent bundle is searched by {@link #getObject getObject}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * when this bundle does not contain a particular resource.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * @param parent this bundle's parent bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    protected void setParent(ResourceBundle parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        assert parent != NONEXISTENT_BUNDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        this.parent = parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Key used for cached resource bundles.  The key checks the base
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   611
     * name, the locale, the bundle module, and the caller module
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   612
     * to determine if the resource is a match to the requested one.
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   613
     * The base name, the locale and both modules must have a non-null value.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   615
    private static final class CacheKey {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   616
        // These four are the actual keys for lookup in Map.
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   617
        private final String name;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   618
        private volatile Locale locale;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   619
        private final KeyElementReference<Module> moduleRef;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   620
        private final KeyElementReference<Module> callerRef;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   621
        // this is the part of hashCode that pertains to module and callerModule
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   622
        // which can be GCed..
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   623
        private final int modulesHash;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        // bundle format which is necessary for calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        // Control.needsReload().
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   627
        private volatile String format;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        // These time values are in CacheKey so that NONEXISTENT_BUNDLE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        // doesn't need to be cloned for caching.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        // The time when the bundle has been loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        private volatile long loadTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        // The time when the bundle expires in the cache, or either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        // Control.TTL_DONT_CACHE or Control.TTL_NO_EXPIRATION_CONTROL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
        private volatile long expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        // Placeholder for an error report by a Throwable
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   640
        private volatile Throwable cause;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   642
        // ResourceBundleProviders for loading ResourceBundles
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   643
        private volatile ServiceLoader<ResourceBundleProvider> providers;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   644
        private volatile boolean providersChecked;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   645
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   646
        // Boolean.TRUE if the factory method caller provides a ResourceBundleProvier.
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   647
        private volatile Boolean callerHasProvider;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   648
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   649
        CacheKey(String baseName, Locale locale, Module module, Module caller) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   650
            Objects.requireNonNull(module);
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   651
            Objects.requireNonNull(caller);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   652
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            this.name = baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            this.locale = locale;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   655
            this.moduleRef = new KeyElementReference<>(module, referenceQueue, this);
42467
46018567e1a6 8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents: 42338
diff changeset
   656
            this.callerRef = new KeyElementReference<>(caller, referenceQueue, this);
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   657
            this.modulesHash = module.hashCode() ^ caller.hashCode();
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   658
        }
42467
46018567e1a6 8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents: 42338
diff changeset
   659
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   660
        CacheKey(CacheKey src) {
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   661
            // Create References to src's modules
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   662
            this.moduleRef = new KeyElementReference<>(
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   663
                Objects.requireNonNull(src.getModule()), referenceQueue, this);
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   664
            this.callerRef = new KeyElementReference<>(
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   665
                Objects.requireNonNull(src.getCallerModule()), referenceQueue, this);
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   666
            // Copy fields from src. ResourceBundleProviders related fields
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   667
            // and "cause" should not be copied.
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   668
            this.name = src.name;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   669
            this.locale = src.locale;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   670
            this.modulesHash = src.modulesHash;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   671
            this.format = src.format;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   672
            this.loadTime = src.loadTime;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   673
            this.expirationTime = src.expirationTime;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            return name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        Locale getLocale() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            return locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        CacheKey setLocale(Locale locale) {
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   685
            this.locale = locale;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   689
        Module getModule() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   690
            return moduleRef.get();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   691
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   692
42467
46018567e1a6 8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents: 42338
diff changeset
   693
        Module getCallerModule() {
46018567e1a6 8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents: 42338
diff changeset
   694
            return callerRef.get();
46018567e1a6 8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents: 42338
diff changeset
   695
        }
46018567e1a6 8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents: 42338
diff changeset
   696
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   697
        ServiceLoader<ResourceBundleProvider> getProviders() {
39768
8de55c86daa2 8161203: ResourceBundle.getBundle performance regression
okutsu
parents: 37363
diff changeset
   698
            if (!providersChecked) {
8de55c86daa2 8161203: ResourceBundle.getBundle performance regression
okutsu
parents: 37363
diff changeset
   699
                providers = getServiceLoader(getModule(), name);
8de55c86daa2 8161203: ResourceBundle.getBundle performance regression
okutsu
parents: 37363
diff changeset
   700
                providersChecked = true;
8de55c86daa2 8161203: ResourceBundle.getBundle performance regression
okutsu
parents: 37363
diff changeset
   701
            }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   702
            return providers;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   703
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   704
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   705
        boolean hasProviders() {
39768
8de55c86daa2 8161203: ResourceBundle.getBundle performance regression
okutsu
parents: 37363
diff changeset
   706
            return getProviders() != null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   707
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   708
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   709
        boolean callerHasProvider() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   710
            return callerHasProvider == Boolean.TRUE;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   711
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   712
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   713
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
            if (this == other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                final CacheKey otherEntry = (CacheKey)other;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                //quick check to see if they are not equal
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   721
                if (modulesHash != otherEntry.modulesHash) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                //are the names the same?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                if (!name.equals(otherEntry.name)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                // are the locales the same?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                if (!locale.equals(otherEntry.locale)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                }
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   732
                // are modules and callerModules the same and non-null?
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   733
                Module module = getModule();
42467
46018567e1a6 8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents: 42338
diff changeset
   734
                Module caller = getCallerModule();
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   735
                return ((module != null) && (module.equals(otherEntry.getModule())) &&
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   736
                        (caller != null) && (caller.equals(otherEntry.getCallerModule())));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   737
            } catch (NullPointerException | ClassCastException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   742
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        public int hashCode() {
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   744
            return (name.hashCode() << 3) ^ locale.hashCode() ^ modulesHash;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        String getFormat() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
        void setFormat(String format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            this.format = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        private void setCause(Throwable cause) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            if (this.cause == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                this.cause = cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                // Override the cause if the previous one is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                // ClassNotFoundException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                if (this.cause instanceof ClassNotFoundException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    this.cause = cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        private Throwable getCause() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            return cause;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   771
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            String l = locale.toString();
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52766
diff changeset
   774
            if (l.isEmpty()) {
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52766
diff changeset
   775
                if (!locale.getVariant().isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                    l = "__" + locale.getVariant();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                    l = "\"\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            }
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   781
            return "CacheKey[" + name +
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   782
                   ", locale=" + l +
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   783
                   ", module=" + getModule() +
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   784
                   ", callerModule=" + getCallerModule() +
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   785
                   ", format=" + format +
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
   786
                   "]";
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * The common interface to get a CacheKey in LoaderReference and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * BundleReference.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
    private static interface CacheKeyReference {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        public CacheKey getCacheKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    /**
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   799
     * References to a CacheKey element as a WeakReference so that it can be
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   800
     * garbage collected when nobody else is using it.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   802
    private static class KeyElementReference<T> extends WeakReference<T>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   803
                                                implements CacheKeyReference {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   804
        private final CacheKey cacheKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   806
        KeyElementReference(T referent, ReferenceQueue<Object> q, CacheKey key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            super(referent, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            cacheKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   811
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        public CacheKey getCacheKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            return cacheKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * References to bundles are soft references so that they can be garbage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * collected when they have no hard references.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
   821
    private static class BundleReference extends SoftReference<ResourceBundle>
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
   822
                                         implements CacheKeyReference {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   823
        private final CacheKey cacheKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
   825
        BundleReference(ResourceBundle referent, ReferenceQueue<Object> q, CacheKey key) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            super(referent, q);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
            cacheKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   830
        @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        public CacheKey getCacheKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
            return cacheKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * Gets a resource bundle using the specified base name, the default locale,
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   838
     * and the caller module. Calling this method is equivalent to calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * <blockquote>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   840
     * {@code getBundle(baseName, Locale.getDefault(), callerModule)},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @param baseName the base name of the resource bundle, a fully qualified class name
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
   844
     * @throws    java.lang.NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   845
     *     if {@code baseName} is {@code null}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
   846
     * @throws    MissingResourceException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *     if no resource bundle for the specified base name can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * @return a resource bundle for the given base name and the default locale
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   849
     *
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   850
     * @see <a href="#default_behavior">Resource Bundle Search and Loading Strategy</a>
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   851
     * @see <a href="#resource-bundle-modules">Resource Bundles and Named Modules</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 13029
diff changeset
   853
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    public static final ResourceBundle getBundle(String baseName)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   856
        Class<?> caller = Reflection.getCallerClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        return getBundleImpl(baseName, Locale.getDefault(),
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   858
                             caller, getDefaultControl(caller, baseName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * Returns a resource bundle using the specified base name, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * default locale and the specified control. Calling this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * getBundle(baseName, Locale.getDefault(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     *           this.getClass().getClassLoader(), control),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * </pre>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   869
     * except that {@code getClassLoader()} is run with the security
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   870
     * privileges of {@code ResourceBundle}.  See {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * #getBundle(String, Locale, ClassLoader, Control) getBundle} for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * complete description of the resource bundle loading process with a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   873
     * {@code ResourceBundle.Control}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *        the base name of the resource bundle, a fully qualified class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *        name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @param control
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *        the control which gives information for the resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     *        loading process
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   881
     * @return a resource bundle for the given base name and the default locale
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   882
     * @throws NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   883
     *         if {@code baseName} or {@code control} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   884
     *         {@code null}
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   885
     * @throws MissingResourceException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   886
     *         if no resource bundle for the specified base name can be found
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   887
     * @throws IllegalArgumentException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   888
     *         if the given {@code control} doesn't perform properly
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   889
     *         (e.g., {@code control.getCandidateLocales} returns null.)
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   890
     *         Note that validation of {@code control} is performed as
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   891
     *         needed.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   892
     * @throws UnsupportedOperationException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   893
     *         if this method is called in a named module
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     * @since 1.6
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
   895
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
   896
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 13029
diff changeset
   898
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    public static final ResourceBundle getBundle(String baseName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
                                                 Control control) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   901
        Class<?> caller = Reflection.getCallerClass();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   902
        Locale targetLocale = Locale.getDefault();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   903
        checkNamedModule(caller);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   904
        return getBundleImpl(baseName, targetLocale, caller, control);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     * Gets a resource bundle using the specified base name and locale,
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   909
     * and the caller module. Calling this method is equivalent to calling
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * <blockquote>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   911
     * {@code getBundle(baseName, locale, callerModule)},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     *        the base name of the resource bundle, a fully qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     *        the locale for which a resource bundle is desired
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
   918
     * @throws    NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   919
     *        if {@code baseName} or {@code locale} is {@code null}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
   920
     * @throws    MissingResourceException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *        if no resource bundle for the specified base name can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * @return a resource bundle for the given base name and locale
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   923
     *
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   924
     * @see <a href="#default_behavior">Resource Bundle Search and Loading Strategy</a>
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   925
     * @see <a href="#resource-bundle-modules">Resource Bundles and Named Modules</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 13029
diff changeset
   927
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    public static final ResourceBundle getBundle(String baseName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
                                                 Locale locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
    {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   931
        Class<?> caller = Reflection.getCallerClass();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        return getBundleImpl(baseName, locale,
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   933
                             caller, getDefaultControl(caller, baseName));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   934
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   935
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   936
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   937
     * Gets a resource bundle using the specified base name and the default locale
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   938
     * on behalf of the specified module. This method is equivalent to calling
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   939
     * <blockquote>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   940
     * {@code getBundle(baseName, Locale.getDefault(), module)}
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   941
     * </blockquote>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   942
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   943
     * @param baseName the base name of the resource bundle,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   944
     *                 a fully qualified class name
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   945
     * @param module   the module for which the resource bundle is searched
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   946
     * @throws NullPointerException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   947
     *         if {@code baseName} or {@code module} is {@code null}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   948
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   949
     *         if a security manager exists and the caller is not the specified
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   950
     *         module and doesn't have {@code RuntimePermission("getClassLoader")}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   951
     * @throws MissingResourceException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   952
     *         if no resource bundle for the specified base name can be found in the
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   953
     *         specified module
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   954
     * @return a resource bundle for the given base name and the default locale
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   955
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
   956
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   957
     * @see ResourceBundleProvider
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   958
     * @see <a href="#default_behavior">Resource Bundle Search and Loading Strategy</a>
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   959
     * @see <a href="#resource-bundle-modules">Resource Bundles and Named Modules</a>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   960
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   961
    @CallerSensitive
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   962
    public static ResourceBundle getBundle(String baseName, Module module) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   963
        return getBundleFromModule(Reflection.getCallerClass(), module, baseName,
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   964
                                   Locale.getDefault(),
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   965
                                   getDefaultControl(module, baseName));
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   966
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   967
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   968
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   969
     * Gets a resource bundle using the specified base name and locale
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   970
     * on behalf of the specified module.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   971
     *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   972
     * <p> Resource bundles in named modules may be encapsulated.  When
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   973
     * the resource bundle is loaded from a
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   974
     * {@linkplain ResourceBundleProvider service provider}, the caller module
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   975
     * must have an appropriate <i>uses</i> clause in its <i>module descriptor</i>
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   976
     * to declare that the module uses of {@link ResourceBundleProvider}
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   977
     * for the named resource bundle.
45439
5673d77a787b 8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents: 45124
diff changeset
   978
     * Otherwise, it will load the resource bundles that are local in the
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   979
     * given module as if calling {@link Module#getResourceAsStream(String)}
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   980
     * or that are visible to the class loader of the given module
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
   981
     * as if calling {@link ClassLoader#getResourceAsStream(String)}.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   982
     * When the resource bundle is loaded from the specified module, it is
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   983
     * subject to the encapsulation rules specified by
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   984
     * {@link Module#getResourceAsStream Module.getResourceAsStream}.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   985
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   986
     * <p>
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   987
     * If the given {@code module} is an unnamed module, then this method is
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   988
     * equivalent to calling {@link #getBundle(String, Locale, ClassLoader)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   989
     * getBundle(baseName, targetLocale, module.getClassLoader()} to load
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
   990
     * resource bundles that are visible to the class loader of the given
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   991
     * unnamed module. Custom {@link java.util.spi.ResourceBundleControlProvider}
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   992
     * implementations, if present, will only be invoked if the specified
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
   993
     * module is an unnamed module.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   994
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   995
     * @param baseName the base name of the resource bundle,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   996
     *                 a fully qualified class name
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   997
     * @param targetLocale the locale for which a resource bundle is desired
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   998
     * @param module   the module for which the resource bundle is searched
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
   999
     * @throws NullPointerException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1000
     *         if {@code baseName}, {@code targetLocale}, or {@code module} is
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1001
     *         {@code null}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1002
     * @throws SecurityException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1003
     *         if a security manager exists and the caller is not the specified
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1004
     *         module and doesn't have {@code RuntimePermission("getClassLoader")}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1005
     * @throws MissingResourceException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1006
     *         if no resource bundle for the specified base name and locale can
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1007
     *         be found in the specified {@code module}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1008
     * @return a resource bundle for the given base name and locale in the module
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1009
     * @since 9
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  1010
     * @spec JPMS
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1011
     * @see <a href="#default_behavior">Resource Bundle Search and Loading Strategy</a>
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1012
     * @see <a href="#resource-bundle-modules">Resource Bundles and Named Modules</a>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1013
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1014
    @CallerSensitive
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1015
    public static ResourceBundle getBundle(String baseName, Locale targetLocale, Module module) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1016
        return getBundleFromModule(Reflection.getCallerClass(), module, baseName, targetLocale,
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1017
                                   getDefaultControl(module, baseName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
     * Returns a resource bundle using the specified base name, target
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
     * locale and control, and the caller's class loader. Calling this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
     * method is equivalent to calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
     * getBundle(baseName, targetLocale, this.getClass().getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
     *           control),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
     * </pre>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1028
     * except that {@code getClassLoader()} is run with the security
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1029
     * privileges of {@code ResourceBundle}.  See {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
     * #getBundle(String, Locale, ClassLoader, Control) getBundle} for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
     * complete description of the resource bundle loading process with a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1032
     * {@code ResourceBundle.Control}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     *        the base name of the resource bundle, a fully qualified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     *        class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     * @param targetLocale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     *        the locale for which a resource bundle is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     * @param control
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
     *        the control which gives information for the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
     *        bundle loading process
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
     * @return a resource bundle for the given base name and a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1043
     *         {@code Locale} in {@code locales}
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1044
     * @throws NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1045
     *         if {@code baseName}, {@code locales} or
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1046
     *         {@code control} is {@code null}
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1047
     * @throws MissingResourceException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1048
     *         if no resource bundle for the specified base name in any
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1049
     *         of the {@code locales} can be found.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1050
     * @throws IllegalArgumentException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1051
     *         if the given {@code control} doesn't perform properly
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1052
     *         (e.g., {@code control.getCandidateLocales} returns null.)
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1053
     *         Note that validation of {@code control} is performed as
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1054
     *         needed.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1055
     * @throws UnsupportedOperationException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1056
     *         if this method is called in a named module
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
     * @since 1.6
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  1058
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  1059
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 13029
diff changeset
  1061
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    public static final ResourceBundle getBundle(String baseName, Locale targetLocale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
                                                 Control control) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1064
        Class<?> caller = Reflection.getCallerClass();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1065
        checkNamedModule(caller);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1066
        return getBundleImpl(baseName, targetLocale, caller, control);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    /**
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1070
     * Gets a resource bundle using the specified base name, locale, and class
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1071
     * loader.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1072
     *
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1073
     * <p>When this method is called from a named module and the given
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1074
     * loader is the class loader of the caller module, this is equivalent
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1075
     * to calling:
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1076
     * <blockquote><pre>
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1077
     * getBundle(baseName, targetLocale, callerModule)
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1078
     * </pre></blockquote>
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1079
     *
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1080
     * otherwise, this is equivalent to calling:
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1081
     * <blockquote><pre>
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1082
     * getBundle(baseName, targetLocale, loader, control)
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1083
     * </pre></blockquote>
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1084
     * where {@code control} is the default instance of {@link Control} unless
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1085
     * a {@code Control} instance is provided by
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1086
     * {@link ResourceBundleControlProvider} SPI.  Refer to the
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1087
     * description of <a href="#modify_default_behavior">modifying the default
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1088
     * behavior</a>. The following describes the default behavior.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1089
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1090
     * <p>
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1091
     * <b><a id="default_behavior">Resource Bundle Search and Loading Strategy</a></b>
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1092
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1093
     * <p>{@code getBundle} uses the base name, the specified locale, and
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1094
     * the default locale (obtained from {@link java.util.Locale#getDefault()
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1095
     * Locale.getDefault}) to generate a sequence of <a
44851
3439a92526a0 8179413: Fix remaining minor HTML5 issues in java.base module
jjg
parents: 44844
diff changeset
  1096
     * id="candidates"><em>candidate bundle names</em></a>.  If the specified
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1097
     * locale's language, script, country, and variant are all empty strings,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1098
     * then the base name is the only candidate bundle name.  Otherwise, a list
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1099
     * of candidate locales is generated from the attribute values of the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1100
     * specified locale (language, script, country and variant) and appended to
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1101
     * the base name.  Typically, this will look like the following:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1102
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1103
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1104
     *     baseName + "_" + language + "_" + script + "_" + country + "_" + variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1105
     *     baseName + "_" + language + "_" + script + "_" + country
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1106
     *     baseName + "_" + language + "_" + script
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1107
     *     baseName + "_" + language + "_" + country + "_" + variant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1108
     *     baseName + "_" + language + "_" + country
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1109
     *     baseName + "_" + language
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1110
     * </pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1112
     * <p>Candidate bundle names where the final component is an empty string
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1113
     * are omitted, along with the underscore.  For example, if country is an
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1114
     * empty string, the second and the fifth candidate bundle names above
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1115
     * would be omitted.  Also, if script is an empty string, the candidate names
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1116
     * including script are omitted.  For example, a locale with language "de"
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1117
     * and variant "JAVA" will produce candidate names with base name
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1118
     * "MyResource" below.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1119
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1120
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1121
     *     MyResource_de__JAVA
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1122
     *     MyResource_de
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1123
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1124
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1125
     * In the case that the variant contains one or more underscores ('_'), a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1126
     * sequence of bundle names generated by truncating the last underscore and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1127
     * the part following it is inserted after a candidate bundle name with the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1128
     * original variant.  For example, for a locale with language "en", script
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1129
     * "Latn, country "US" and variant "WINDOWS_VISTA", and bundle base name
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1130
     * "MyResource", the list of candidate bundle names below is generated:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1131
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1132
     * <pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1133
     * MyResource_en_Latn_US_WINDOWS_VISTA
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1134
     * MyResource_en_Latn_US_WINDOWS
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1135
     * MyResource_en_Latn_US
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1136
     * MyResource_en_Latn
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1137
     * MyResource_en_US_WINDOWS_VISTA
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1138
     * MyResource_en_US_WINDOWS
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1139
     * MyResource_en_US
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1140
     * MyResource_en
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1141
     * </pre>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1142
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1143
     * <blockquote><b>Note:</b> For some {@code Locale}s, the list of
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1144
     * candidate bundle names contains extra names, or the order of bundle names
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1145
     * is slightly modified.  See the description of the default implementation
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1146
     * of {@link Control#getCandidateLocales(String, Locale)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1147
     * getCandidateLocales} for details.</blockquote>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1149
     * <p>{@code getBundle} then iterates over the candidate bundle names
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1150
     * to find the first one for which it can <em>instantiate</em> an actual
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1151
     * resource bundle. It uses the default controls' {@link Control#getFormats
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1152
     * getFormats} method, which generates two bundle names for each generated
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1153
     * name, the first a class name and the second a properties file name. For
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1154
     * each candidate bundle name, it attempts to create a resource bundle:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1155
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1156
     * <ul><li>First, it attempts to load a class using the generated class name.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1157
     * If such a class can be found and loaded using the specified class
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1158
     * loader, is assignment compatible with ResourceBundle, is accessible from
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1159
     * ResourceBundle, and can be instantiated, {@code getBundle} creates a
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1160
     * new instance of this class and uses it as the <em>result resource
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1161
     * bundle</em>.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1162
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1163
     * <li>Otherwise, {@code getBundle} attempts to locate a property
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1164
     * resource file using the generated properties file name.  It generates a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1165
     * path name from the candidate bundle name by replacing all "." characters
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1166
     * with "/" and appending the string ".properties".  It attempts to find a
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1167
     * "resource" with this name using {@link
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1168
     * java.lang.ClassLoader#getResource(java.lang.String)
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1169
     * ClassLoader.getResource}.  (Note that a "resource" in the sense of
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1170
     * {@code getResource} has nothing to do with the contents of a
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1171
     * resource bundle, it is just a container of data, such as a file.)  If it
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1172
     * finds a "resource", it attempts to create a new {@link
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1173
     * PropertyResourceBundle} instance from its contents.  If successful, this
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1174
     * instance becomes the <em>result resource bundle</em>.  </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
     *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1176
     * <p>This continues until a result resource bundle is instantiated or the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1177
     * list of candidate bundle names is exhausted.  If no matching resource
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1178
     * bundle is found, the default control's {@link Control#getFallbackLocale
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1179
     * getFallbackLocale} method is called, which returns the current default
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1180
     * locale.  A new sequence of candidate locale names is generated using this
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 27083
diff changeset
  1181
     * locale and searched again, as above.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1182
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1183
     * <p>If still no result bundle is found, the base name alone is looked up. If
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1184
     * this still fails, a {@code MissingResourceException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
     *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44546
diff changeset
  1186
     * <p><a id="parent_chain"> Once a result resource bundle has been found,
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  1187
     * its <em>parent chain</em> is instantiated</a>.  If the result bundle already
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1188
     * has a parent (perhaps because it was returned from a cache) the chain is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1189
     * complete.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1190
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1191
     * <p>Otherwise, {@code getBundle} examines the remainder of the
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1192
     * candidate locale list that was used during the pass that generated the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1193
     * result resource bundle.  (As before, candidate bundle names where the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1194
     * final component is an empty string are omitted.)  When it comes to the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1195
     * end of the candidate list, it tries the plain bundle name.  With each of the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1196
     * candidate bundle names it attempts to instantiate a resource bundle (first
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1197
     * looking for a class and then a properties file, as described above).
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1198
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1199
     * <p>Whenever it succeeds, it calls the previously instantiated resource
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * bundle's {@link #setParent(java.util.ResourceBundle) setParent} method
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1201
     * with the new resource bundle.  This continues until the list of names
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1202
     * is exhausted or the current bundle already has a non-null parent.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1203
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1204
     * <p>Once the parent chain is complete, the bundle is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1206
     * <p><b>Note:</b> {@code getBundle} caches instantiated resource
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1207
     * bundles and might return the same resource bundle instance multiple times.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1209
     * <p><b>Note:</b>The {@code baseName} argument should be a fully
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1210
     * qualified class name. However, for compatibility with earlier versions,
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1211
     * Java SE Runtime Environments do not verify this, and so it is
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1212
     * possible to access {@code PropertyResourceBundle}s by specifying a
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1213
     * path name (using "/") instead of a fully qualified class name (using
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1214
     * ".").
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
     *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 44546
diff changeset
  1216
     * <p><a id="default_behavior_example">
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  1217
     * <strong>Example:</strong></a>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1218
     * <p>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1219
     * The following class and property files are provided:
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1220
     * <ul>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1221
     *     <li>MyResources.class
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1222
     *     <li>MyResources.properties
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1223
     *     <li>MyResources_fr.properties
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1224
     *     <li>MyResources_fr_CH.class
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1225
     *     <li>MyResources_fr_CH.properties
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1226
     *     <li>MyResources_en.properties
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1227
     *     <li>MyResources_es_ES.class
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1228
     * </ul>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1229
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1230
     * The contents of all files are valid (that is, public non-abstract
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1231
     * subclasses of {@code ResourceBundle} for the ".class" files,
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1232
     * syntactically correct ".properties" files).  The default locale is
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1233
     * {@code Locale("en", "GB")}.
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1234
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1235
     * <p>Calling {@code getBundle} with the locale arguments below will
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1236
     * instantiate resource bundles as follows:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1237
     *
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1238
     * <table class="striped">
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44851
diff changeset
  1239
     * <caption style="display:none">getBundle() locale to resource bundle mapping</caption>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1240
     * <thead>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1241
     * <tr><th scope="col">Locale</th><th scope="col">Resource bundle</th></tr>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1242
     * </thead>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44851
diff changeset
  1243
     * <tbody>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1244
     * <tr><th scope="row">Locale("fr", "CH")</th><td>MyResources_fr_CH.class, parent MyResources_fr.properties, parent MyResources.class</td></tr>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1245
     * <tr><th scope="row">Locale("fr", "FR")</th><td>MyResources_fr.properties, parent MyResources.class</td></tr>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1246
     * <tr><th scope="row">Locale("de", "DE")</th><td>MyResources_en.properties, parent MyResources.class</td></tr>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1247
     * <tr><th scope="row">Locale("en", "US")</th><td>MyResources_en.properties, parent MyResources.class</td></tr>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1248
     * <tr><th scope="row">Locale("es", "ES")</th><td>MyResources_es_ES.class, parent MyResources.class</td></tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44851
diff changeset
  1249
     * </tbody>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1250
     * </table>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1251
     *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1252
     * <p>The file MyResources_fr_CH.properties is never used because it is
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1253
     * hidden by the MyResources_fr_CH.class. Likewise, MyResources.properties
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  1254
     * is also hidden by MyResources.class.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1256
     * @apiNote If the caller module is a named module and the given
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1257
     * {@code loader} is the caller module's class loader, this method is
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1258
     * equivalent to {@code getBundle(baseName, locale)}; otherwise, it may not
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1259
     * find resource bundles from named modules.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1260
     * Use {@link #getBundle(String, Locale, Module)} to load resource bundles
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1261
     * on behalf on a specific module instead.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1262
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     * @param baseName the base name of the resource bundle, a fully qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     * @param locale the locale for which a resource bundle is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
     * @param loader the class loader from which to load the resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
     * @return a resource bundle for the given base name and locale
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  1267
     * @throws    java.lang.NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1268
     *        if {@code baseName}, {@code locale}, or {@code loader} is {@code null}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  1269
     * @throws    MissingResourceException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
     *        if no resource bundle for the specified base name can be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
     * @since 1.2
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  1272
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  1273
     * @spec JPMS
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1274
     * @see <a href="#resource-bundle-modules">Resource Bundles and Named Modules</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1276
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
    public static ResourceBundle getBundle(String baseName, Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                                           ClassLoader loader)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        if (loader == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1283
        Class<?> caller = Reflection.getCallerClass();
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1284
        return getBundleImpl(baseName, locale, caller, loader, getDefaultControl(caller, baseName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
     * Returns a resource bundle using the specified base name, target
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1289
     * locale, class loader and control. Unlike the {@link
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1290
     * #getBundle(String, Locale, ClassLoader) getBundle}
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  1291
     * factory methods with no {@code control} argument, the given
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1292
     * {@code control} specifies how to locate and instantiate resource
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     * bundles. Conceptually, the bundle loading process with the given
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1294
     * {@code control} is performed in the following steps.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
     * <ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
     * <li>This factory method looks up the resource bundle in the cache for
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1298
     * the specified {@code baseName}, {@code targetLocale} and
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1299
     * {@code loader}.  If the requested resource bundle instance is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
     * found in the cache and the time-to-live periods of the instance and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
     * all of its parent instances have not expired, the instance is returned
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
     * to the caller. Otherwise, this factory method proceeds with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
     * loading process below.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     * <li>The {@link ResourceBundle.Control#getFormats(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
     * control.getFormats} method is called to get resource bundle formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
     * to produce bundle or resource names. The strings
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1308
     * {@code "java.class"} and {@code "java.properties"}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
     * designate class-based and {@linkplain PropertyResourceBundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
     * property}-based resource bundles, respectively. Other strings
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1311
     * starting with {@code "java."} are reserved for future extensions
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
     * and must not be used for application-defined formats. Other strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
     * designate application-defined formats.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
     * <li>The {@link ResourceBundle.Control#getCandidateLocales(String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
     * Locale) control.getCandidateLocales} method is called with the target
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1317
     * locale to get a list of <em>candidate {@code Locale}s</em> for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
     * which resource bundles are searched.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
     * <li>The {@link ResourceBundle.Control#newBundle(String, Locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     * String, ClassLoader, boolean) control.newBundle} method is called to
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1322
     * instantiate a {@code ResourceBundle} for the base bundle name, a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
     * candidate locale, and a format. (Refer to the note on the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
     * lookup below.) This step is iterated over all combinations of the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1325
     * candidate locales and formats until the {@code newBundle} method
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1326
     * returns a {@code ResourceBundle} instance or the iteration has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
     * used up all the combinations. For example, if the candidate locales
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1328
     * are {@code Locale("de", "DE")}, {@code Locale("de")} and
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1329
     * {@code Locale("")} and the formats are {@code "java.class"}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1330
     * and {@code "java.properties"}, then the following is the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
     * sequence of locale-format combinations to be used to call
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1332
     * {@code control.newBundle}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
     *
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44851
diff changeset
  1334
     * <table class=striped style="width: 50%; text-align: left; margin-left: 40px;">
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44851
diff changeset
  1335
     * <caption style="display:none">locale-format combinations for newBundle</caption>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44851
diff changeset
  1336
     * <thead>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     * <tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1338
     * <th scope="col">Index</th>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1339
     * <th scope="col">{@code Locale}</th>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1340
     * <th scope="col">{@code format}</th>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
     * </tr>
45124
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44851
diff changeset
  1342
     * </thead>
144479e89cdb 8179592: Update tables in java.base to be HTML 5-friendly.
jjg
parents: 44851
diff changeset
  1343
     * <tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
     * <tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1345
     * <th scope="row">1</th>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1346
     * <td>{@code Locale("de", "DE")}</td>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1347
     * <td>{@code java.class}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * <tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1350
     * <th scope="row">2</th>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1351
     * <td>{@code Locale("de", "DE")}</td>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1352
     * <td>{@code java.properties}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
     * <tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1355
     * <th scope="row">3</th>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1356
     * <td>{@code Locale("de")}</td>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1357
     * <td>{@code java.class}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
     * <tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1360
     * <th scope="row">4</th>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1361
     * <td>{@code Locale("de")}</td>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1362
     * <td>{@code java.properties}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
     * </tr>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
     * <tr>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1365
     * <th scope="row">5</th>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1366
     * <td>{@code Locale("")}</td>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1367
     * <td>{@code java.class}</td>
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1368
     * </tr>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1369
     * <tr>
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  1370
     * <th scope="row">6</th>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1371
     * <td>{@code Locale("")}</td>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1372
     * <td>{@code java.properties}</td>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
     * </tr>
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  1374
     * </tbody>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
     * </table>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
     * </li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
     * <li>If the previous step has found no resource bundle, proceed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
     * Step 6. If a bundle has been found that is a base bundle (a bundle
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1380
     * for {@code Locale("")}), and the candidate locale list only contained
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1381
     * {@code Locale("")}, return the bundle to the caller. If a bundle
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
     * has been found that is a base bundle, but the candidate locale list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
     * contained locales other than Locale(""), put the bundle on hold and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
     * proceed to Step 6. If a bundle has been found that is not a base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
     * bundle, proceed to Step 7.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
     * <li>The {@link ResourceBundle.Control#getFallbackLocale(String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
     * Locale) control.getFallbackLocale} method is called to get a fallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
     * locale (alternative to the current target locale) to try further
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
     * finding a resource bundle. If the method returns a non-null locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
     * it becomes the next target locale and the loading process starts over
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
     * from Step 3. Otherwise, if a base bundle was found and put on hold in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
     * a previous Step 5, it is returned to the caller now. Otherwise, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
     * MissingResourceException is thrown.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
     * <li>At this point, we have found a resource bundle that's not the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
     * base bundle. If this bundle set its parent during its instantiation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
     * it is returned to the caller. Otherwise, its <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
     * href="./ResourceBundle.html#parent_chain">parent chain</a> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
     * instantiated based on the list of candidate locales from which it was
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
     * found. Finally, the bundle is returned to the caller.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
     * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
     * <p>During the resource bundle loading process above, this factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
     * method looks up the cache before calling the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
     * Control#newBundle(String, Locale, String, ClassLoader, boolean)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * control.newBundle} method.  If the time-to-live period of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * resource bundle found in the cache has expired, the factory method
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     * calls the {@link ResourceBundle.Control#needsReload(String, Locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * String, ClassLoader, ResourceBundle, long) control.needsReload}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * method to determine whether the resource bundle needs to be reloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     * If reloading is required, the factory method calls
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1413
     * {@code control.newBundle} to reload the resource bundle.  If
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1414
     * {@code control.newBundle} returns {@code null}, the factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
     * method puts a dummy resource bundle in the cache as a mark of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
     * nonexistent resource bundles in order to avoid lookup overhead for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
     * subsequent requests. Such dummy resource bundles are under the same
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1418
     * expiration control as specified by {@code control}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
     * <p>All resource bundles loaded are cached by default. Refer to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
     * {@link Control#getTimeToLive(String,Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
     * control.getTimeToLive} for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
     * <p>The following is an example of the bundle loading process with the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1425
     * default {@code ResourceBundle.Control} implementation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
     * <p>Conditions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
     * <ul>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1429
     * <li>Base bundle name: {@code foo.bar.Messages}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1430
     * <li>Requested {@code Locale}: {@link Locale#ITALY}</li>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1431
     * <li>Default {@code Locale}: {@link Locale#FRENCH}</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
     * <li>Available resource bundles:
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1433
     * {@code foo/bar/Messages_fr.properties} and
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1434
     * {@code foo/bar/Messages.properties}</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1437
     * <p>First, {@code getBundle} tries loading a resource bundle in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
     * the following sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
     * <ul>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1441
     * <li>class {@code foo.bar.Messages_it_IT}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1442
     * <li>file {@code foo/bar/Messages_it_IT.properties}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1443
     * <li>class {@code foo.bar.Messages_it}</li>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1444
     * <li>file {@code foo/bar/Messages_it.properties}</li>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1445
     * <li>class {@code foo.bar.Messages}</li>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1446
     * <li>file {@code foo/bar/Messages.properties}</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1449
     * <p>At this point, {@code getBundle} finds
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1450
     * {@code foo/bar/Messages.properties}, which is put on hold
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1451
     * because it's the base bundle.  {@code getBundle} calls {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
     * Control#getFallbackLocale(String, Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
     * control.getFallbackLocale("foo.bar.Messages", Locale.ITALY)} which
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1454
     * returns {@code Locale.FRENCH}. Next, {@code getBundle}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
     * tries loading a bundle in the following sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
     * <ul>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1458
     * <li>class {@code foo.bar.Messages_fr}</li>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1459
     * <li>file {@code foo/bar/Messages_fr.properties}</li>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1460
     * <li>class {@code foo.bar.Messages}</li>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1461
     * <li>file {@code foo/bar/Messages.properties}</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1464
     * <p>{@code getBundle} finds
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1465
     * {@code foo/bar/Messages_fr.properties} and creates a
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1466
     * {@code ResourceBundle} instance. Then, {@code getBundle}
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20865
diff changeset
  1467
     * sets up its parent chain from the list of the candidate locales.  Only
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1468
     * {@code foo/bar/Messages.properties} is found in the list and
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1469
     * {@code getBundle} creates a {@code ResourceBundle} instance
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
     * that becomes the parent of the instance for
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1471
     * {@code foo/bar/Messages_fr.properties}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
     * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
     *        the base name of the resource bundle, a fully qualified
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
     *        class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
     * @param targetLocale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
     *        the locale for which a resource bundle is desired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
     * @param loader
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
     *        the class loader from which to load the resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
     * @param control
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
     *        the control which gives information for the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
     *        bundle loading process
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
     * @return a resource bundle for the given base name and locale
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1484
     * @throws NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1485
     *         if {@code baseName}, {@code targetLocale},
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1486
     *         {@code loader}, or {@code control} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1487
     *         {@code null}
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1488
     * @throws MissingResourceException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1489
     *         if no resource bundle for the specified base name can be found
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1490
     * @throws IllegalArgumentException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1491
     *         if the given {@code control} doesn't perform properly
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1492
     *         (e.g., {@code control.getCandidateLocales} returns null.)
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1493
     *         Note that validation of {@code control} is performed as
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1494
     *         needed.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1495
     * @throws UnsupportedOperationException
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1496
     *         if this method is called in a named module
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
     * @since 1.6
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  1498
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  1499
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1501
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    public static ResourceBundle getBundle(String baseName, Locale targetLocale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                                           ClassLoader loader, Control control) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
        if (loader == null || control == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
        }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1507
        Class<?> caller = Reflection.getCallerClass();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1508
        checkNamedModule(caller);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1509
        return getBundleImpl(baseName, targetLocale, caller, loader, control);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1512
    private static Control getDefaultControl(Class<?> caller, String baseName) {
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1513
        return getDefaultControl(caller.getModule(), baseName);
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1514
    }
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1515
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1516
    private static Control getDefaultControl(Module targetModule, String baseName) {
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1517
        return targetModule.isNamed() ?
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1518
            Control.INSTANCE :
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1519
            ResourceBundleControlProviderHolder.getControl(baseName);
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1520
    }
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1521
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1522
    private static class ResourceBundleControlProviderHolder {
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1523
        private static final PrivilegedAction<List<ResourceBundleControlProvider>> pa =
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1524
            () -> {
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1525
                return Collections.unmodifiableList(
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1526
                    ServiceLoader.load(ResourceBundleControlProvider.class,
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1527
                                       ClassLoader.getSystemClassLoader()).stream()
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1528
                        .map(ServiceLoader.Provider::get)
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1529
                        .collect(Collectors.toList()));
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1530
            };
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1531
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1532
        private static final List<ResourceBundleControlProvider> CONTROL_PROVIDERS =
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1533
            AccessController.doPrivileged(pa);
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1534
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1535
        private static Control getControl(String baseName) {
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1536
            return CONTROL_PROVIDERS.isEmpty() ?
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1537
                Control.INSTANCE :
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1538
                CONTROL_PROVIDERS.stream()
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1539
                    .flatMap(provider -> Stream.ofNullable(provider.getControl(baseName)))
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1540
                    .findFirst()
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1541
                    .orElse(Control.INSTANCE);
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1542
        }
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1543
    }
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  1544
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1545
    private static void checkNamedModule(Class<?> caller) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1546
        if (caller.getModule().isNamed()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1547
            throw new UnsupportedOperationException(
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1548
                    "ResourceBundle.Control not supported in named modules");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1549
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1550
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1551
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1552
    private static ResourceBundle getBundleImpl(String baseName,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1553
                                                Locale locale,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1554
                                                Class<?> caller,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1555
                                                Control control) {
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1556
        return getBundleImpl(baseName, locale, caller, caller.getClassLoader(), control);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1557
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1558
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1559
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1560
     * This method will find resource bundles using the legacy mechanism
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1561
     * if the caller is unnamed module or the given class loader is
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1562
     * not the class loader of the caller module getting the resource
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1563
     * bundle, i.e. find the class that is visible to the class loader
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1564
     * and properties from unnamed module.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1565
     *
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1566
     * The module-aware resource bundle lookup mechanism will load
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1567
     * the service providers using the service loader mechanism
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1568
     * as well as properties local in the caller module.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1569
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1570
    private static ResourceBundle getBundleImpl(String baseName,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1571
                                                Locale locale,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1572
                                                Class<?> caller,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1573
                                                ClassLoader loader,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1574
                                                Control control) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1575
        if (caller == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1576
            throw new InternalError("null caller");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1577
        }
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1578
        Module callerModule = caller.getModule();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1579
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1580
        // get resource bundles for a named module only if loader is the module's class loader
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1581
        if (callerModule.isNamed() && loader == getLoader(callerModule)) {
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1582
            return getBundleImpl(callerModule, callerModule, baseName, locale, control);
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1583
        }
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1584
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1585
        // find resource bundles from unnamed module of given class loader
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1586
        // Java agent can add to the bootclasspath e.g. via
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1587
        // java.lang.instrument.Instrumentation and load classes in unnamed module.
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1588
        // It may call RB::getBundle that will end up here with loader == null.
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1589
        Module unnamedModule = loader != null
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1590
            ? loader.getUnnamedModule()
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1591
            : BootLoader.getUnnamedModule();
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1592
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1593
        return getBundleImpl(callerModule, unnamedModule, baseName, locale, control);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1594
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1595
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1596
    private static ResourceBundle getBundleFromModule(Class<?> caller,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1597
                                                      Module module,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1598
                                                      String baseName,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1599
                                                      Locale locale,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1600
                                                      Control control) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1601
        Objects.requireNonNull(module);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1602
        Module callerModule = caller.getModule();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1603
        if (callerModule != module) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1604
            SecurityManager sm = System.getSecurityManager();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1605
            if (sm != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1606
                sm.checkPermission(GET_CLASSLOADER_PERMISSION);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1607
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1608
        }
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1609
        return getBundleImpl(callerModule, module, baseName, locale, control);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1610
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1611
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1612
    private static ResourceBundle getBundleImpl(Module callerModule,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1613
                                                Module module,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1614
                                                String baseName,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1615
                                                Locale locale,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1616
                                                Control control) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
        if (locale == null || control == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1621
        // We create a CacheKey here for use by this call. The base name
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1622
        // and modules will never change during the bundle loading
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
        // process. We have to make sure that the locale is set before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        // using it as a cache key.
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1625
        CacheKey cacheKey = new CacheKey(baseName, locale, module, callerModule);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
        ResourceBundle bundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        // Quick lookup of the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
        BundleReference bundleRef = cacheList.get(cacheKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        if (bundleRef != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
            bundle = bundleRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
            bundleRef = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
        // If this bundle and all of its parents are valid (not expired),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        // then return this bundle. If any of the bundles is expired, we
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        // don't call control.needsReload here but instead drop into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
        // complete loading process below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
        if (isValidBundle(bundle) && hasValidParentChain(bundle)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        // No valid bundle was found in the cache, so we need to load the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        // resource bundle and its parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        boolean isKnownControl = (control == Control.INSTANCE) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
                                   (control instanceof SingleFormatControl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
        List<String> formats = control.getFormats(baseName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
        if (!isKnownControl && !checkList(formats)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            throw new IllegalArgumentException("Invalid Control: getFormats");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
        ResourceBundle baseBundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        for (Locale targetLocale = locale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
             targetLocale != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
             targetLocale = control.getFallbackLocale(baseName, targetLocale)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
            List<Locale> candidateLocales = control.getCandidateLocales(baseName, targetLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
            if (!isKnownControl && !checkList(candidateLocales)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
                throw new IllegalArgumentException("Invalid Control: getCandidateLocales");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1662
            bundle = findBundle(callerModule, module, cacheKey,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1663
                                candidateLocales, formats, 0, control, baseBundle);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
            // If the loaded bundle is the base bundle and exactly for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
            // requested locale or the only candidate locale, then take the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
            // bundle as the resulting one. If the loaded bundle is the base
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
            // bundle, it's put on hold until we finish processing all
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
            // fallback locales.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
            if (isValidBundle(bundle)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
                boolean isBaseBundle = Locale.ROOT.equals(bundle.locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
                if (!isBaseBundle || bundle.locale.equals(locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
                    || (candidateLocales.size() == 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
                        && bundle.locale.equals(candidateLocales.get(0)))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
                // If the base bundle has been loaded, keep the reference in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
                // baseBundle so that we can avoid any redundant loading in case
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
                // the control specify not to cache bundles.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
                if (isBaseBundle && baseBundle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
                    baseBundle = bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
        if (bundle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
            if (baseBundle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
                throwMissingResourceException(baseName, locale, cacheKey.getCause());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
            bundle = baseBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1694
        // keep callerModule and module reachable for as long as we are operating
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1695
        // with WeakReference(s) to them (in CacheKey)...
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1696
        Reference.reachabilityFence(callerModule);
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1697
        Reference.reachabilityFence(module);
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1698
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
        return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  1703
     * Checks if the given {@code List} is not null, not empty,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     * not having null in its elements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  1706
    private static boolean checkList(List<?> a) {
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  1707
        boolean valid = (a != null && !a.isEmpty());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        if (valid) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
            int size = a.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
            for (int i = 0; valid && i < size; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                valid = (a.get(i) != null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
        return valid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1717
    private static ResourceBundle findBundle(Module callerModule,
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1718
                                             Module module,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1719
                                             CacheKey cacheKey,
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  1720
                                             List<Locale> candidateLocales,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  1721
                                             List<String> formats,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  1722
                                             int index,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  1723
                                             Control control,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  1724
                                             ResourceBundle baseBundle) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
        Locale targetLocale = candidateLocales.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
        ResourceBundle parent = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
        if (index != candidateLocales.size() - 1) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1728
            parent = findBundle(callerModule, module, cacheKey,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1729
                                candidateLocales, formats, index + 1,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
                                control, baseBundle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
        } else if (baseBundle != null && Locale.ROOT.equals(targetLocale)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
            return baseBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
        // Before we do the real loading work, see whether we need to
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1736
        // do some housekeeping: If references to modules or
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
        // resource bundles have been nulled out, remove all related
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
        // information from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
        Object ref;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
        while ((ref = referenceQueue.poll()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
            cacheList.remove(((CacheKeyReference)ref).getCacheKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        // flag indicating the resource bundle has expired in the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        boolean expiredBundle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
        // First, look up the cache to see if it's in the cache, without
6660
7bc303389b4f 6915621: (rb) ResourceBundle.getBundle() deadlock when called inside a synchronized thread
naoto
parents: 6501
diff changeset
  1748
        // attempting to load bundle.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
        cacheKey.setLocale(targetLocale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        ResourceBundle bundle = findBundleInCache(cacheKey, control);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
        if (isValidBundle(bundle)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
            expiredBundle = bundle.expired;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
            if (!expiredBundle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                // If its parent is the one asked for by the candidate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                // locales (the runtime lookup path), we can take the cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                // one. (If it's not identical, then we'd have to check the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
                // parent's parents to be consistent with what's been
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                // requested.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                if (bundle.parent == parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                    return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                // Otherwise, remove the cached one since we can't keep
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                // the same bundles having different parents.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
                BundleReference bundleRef = cacheList.get(cacheKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
                if (bundleRef != null && bundleRef.get() == bundle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
                    cacheList.remove(cacheKey, bundleRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
        if (bundle != NONEXISTENT_BUNDLE) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1772
            trace("findBundle: %d %s %s formats: %s%n", index, candidateLocales, cacheKey, formats);
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1773
            if (module.isNamed()) {
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1774
                bundle = loadBundle(cacheKey, formats, control, module, callerModule);
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1775
            } else {
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1776
                bundle = loadBundle(cacheKey, formats, control, expiredBundle);
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1777
            }
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1778
            if (bundle != null) {
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1779
                if (bundle.parent == null) {
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1780
                    bundle.setParent(parent);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1781
                }
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1782
                bundle.locale = targetLocale;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1783
                bundle = putBundleInCache(cacheKey, bundle, control);
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1784
                return bundle;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1785
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1787
            // Put NONEXISTENT_BUNDLE in the cache as a mark that there's no bundle
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1788
            // instance for the locale.
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1789
            putBundleInCache(cacheKey, NONEXISTENT_BUNDLE, control);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
        return parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1794
    private static final String UNKNOWN_FORMAT = "";
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1795
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1796
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1797
    /*
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1798
     * Loads a ResourceBundle in named modules
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1799
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1800
    private static ResourceBundle loadBundle(CacheKey cacheKey,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1801
                                             List<String> formats,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1802
                                             Control control,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1803
                                             Module module,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1804
                                             Module callerModule) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1805
        String baseName = cacheKey.getName();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1806
        Locale targetLocale = cacheKey.getLocale();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1807
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1808
        ResourceBundle bundle = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1809
        if (cacheKey.hasProviders()) {
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1810
            if (callerModule == module) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1811
                bundle = loadBundleFromProviders(baseName,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1812
                                                 targetLocale,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1813
                                                 cacheKey.getProviders(),
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1814
                                                 cacheKey);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1815
            } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1816
                // load from provider if the caller module has access to the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1817
                // service type and also declares `uses`
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1818
                ClassLoader loader = getLoader(module);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1819
                Class<ResourceBundleProvider> svc =
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1820
                    getResourceBundleProviderType(baseName, loader);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1821
                if (svc != null
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1822
                        && Reflection.verifyModuleAccess(callerModule, svc)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1823
                        && callerModule.canUse(svc)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1824
                    bundle = loadBundleFromProviders(baseName,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1825
                                                     targetLocale,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1826
                                                     cacheKey.getProviders(),
42467
46018567e1a6 8170772: ResourceBundle improper caching causes tools/javadoc tests intermittently
mchung
parents: 42338
diff changeset
  1827
                                                     cacheKey);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1828
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1829
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1830
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1831
            if (bundle != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1832
                cacheKey.setFormat(UNKNOWN_FORMAT);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1833
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1834
        }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1835
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1836
        // If none of providers returned a bundle and the caller has no provider,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1837
        // look up module-local bundles or from the class path
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1838
        if (bundle == null && !cacheKey.callerHasProvider()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1839
            for (String format : formats) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1840
                try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1841
                    switch (format) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1842
                    case "java.class":
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1843
                        bundle = ResourceBundleProviderHelper
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1844
                            .loadResourceBundle(callerModule, module, baseName, targetLocale);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1845
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1846
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1847
                    case "java.properties":
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1848
                        bundle = ResourceBundleProviderHelper
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1849
                            .loadPropertyResourceBundle(callerModule, module, baseName, targetLocale);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1850
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1851
                    default:
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1852
                        throw new InternalError("unexpected format: " + format);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1853
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1854
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1855
                    if (bundle != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1856
                        cacheKey.setFormat(format);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1857
                        break;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1858
                    }
44535
c22dd62a2542 8177980: ResourceBundle.getBundle throws NoClassDefFoundError when fails to define a class
mchung
parents: 43712
diff changeset
  1859
                } catch (LinkageError|Exception e) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1860
                    cacheKey.setCause(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1861
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1862
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1863
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1864
        return bundle;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1865
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1866
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1867
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1868
     * Returns a ServiceLoader that will find providers that are bound to
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1869
     * a given named module.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1870
     */
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1871
    private static ServiceLoader<ResourceBundleProvider> getServiceLoader(Module module,
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1872
                                                                          String baseName)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1873
    {
39768
8de55c86daa2 8161203: ResourceBundle.getBundle performance regression
okutsu
parents: 37363
diff changeset
  1874
        if (!module.isNamed()) {
8de55c86daa2 8161203: ResourceBundle.getBundle performance regression
okutsu
parents: 37363
diff changeset
  1875
            return null;
8de55c86daa2 8161203: ResourceBundle.getBundle performance regression
okutsu
parents: 37363
diff changeset
  1876
        }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1877
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1878
        ClassLoader loader = getLoader(module);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1879
        Class<ResourceBundleProvider> service =
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1880
                getResourceBundleProviderType(baseName, loader);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1881
        if (service != null && Reflection.verifyModuleAccess(module, service)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1882
            try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1883
                // locate providers that are visible to the class loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1884
                // ServiceConfigurationError will be thrown if the module
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1885
                // does not declare `uses` the service type
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1886
                return ServiceLoader.load(service, loader, module);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1887
            } catch (ServiceConfigurationError e) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1888
                // "uses" not declared
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1889
                return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1890
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1891
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1892
        return null;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1893
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1894
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1895
    /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1896
     * Returns the service type of the given baseName that is visible
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1897
     * to the given class loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1898
     */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1899
    private static Class<ResourceBundleProvider>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1900
            getResourceBundleProviderType(String baseName, ClassLoader loader)
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1901
    {
45439
5673d77a787b 8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents: 45124
diff changeset
  1902
        // Look up <packagename> + ".spi." + <name>"Provider"
5673d77a787b 8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents: 45124
diff changeset
  1903
        int i = baseName.lastIndexOf('.');
5673d77a787b 8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents: 45124
diff changeset
  1904
        if (i <= 0) {
5673d77a787b 8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents: 45124
diff changeset
  1905
            return null;
5673d77a787b 8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents: 45124
diff changeset
  1906
        }
5673d77a787b 8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents: 45124
diff changeset
  1907
5673d77a787b 8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents: 45124
diff changeset
  1908
        String name = baseName.substring(i+1, baseName.length()) + "Provider";
5673d77a787b 8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents: 45124
diff changeset
  1909
        String providerName = baseName.substring(0, i) + ".spi." + name;
5673d77a787b 8180375: Rename <baseName>Provider to <packagename>.spi.<simpleName>Provider
naoto
parents: 45124
diff changeset
  1910
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1911
        // Use the class loader of the getBundle caller so that the caller's
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1912
        // visibility of the provider type is checked.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1913
        return AccessController.doPrivileged(
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1914
            new PrivilegedAction<>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1915
                @Override
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1916
                public Class<ResourceBundleProvider> run() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1917
                    try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1918
                        Class<?> c = Class.forName(providerName, false, loader);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1919
                        if (ResourceBundleProvider.class.isAssignableFrom(c)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1920
                            @SuppressWarnings("unchecked")
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1921
                            Class<ResourceBundleProvider> s = (Class<ResourceBundleProvider>) c;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1922
                            return s;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1923
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1924
                    } catch (ClassNotFoundException e) {}
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1925
                    return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1926
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1927
            });
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1928
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1929
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1930
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1931
     * Loads ResourceBundle from service providers.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1932
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1933
    private static ResourceBundle loadBundleFromProviders(String baseName,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1934
                                                          Locale locale,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1935
                                                          ServiceLoader<ResourceBundleProvider> providers,
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1936
                                                          CacheKey cacheKey)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1937
    {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1938
        if (providers == null) return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1939
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1940
        return AccessController.doPrivileged(
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1941
                new PrivilegedAction<>() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1942
                    public ResourceBundle run() {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1943
                        for (Iterator<ResourceBundleProvider> itr = providers.iterator(); itr.hasNext(); ) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1944
                            try {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1945
                                ResourceBundleProvider provider = itr.next();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1946
                                if (cacheKey != null && cacheKey.callerHasProvider == null
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1947
                                        && cacheKey.getModule() == provider.getClass().getModule()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1948
                                    cacheKey.callerHasProvider = Boolean.TRUE;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1949
                                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1950
                                ResourceBundle bundle = provider.getBundle(baseName, locale);
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  1951
                                trace("provider %s %s locale: %s bundle: %s%n", provider, baseName, locale, bundle);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1952
                                if (bundle != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1953
                                    return bundle;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1954
                                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1955
                            } catch (ServiceConfigurationError | SecurityException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1956
                                if (cacheKey != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1957
                                    cacheKey.setCause(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1958
                                }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1959
                            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1960
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1961
                        if (cacheKey != null && cacheKey.callerHasProvider == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1962
                            cacheKey.callerHasProvider = Boolean.FALSE;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1963
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1964
                        return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1965
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1966
                });
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1967
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1968
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1969
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1970
    /*
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1971
     * Legacy mechanism to load resource bundles
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1972
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  1973
    private static ResourceBundle loadBundle(CacheKey cacheKey,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  1974
                                             List<String> formats,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  1975
                                             Control control,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  1976
                                             boolean reload) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
        // Here we actually load the bundle in the order of formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
        // specified by the getFormats() value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        Locale targetLocale = cacheKey.getLocale();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1982
        Module module = cacheKey.getModule();
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1983
        if (module == null) {
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1984
            // should not happen
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1985
            throw new InternalError(
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1986
                "Module for cache key: " + cacheKey + " has been GCed.");
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1987
        }
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1988
        ClassLoader loader = getLoaderForControl(module);
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1989
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        ResourceBundle bundle = null;
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21949
diff changeset
  1991
        for (String format : formats) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
            try {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  1993
                // ResourceBundle.Control.newBundle may be overridden
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
                bundle = control.newBundle(cacheKey.getName(), targetLocale, format,
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  1995
                                           loader, reload);
22078
bdec5d53e98c 8030851: Update code in java.util to use newer language features
psandoz
parents: 21949
diff changeset
  1996
            } catch (LinkageError | Exception error) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
                // We need to handle the LinkageError case due to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
                // inconsistent case-sensitivity in ClassLoader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
                // See 6572242 for details.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
                cacheKey.setCause(error);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
            if (bundle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
                // Set the format in the cache key so that it can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
                // used when calling needsReload later.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
                cacheKey.setFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
                bundle.name = cacheKey.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
                bundle.locale = targetLocale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
                // Bundle provider might reuse instances. So we should make
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
                // sure to clear the expired flag here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
                bundle.expired = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
        return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2018
    private static boolean isValidBundle(ResourceBundle bundle) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
        return bundle != null && bundle != NONEXISTENT_BUNDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
     * Determines whether any of resource bundles in the parent chain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
     * including the leaf, have expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2026
    private static boolean hasValidParentChain(ResourceBundle bundle) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
        long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        while (bundle != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            if (bundle.expired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
            CacheKey key = bundle.cacheKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            if (key != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
                long expirationTime = key.expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
                if (expirationTime >= 0 && expirationTime <= now) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
            bundle = bundle.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
     * Throw a MissingResourceException with proper message
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2047
    private static void throwMissingResourceException(String baseName,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2048
                                                      Locale locale,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2049
                                                      Throwable cause) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
        // If the cause is a MissingResourceException, avoid creating
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
        // a long chain. (6355009)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
        if (cause instanceof MissingResourceException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
            cause = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
        throw new MissingResourceException("Can't find bundle for base name "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                                           + baseName + ", locale " + locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                                           baseName + "_" + locale, // className
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                                           "",                      // key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                                           cause);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
     * Finds a bundle in the cache. Any expired bundles are marked as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
     * `expired' and removed from the cache upon return.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
     * @param cacheKey the key to look up the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
     * @param control the Control to be used for the expiration control
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
     * @return the cached bundle, or null if the bundle is not found in the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2069
     * cache or its parent has expired. {@code bundle.expire} is true
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
     * upon return if the bundle in the cache has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2072
    private static ResourceBundle findBundleInCache(CacheKey cacheKey,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2073
                                                    Control control) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
        BundleReference bundleRef = cacheList.get(cacheKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
        if (bundleRef == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
        ResourceBundle bundle = bundleRef.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
        if (bundle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
        ResourceBundle p = bundle.parent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        assert p != NONEXISTENT_BUNDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
        // If the parent has expired, then this one must also expire. We
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
        // check only the immediate parent because the actual loading is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        // done from the root (base) to leaf (child) and the purpose of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
        // checking is to propagate expiration towards the leaf. For
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
        // example, if the requested locale is ja_JP_JP and there are
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
        // bundles for all of the candidates in the cache, we have a list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
        // base <- ja <- ja_JP <- ja_JP_JP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
        // If ja has expired, then it will reload ja and the list becomes a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
        // tree.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
        // base <- ja (new)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
        //  "   <- ja (expired) <- ja_JP <- ja_JP_JP
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
        // When looking up ja_JP in the cache, it finds ja_JP in the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
        // which references to the expired ja. Then, ja_JP is marked as
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
        // expired and removed from the cache. This will be propagated to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
        // ja_JP_JP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
        // Now, it's possible, for example, that while loading new ja_JP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
        // someone else has started loading the same bundle and finds the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
        // base bundle has expired. Then, what we get from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
        // getBundle call includes the expired base bundle. However, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
        // someone else didn't start its loading, we wouldn't know if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
        // base bundle has expired at the end of the loading process. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
        // expiration control doesn't guarantee that the returned bundle and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
        // its parents haven't expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
        // We could check the entire parent chain to see if there's any in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
        // the chain that has expired. But this process may never end. An
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
        // extreme case would be that getTimeToLive returns 0 and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        // needsReload always returns true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
        if (p != null && p.expired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
            assert bundle != NONEXISTENT_BUNDLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
            bundle.expired = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
            bundle.cacheKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
            cacheList.remove(cacheKey, bundleRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
            bundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
            CacheKey key = bundleRef.getCacheKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
            long expirationTime = key.expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
            if (!bundle.expired && expirationTime >= 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
                expirationTime <= System.currentTimeMillis()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
                // its TTL period has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
                if (bundle != NONEXISTENT_BUNDLE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
                    // Synchronize here to call needsReload to avoid
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
                    // redundant concurrent calls for the same bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
                    synchronized (bundle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
                        expirationTime = key.expirationTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
                        if (!bundle.expired && expirationTime >= 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                            expirationTime <= System.currentTimeMillis()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                            try {
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2137
                                Module module = cacheKey.getModule();
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2138
                                bundle.expired =
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2139
                                    module == null || // already GCed
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2140
                                    control.needsReload(key.getName(),
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2141
                                                        key.getLocale(),
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2142
                                                        key.getFormat(),
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2143
                                                        getLoaderForControl(module),
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2144
                                                        bundle,
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2145
                                                        key.loadTime);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2146
                            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2147
                                cacheKey.setCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2148
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2149
                            if (bundle.expired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2150
                                // If the bundle needs to be reloaded, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2151
                                // remove the bundle from the cache, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2152
                                // return the bundle with the expired flag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2153
                                // on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2154
                                bundle.cacheKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2155
                                cacheList.remove(cacheKey, bundleRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2156
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2157
                                // Update the expiration control info. and reuse
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2158
                                // the same bundle instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2159
                                setExpirationTime(key, control);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
                    // We just remove NONEXISTENT_BUNDLE from the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2165
                    cacheList.remove(cacheKey, bundleRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2166
                    bundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2168
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
        return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2172
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2174
     * Put a new bundle in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2176
     * @param cacheKey the key for the resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2177
     * @param bundle the resource bundle to be put in the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2178
     * @return the ResourceBundle for the cacheKey; if someone has put
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2179
     * the bundle before this call, the one found in the cache is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2180
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2181
     */
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2182
    private static ResourceBundle putBundleInCache(CacheKey cacheKey,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2183
                                                   ResourceBundle bundle,
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2184
                                                   Control control) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2185
        setExpirationTime(cacheKey, control);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2186
        if (cacheKey.expirationTime != Control.TTL_DONT_CACHE) {
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2187
            CacheKey key = new CacheKey(cacheKey);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2188
            BundleReference bundleRef = new BundleReference(bundle, referenceQueue, key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2189
            bundle.cacheKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2190
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
            // Put the bundle in the cache if it's not been in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
            BundleReference result = cacheList.putIfAbsent(key, bundleRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
            // If someone else has put the same bundle in the cache before
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
            // us and it has not expired, we should use the one in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2196
            if (result != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                ResourceBundle rb = result.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                if (rb != null && !rb.expired) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                    // Clear the back link to the cache key
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                    bundle.cacheKey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
                    bundle = rb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
                    // Clear the reference in the BundleReference so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2203
                    // it won't be enqueued.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2204
                    bundleRef.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2205
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2206
                    // Replace the invalid (garbage collected or expired)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2207
                    // instance with the valid one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2208
                    cacheList.put(key, bundleRef);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2209
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2212
        return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2213
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2214
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2215
    private static void setExpirationTime(CacheKey cacheKey, Control control) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2216
        long ttl = control.getTimeToLive(cacheKey.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2217
                                         cacheKey.getLocale());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2218
        if (ttl >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2219
            // If any expiration time is specified, set the time to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2220
            // expired in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
            long now = System.currentTimeMillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
            cacheKey.loadTime = now;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
            cacheKey.expirationTime = now + ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2224
        } else if (ttl >= Control.TTL_NO_EXPIRATION_CONTROL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2225
            cacheKey.expirationTime = ttl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2226
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2227
            throw new IllegalArgumentException("Invalid Control: TTL=" + ttl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2232
     * Removes all resource bundles from the cache that have been loaded
43189
d37bfddf7c38 8171140: Re-examine ResourceBundle::clearCache method
naoto
parents: 43182
diff changeset
  2233
     * by the caller's module.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2235
     * @since 1.6
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  2236
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  2237
     * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2238
     * @see ResourceBundle.Control#getTimeToLive(String,Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2239
     */
16906
44dfee24cb71 8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents: 13029
diff changeset
  2240
    @CallerSensitive
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2241
    public static final void clearCache() {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  2242
        Class<?> caller = Reflection.getCallerClass();
43189
d37bfddf7c38 8171140: Re-examine ResourceBundle::clearCache method
naoto
parents: 43182
diff changeset
  2243
        cacheList.keySet().removeIf(
d37bfddf7c38 8171140: Re-examine ResourceBundle::clearCache method
naoto
parents: 43182
diff changeset
  2244
            key -> key.getCallerModule() == caller.getModule()
d37bfddf7c38 8171140: Re-examine ResourceBundle::clearCache method
naoto
parents: 43182
diff changeset
  2245
        );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2247
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2249
     * Removes all resource bundles from the cache that have been loaded
43189
d37bfddf7c38 8171140: Re-examine ResourceBundle::clearCache method
naoto
parents: 43182
diff changeset
  2250
     * by the given class loader.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
     * @param loader the class loader
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2253
     * @throws    NullPointerException if {@code loader} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
     * @see ResourceBundle.Control#getTimeToLive(String,Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
    public static final void clearCache(ClassLoader loader) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  2258
        Objects.requireNonNull(loader);
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2259
        cacheList.keySet().removeIf(
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2260
            key -> {
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2261
                Module m;
43189
d37bfddf7c38 8171140: Re-examine ResourceBundle::clearCache method
naoto
parents: 43182
diff changeset
  2262
                return (m = key.getModule()) != null &&
43182
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2263
                       getLoader(m) == loader;
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2264
            }
66e6655abfde 8171139: Simplify ResourceBundle.CacheKey and ClassLoader may not be needed
naoto
parents: 42930
diff changeset
  2265
        );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
     * Gets an object for the given key from this resource bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
     * Returns null if this resource bundle does not contain an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
     * object for the given key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2273
     * @param key the key for the desired object
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2274
     * @throws    NullPointerException if {@code key} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
     * @return the object for the given key, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2277
    protected abstract Object handleGetObject(String key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
     * Returns an enumeration of the keys.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2282
     * @return an {@code Enumeration} of the keys contained in
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2283
     *         this {@code ResourceBundle} and its parent bundles.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
    public abstract Enumeration<String> getKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2288
     * Determines whether the given {@code key} is contained in
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2289
     * this {@code ResourceBundle} or its parent bundles.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2291
     * @param key
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2292
     *        the resource {@code key}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2293
     * @return {@code true} if the given {@code key} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2294
     *        contained in this {@code ResourceBundle} or its
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2295
     *        parent bundles; {@code false} otherwise.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  2296
     * @throws    NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2297
     *         if {@code key} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2298
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2299
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2300
    public boolean containsKey(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2301
        if (key == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2302
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2304
        for (ResourceBundle rb = this; rb != null; rb = rb.parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2305
            if (rb.handleKeySet().contains(key)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2309
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2313
     * Returns a {@code Set} of all keys contained in this
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2314
     * {@code ResourceBundle} and its parent bundles.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2315
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2316
     * @return a {@code Set} of all keys contained in this
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2317
     *         {@code ResourceBundle} and its parent bundles.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
    public Set<String> keySet() {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2321
        Set<String> keys = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
        for (ResourceBundle rb = this; rb != null; rb = rb.parent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
            keys.addAll(rb.handleKeySet());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2325
        return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2329
     * Returns a {@code Set} of the keys contained <em>only</em>
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2330
     * in this {@code ResourceBundle}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2331
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2332
     * <p>The default implementation returns a {@code Set} of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
     * keys returned by the {@link #getKeys() getKeys} method except
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
     * for the ones for which the {@link #handleGetObject(String)
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2335
     * handleGetObject} method returns {@code null}. Once the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2336
     * {@code Set} has been created, the value is kept in this
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2337
     * {@code ResourceBundle} in order to avoid producing the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2338
     * same {@code Set} in subsequent calls. Subclasses can
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2339
     * override this method for faster handling.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2341
     * @return a {@code Set} of the keys contained only in this
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2342
     *        {@code ResourceBundle}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2343
     * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2345
    protected Set<String> handleKeySet() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
        if (keySet == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2347
            synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2348
                if (keySet == null) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2349
                    Set<String> keys = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2350
                    Enumeration<String> enumKeys = getKeys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2351
                    while (enumKeys.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2352
                        String key = enumKeys.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                        if (handleGetObject(key) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2354
                            keys.add(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
                    keySet = keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
        return keySet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2367
     * {@code ResourceBundle.Control} defines a set of callback methods
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
     * that are invoked by the {@link ResourceBundle#getBundle(String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
     * Locale, ClassLoader, Control) ResourceBundle.getBundle} factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2370
     * methods during the bundle loading process. In other words, a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2371
     * {@code ResourceBundle.Control} collaborates with the factory
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2372
     * methods for loading resource bundles. The default implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2373
     * the callback methods provides the information necessary for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2374
     * factory methods to perform the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2375
     * href="./ResourceBundle.html#default_behavior">default behavior</a>.
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  2376
     *
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  2377
     * <p> {@link ResourceBundle.Control} is designed for an application deployed
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  2378
     * in an unnamed module, for example to support resource bundles in
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  2379
     * non-standard formats or package localized resources in a non-traditional
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  2380
     * convention. {@link ResourceBundleProvider} is the replacement for
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  2381
     * {@code ResourceBundle.Control} when migrating to modules.
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  2382
     * {@code UnsupportedOperationException} will be thrown when a factory
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  2383
     * method that takes the {@code ResourceBundle.Control} parameter is called.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
     * <p>In addition to the callback methods, the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
     * #toBundleName(String, Locale) toBundleName} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
     * #toResourceName(String, String) toResourceName} methods are defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
     * primarily for convenience in implementing the callback
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2389
     * methods. However, the {@code toBundleName} method could be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
     * overridden to provide different conventions in the organization and
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2391
     * packaging of localized resources.  The {@code toResourceName}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2392
     * method is {@code final} to avoid use of wrong resource and class
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
     * name separators.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
     * <p>Two factory methods, {@link #getControl(List)} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
     * #getNoFallbackControl(List)}, provide
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2397
     * {@code ResourceBundle.Control} instances that implement common
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
     * variations of the default bundle loading process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2400
     * <p>The formats returned by the {@link Control#getFormats(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2401
     * getFormats} method and candidate locales returned by the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2402
     * ResourceBundle.Control#getCandidateLocales(String, Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2403
     * getCandidateLocales} method must be consistent in all
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2404
     * {@code ResourceBundle.getBundle} invocations for the same base
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2405
     * bundle. Otherwise, the {@code ResourceBundle.getBundle} methods
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2406
     * may return unintended bundles. For example, if only
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2407
     * {@code "java.class"} is returned by the {@code getFormats}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2408
     * method for the first call to {@code ResourceBundle.getBundle}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2409
     * and only {@code "java.properties"} for the second call, then the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2410
     * second call will return the class-based one that has been cached
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
     * during the first call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2412
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2413
     * <p>A {@code ResourceBundle.Control} instance must be thread-safe
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
     * if it's simultaneously used by multiple threads.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2415
     * {@code ResourceBundle.getBundle} does not synchronize to call
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2416
     * the {@code ResourceBundle.Control} methods. The default
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
     * implementations of the methods are thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2419
     * <p>Applications can specify {@code ResourceBundle.Control}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2420
     * instances returned by the {@code getControl} factory methods or
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2421
     * created from a subclass of {@code ResourceBundle.Control} to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
     * customize the bundle loading process. The following are examples of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
     * changing the default bundle loading process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
     * <p><b>Example 1</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2426
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2427
     * <p>The following code lets {@code ResourceBundle.getBundle} look
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2428
     * up only properties-based resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2430
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2431
     * import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2432
     * import static java.util.ResourceBundle.Control.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2433
     * ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2434
     * ResourceBundle bundle =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2435
     *   ResourceBundle.getBundle("MyResources", new Locale("fr", "CH"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2436
     *                            ResourceBundle.Control.getControl(FORMAT_PROPERTIES));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2437
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
     * Given the resource bundles in the <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2440
     * href="./ResourceBundle.html#default_behavior_example">example</a> in
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2441
     * the {@code ResourceBundle.getBundle} description, this
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2442
     * {@code ResourceBundle.getBundle} call loads
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2443
     * {@code MyResources_fr_CH.properties} whose parent is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2444
     * {@code MyResources_fr.properties} whose parent is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2445
     * {@code MyResources.properties}. ({@code MyResources_fr_CH.properties}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2446
     * is not hidden, but {@code MyResources_fr_CH.class} is.)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
     * <p><b>Example 2</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
     * <p>The following is an example of loading XML-based bundles
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
     * using {@link Properties#loadFromXML(java.io.InputStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2452
     * Properties.loadFromXML}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2454
     * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2455
     * ResourceBundle rb = ResourceBundle.getBundle("Messages",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2456
     *     new ResourceBundle.Control() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2457
     *         public List&lt;String&gt; getFormats(String baseName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2458
     *             if (baseName == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2459
     *                 throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2460
     *             return Arrays.asList("xml");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2461
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2462
     *         public ResourceBundle newBundle(String baseName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2463
     *                                         Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2464
     *                                         String format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2465
     *                                         ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2466
     *                                         boolean reload)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2467
     *                          throws IllegalAccessException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2468
     *                                 InstantiationException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2469
     *                                 IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2470
     *             if (baseName == null || locale == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2471
     *                   || format == null || loader == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2472
     *                 throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2473
     *             ResourceBundle bundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2474
     *             if (format.equals("xml")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2475
     *                 String bundleName = toBundleName(baseName, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2476
     *                 String resourceName = toResourceName(bundleName, format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2477
     *                 InputStream stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2478
     *                 if (reload) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2479
     *                     URL url = loader.getResource(resourceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2480
     *                     if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2481
     *                         URLConnection connection = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2482
     *                         if (connection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2483
     *                             // Disable caches to get fresh data for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2484
     *                             // reloading.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2485
     *                             connection.setUseCaches(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2486
     *                             stream = connection.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2487
     *                         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2488
     *                     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2489
     *                 } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2490
     *                     stream = loader.getResourceAsStream(resourceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2491
     *                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2492
     *                 if (stream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2493
     *                     BufferedInputStream bis = new BufferedInputStream(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2494
     *                     bundle = new XMLResourceBundle(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2495
     *                     bis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2496
     *                 }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2497
     *             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2498
     *             return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2499
     *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2500
     *     });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2502
     * ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2503
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2504
     * private static class XMLResourceBundle extends ResourceBundle {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2505
     *     private Properties props;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2506
     *     XMLResourceBundle(InputStream stream) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2507
     *         props = new Properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2508
     *         props.loadFromXML(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2509
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2510
     *     protected Object handleGetObject(String key) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2511
     *         return props.getProperty(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2512
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2513
     *     public Enumeration&lt;String&gt; getKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2514
     *         ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2515
     *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2516
     * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2517
     * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2518
     *
48361
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  2519
     * @apiNote {@code ResourceBundle.Control} is not supported
e5a3b905622e 8193767: Improve javadoc in ResourceBundle working with modules
mchung
parents: 47478
diff changeset
  2520
     * in named modules. If the {@code ResourceBundle.getBundle} method with
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  2521
     * a {@code ResourceBundle.Control} is called in a named module, the method
43337
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  2522
     * will throw an {@link UnsupportedOperationException}. Any service providers
e107e6921357 8172365: Provide a better migration path for ResourceBundleControlProvider
naoto
parents: 43189
diff changeset
  2523
     * of {@link ResourceBundleControlProvider} are ignored in named modules.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  2524
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2525
     * @since 1.6
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  2526
     * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  2527
     * @spec JPMS
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  2528
     * @see java.util.spi.ResourceBundleProvider
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2529
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2530
    public static class Control {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2531
        /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2532
         * The default format {@code List}, which contains the strings
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2533
         * {@code "java.class"} and {@code "java.properties"}, in
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2534
         * this order. This {@code List} is unmodifiable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2535
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2536
         * @see #getFormats(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2537
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2538
        public static final List<String> FORMAT_DEFAULT
41482
05e75e5f3afb 8134373: use collections convenience factories in the JDK
smarks
parents: 39768
diff changeset
  2539
            = List.of("java.class", "java.properties");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2540
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2541
        /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2542
         * The class-only format {@code List} containing
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2543
         * {@code "java.class"}. This {@code List} is unmodifiable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2544
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2545
         * @see #getFormats(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2546
         */
41482
05e75e5f3afb 8134373: use collections convenience factories in the JDK
smarks
parents: 39768
diff changeset
  2547
        public static final List<String> FORMAT_CLASS = List.of("java.class");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2548
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2549
        /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2550
         * The properties-only format {@code List} containing
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2551
         * {@code "java.properties"}. This {@code List} is unmodifiable.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2552
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2553
         * @see #getFormats(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2554
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2555
        public static final List<String> FORMAT_PROPERTIES
41482
05e75e5f3afb 8134373: use collections convenience factories in the JDK
smarks
parents: 39768
diff changeset
  2556
            = List.of("java.properties");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2557
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2558
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2559
         * The time-to-live constant for not caching loaded resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2560
         * instances.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2561
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2562
         * @see #getTimeToLive(String, Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2563
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2564
        public static final long TTL_DONT_CACHE = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2566
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2567
         * The time-to-live constant for disabling the expiration control
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2568
         * for loaded resource bundle instances in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2569
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2570
         * @see #getTimeToLive(String, Locale)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2571
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2572
        public static final long TTL_NO_EXPIRATION_CONTROL = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2573
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2574
        private static final Control INSTANCE = new Control();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2575
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2576
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2577
         * Sole constructor. (For invocation by subclass constructors,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2578
         * typically implicit.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2579
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2580
        protected Control() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2581
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2582
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2583
        /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2584
         * Returns a {@code ResourceBundle.Control} in which the {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2585
         * #getFormats(String) getFormats} method returns the specified
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2586
         * {@code formats}. The {@code formats} must be equal to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2587
         * one of {@link Control#FORMAT_PROPERTIES}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2588
         * Control#FORMAT_CLASS} or {@link
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2589
         * Control#FORMAT_DEFAULT}. {@code ResourceBundle.Control}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2590
         * instances returned by this method are singletons and thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2591
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2592
         * <p>Specifying {@link Control#FORMAT_DEFAULT} is equivalent to
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2593
         * instantiating the {@code ResourceBundle.Control} class,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2594
         * except that this method returns a singleton.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2595
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2596
         * @param formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2597
         *        the formats to be returned by the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2598
         *        {@code ResourceBundle.Control.getFormats} method
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2599
         * @return a {@code ResourceBundle.Control} supporting the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2600
         *        specified {@code formats}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  2601
         * @throws    NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2602
         *        if {@code formats} is {@code null}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  2603
         * @throws    IllegalArgumentException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2604
         *        if {@code formats} is unknown
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2605
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2606
        public static final Control getControl(List<String> formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2607
            if (formats.equals(Control.FORMAT_PROPERTIES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2608
                return SingleFormatControl.PROPERTIES_ONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2609
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2610
            if (formats.equals(Control.FORMAT_CLASS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2611
                return SingleFormatControl.CLASS_ONLY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2612
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2613
            if (formats.equals(Control.FORMAT_DEFAULT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2614
                return Control.INSTANCE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2615
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2616
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2618
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2619
        /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2620
         * Returns a {@code ResourceBundle.Control} in which the {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2621
         * #getFormats(String) getFormats} method returns the specified
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2622
         * {@code formats} and the {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2623
         * Control#getFallbackLocale(String, Locale) getFallbackLocale}
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2624
         * method returns {@code null}. The {@code formats} must
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2625
         * be equal to one of {@link Control#FORMAT_PROPERTIES}, {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2626
         * Control#FORMAT_CLASS} or {@link Control#FORMAT_DEFAULT}.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2627
         * {@code ResourceBundle.Control} instances returned by this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2628
         * method are singletons and thread-safe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2629
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2630
         * @param formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2631
         *        the formats to be returned by the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2632
         *        {@code ResourceBundle.Control.getFormats} method
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2633
         * @return a {@code ResourceBundle.Control} supporting the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2634
         *        specified {@code formats} with no fallback
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2635
         *        {@code Locale} support
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  2636
         * @throws    NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2637
         *        if {@code formats} is {@code null}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  2638
         * @throws    IllegalArgumentException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2639
         *        if {@code formats} is unknown
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2640
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2641
        public static final Control getNoFallbackControl(List<String> formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2642
            if (formats.equals(Control.FORMAT_DEFAULT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2643
                return NoFallbackControl.NO_FALLBACK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2644
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2645
            if (formats.equals(Control.FORMAT_PROPERTIES)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2646
                return NoFallbackControl.PROPERTIES_ONLY_NO_FALLBACK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2647
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2648
            if (formats.equals(Control.FORMAT_CLASS)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2649
                return NoFallbackControl.CLASS_ONLY_NO_FALLBACK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2651
            throw new IllegalArgumentException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2653
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2654
        /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2655
         * Returns a {@code List} of {@code String}s containing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2656
         * formats to be used to load resource bundles for the given
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2657
         * {@code baseName}. The {@code ResourceBundle.getBundle}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2658
         * factory method tries to load resource bundles with formats in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2659
         * order specified by the list. The list returned by this method
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2660
         * must have at least one {@code String}. The predefined
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2661
         * formats are {@code "java.class"} for class-based resource
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2662
         * bundles and {@code "java.properties"} for {@linkplain
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2663
         * PropertyResourceBundle properties-based} ones. Strings starting
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2664
         * with {@code "java."} are reserved for future extensions and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2665
         * must not be used by application-defined formats.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2666
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2667
         * <p>It is not a requirement to return an immutable (unmodifiable)
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2668
         * {@code List}.  However, the returned {@code List} must
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2669
         * not be mutated after it has been returned by
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2670
         * {@code getFormats}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2671
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2672
         * <p>The default implementation returns {@link #FORMAT_DEFAULT} so
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2673
         * that the {@code ResourceBundle.getBundle} factory method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2674
         * looks up first class-based resource bundles, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2675
         * properties-based ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2676
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2677
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2678
         *        the base name of the resource bundle, a fully qualified class
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2679
         *        name
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2680
         * @return a {@code List} of {@code String}s containing
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2681
         *        formats for loading resource bundles.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  2682
         * @throws    NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2683
         *        if {@code baseName} is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2684
         * @see #FORMAT_DEFAULT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2685
         * @see #FORMAT_CLASS
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2686
         * @see #FORMAT_PROPERTIES
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2687
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2688
        public List<String> getFormats(String baseName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2689
            if (baseName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2690
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2691
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2692
            return FORMAT_DEFAULT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2693
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2694
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2695
        /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2696
         * Returns a {@code List} of {@code Locale}s as candidate
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2697
         * locales for {@code baseName} and {@code locale}. This
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2698
         * method is called by the {@code ResourceBundle.getBundle}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2699
         * factory method each time the factory method tries finding a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2700
         * resource bundle for a target {@code Locale}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2701
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2702
         * <p>The sequence of the candidate locales also corresponds to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2703
         * runtime resource lookup path (also known as the <I>parent
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2704
         * chain</I>), if the corresponding resource bundles for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2705
         * candidate locales exist and their parents are not defined by
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2706
         * loaded resource bundles themselves.  The last element of the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2707
         * must be a {@linkplain Locale#ROOT root locale} if it is desired to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2708
         * have the base bundle as the terminal of the parent chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2709
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2710
         * <p>If the given locale is equal to {@code Locale.ROOT} (the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2711
         * root locale), a {@code List} containing only the root
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2712
         * {@code Locale} must be returned. In this case, the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2713
         * {@code ResourceBundle.getBundle} factory method loads only
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2714
         * the base bundle as the resulting resource bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2715
         *
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2716
         * <p>It is not a requirement to return an immutable (unmodifiable)
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2717
         * {@code List}. However, the returned {@code List} must not
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2718
         * be mutated after it has been returned by
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2719
         * {@code getCandidateLocales}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2720
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2721
         * <p>The default implementation returns a {@code List} containing
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2722
         * {@code Locale}s using the rules described below.  In the
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2723
         * description below, <em>L</em>, <em>S</em>, <em>C</em> and <em>V</em>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2724
         * respectively represent non-empty language, script, country, and
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2725
         * variant.  For example, [<em>L</em>, <em>C</em>] represents a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2726
         * {@code Locale} that has non-empty values only for language and
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2727
         * country.  The form <em>L</em>("xx") represents the (non-empty)
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2728
         * language value is "xx".  For all cases, {@code Locale}s whose
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2729
         * final component values are empty strings are omitted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2730
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2731
         * <ol><li>For an input {@code Locale} with an empty script value,
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2732
         * append candidate {@code Locale}s by omitting the final component
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2733
         * one by one as below:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2734
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2735
         * <ul>
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2736
         * <li> [<em>L</em>, <em>C</em>, <em>V</em>] </li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2737
         * <li> [<em>L</em>, <em>C</em>] </li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2738
         * <li> [<em>L</em>] </li>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2739
         * <li> {@code Locale.ROOT} </li>
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2740
         * </ul></li>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2741
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2742
         * <li>For an input {@code Locale} with a non-empty script value,
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2743
         * append candidate {@code Locale}s by omitting the final component
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2744
         * up to language, then append candidates generated from the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2745
         * {@code Locale} with country and variant restored:
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2746
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2747
         * <ul>
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2748
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V</em>]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2749
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2750
         * <li> [<em>L</em>, <em>S</em>]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2751
         * <li> [<em>L</em>, <em>C</em>, <em>V</em>]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2752
         * <li> [<em>L</em>, <em>C</em>]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2753
         * <li> [<em>L</em>]</li>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2754
         * <li> {@code Locale.ROOT}</li>
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2755
         * </ul></li>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2756
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2757
         * <li>For an input {@code Locale} with a variant value consisting
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2758
         * of multiple subtags separated by underscore, generate candidate
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2759
         * {@code Locale}s by omitting the variant subtags one by one, then
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2760
         * insert them after every occurrence of {@code  Locale}s with the
47478
438e0c9f2f17 8190382: fix small typographic errors in comments
smarks
parents: 47216
diff changeset
  2761
         * full variant value in the original list.  For example, if
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2762
         * the variant consists of two subtags <em>V1</em> and <em>V2</em>:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2763
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2764
         * <ul>
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2765
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V1</em>, <em>V2</em>]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2766
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>, <em>V1</em>]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2767
         * <li> [<em>L</em>, <em>S</em>, <em>C</em>]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2768
         * <li> [<em>L</em>, <em>S</em>]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2769
         * <li> [<em>L</em>, <em>C</em>, <em>V1</em>, <em>V2</em>]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2770
         * <li> [<em>L</em>, <em>C</em>, <em>V1</em>]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2771
         * <li> [<em>L</em>, <em>C</em>]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2772
         * <li> [<em>L</em>]</li>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2773
         * <li> {@code Locale.ROOT}</li>
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2774
         * </ul></li>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2775
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2776
         * <li>Special cases for Chinese.  When an input {@code Locale} has the
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2777
         * language "zh" (Chinese) and an empty script value, either "Hans" (Simplified) or
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2778
         * "Hant" (Traditional) might be supplied, depending on the country.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2779
         * When the country is "CN" (China) or "SG" (Singapore), "Hans" is supplied.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2780
         * When the country is "HK" (Hong Kong SAR China), "MO" (Macau SAR China),
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2781
         * or "TW" (Taiwan), "Hant" is supplied.  For all other countries or when the country
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2782
         * is empty, no script is supplied.  For example, for <code>Locale("zh", "CN")
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2783
         * </code>, the candidate list will be:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2784
         * <ul>
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2785
         * <li> [<em>L</em>("zh"), <em>S</em>("Hans"), <em>C</em>("CN")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2786
         * <li> [<em>L</em>("zh"), <em>S</em>("Hans")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2787
         * <li> [<em>L</em>("zh"), <em>C</em>("CN")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2788
         * <li> [<em>L</em>("zh")]</li>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2789
         * <li> {@code Locale.ROOT}</li>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2790
         * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2791
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2792
         * For {@code Locale("zh", "TW")}, the candidate list will be:
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2793
         * <ul>
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2794
         * <li> [<em>L</em>("zh"), <em>S</em>("Hant"), <em>C</em>("TW")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2795
         * <li> [<em>L</em>("zh"), <em>S</em>("Hant")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2796
         * <li> [<em>L</em>("zh"), <em>C</em>("TW")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2797
         * <li> [<em>L</em>("zh")]</li>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2798
         * <li> {@code Locale.ROOT}</li>
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2799
         * </ul></li>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2800
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2801
         * <li>Special cases for Norwegian.  Both {@code Locale("no", "NO",
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2802
         * "NY")} and {@code Locale("nn", "NO")} represent Norwegian
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2803
         * Nynorsk.  When a locale's language is "nn", the standard candidate
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2804
         * list is generated up to [<em>L</em>("nn")], and then the following
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2805
         * candidates are added:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2806
         *
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2807
         * <ul><li> [<em>L</em>("no"), <em>C</em>("NO"), <em>V</em>("NY")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2808
         * <li> [<em>L</em>("no"), <em>C</em>("NO")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2809
         * <li> [<em>L</em>("no")]</li>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2810
         * <li> {@code Locale.ROOT}</li>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2811
         * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2812
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2813
         * If the locale is exactly {@code Locale("no", "NO", "NY")}, it is first
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2814
         * converted to {@code Locale("nn", "NO")} and then the above procedure is
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2815
         * followed.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2816
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2817
         * <p>Also, Java treats the language "no" as a synonym of Norwegian
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2818
         * Bokm&#xE5;l "nb".  Except for the single case {@code Locale("no",
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2819
         * "NO", "NY")} (handled above), when an input {@code Locale}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2820
         * has language "no" or "nb", candidate {@code Locale}s with
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2821
         * language code "no" and "nb" are interleaved, first using the
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2822
         * requested language, then using its synonym. For example,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2823
         * {@code Locale("nb", "NO", "POSIX")} generates the following
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2824
         * candidate list:
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2825
         *
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2826
         * <ul>
18829
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2827
         * <li> [<em>L</em>("nb"), <em>C</em>("NO"), <em>V</em>("POSIX")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2828
         * <li> [<em>L</em>("no"), <em>C</em>("NO"), <em>V</em>("POSIX")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2829
         * <li> [<em>L</em>("nb"), <em>C</em>("NO")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2830
         * <li> [<em>L</em>("no"), <em>C</em>("NO")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2831
         * <li> [<em>L</em>("nb")]</li>
ec84f0c313b0 8020409: Clean up doclint problems in java.util package, part 1
bpb
parents: 18156
diff changeset
  2832
         * <li> [<em>L</em>("no")]</li>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2833
         * <li> {@code Locale.ROOT}</li>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2834
         * </ul>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2835
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2836
         * {@code Locale("no", "NO", "POSIX")} would generate the same list
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2837
         * except that locales with "no" would appear before the corresponding
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2838
         * locales with "nb".</li>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2839
         * </ol>
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2840
         *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2841
         * <p>The default implementation uses an {@link ArrayList} that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2842
         * overriding implementations may modify before returning it to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2843
         * caller. However, a subclass must not modify it after it has
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2844
         * been returned by {@code getCandidateLocales}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2845
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2846
         * <p>For example, if the given {@code baseName} is "Messages"
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2847
         * and the given {@code locale} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2848
         * <code>Locale("ja",&nbsp;"",&nbsp;"XX")</code>, then a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2849
         * {@code List} of {@code Locale}s:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2850
         * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2851
         *     Locale("ja", "", "XX")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2852
         *     Locale("ja")
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2853
         *     Locale.ROOT
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2854
         * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2855
         * is returned. And if the resource bundles for the "ja" and
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2856
         * "" {@code Locale}s are found, then the runtime resource
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2857
         * lookup path (parent chain) is:
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 16906
diff changeset
  2858
         * <pre>{@code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2859
         *     Messages_ja -> Messages
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 16906
diff changeset
  2860
         * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2861
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2862
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2863
         *        the base name of the resource bundle, a fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2864
         *        qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2865
         * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2866
         *        the locale for which a resource bundle is desired
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2867
         * @return a {@code List} of candidate
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2868
         *        {@code Locale}s for the given {@code locale}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  2869
         * @throws    NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2870
         *        if {@code baseName} or {@code locale} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  2871
         *        {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2872
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2873
        public List<Locale> getCandidateLocales(String baseName, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2874
            if (baseName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2875
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2876
            }
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2877
            return new ArrayList<>(CANDIDATES_CACHE.get(locale.getBaseLocale()));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2878
        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2879
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2880
        private static final CandidateListCache CANDIDATES_CACHE = new CandidateListCache();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2881
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2882
        private static class CandidateListCache extends LocaleObjectCache<BaseLocale, List<Locale>> {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2883
            protected List<Locale> createObject(BaseLocale base) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2884
                String language = base.getLanguage();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2885
                String script = base.getScript();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2886
                String region = base.getRegion();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2887
                String variant = base.getVariant();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2888
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2889
                // Special handling for Norwegian
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2890
                boolean isNorwegianBokmal = false;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2891
                boolean isNorwegianNynorsk = false;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2892
                if (language.equals("no")) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2893
                    if (region.equals("NO") && variant.equals("NY")) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2894
                        variant = "";
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2895
                        isNorwegianNynorsk = true;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2896
                    } else {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2897
                        isNorwegianBokmal = true;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2898
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2899
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2900
                if (language.equals("nb") || isNorwegianBokmal) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2901
                    List<Locale> tmpList = getDefaultList("nb", script, region, variant);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2902
                    // Insert a locale replacing "nb" with "no" for every list entry
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2903
                    List<Locale> bokmalList = new LinkedList<>();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2904
                    for (Locale l : tmpList) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2905
                        bokmalList.add(l);
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52766
diff changeset
  2906
                        if (l.getLanguage().isEmpty()) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2907
                            break;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2908
                        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2909
                        bokmalList.add(Locale.getInstance("no", l.getScript(), l.getCountry(),
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2910
                                l.getVariant(), null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2911
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2912
                    return bokmalList;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2913
                } else if (language.equals("nn") || isNorwegianNynorsk) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2914
                    // Insert no_NO_NY, no_NO, no after nn
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2915
                    List<Locale> nynorskList = getDefaultList("nn", script, region, variant);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2916
                    int idx = nynorskList.size() - 1;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2917
                    nynorskList.add(idx++, Locale.getInstance("no", "NO", "NY"));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2918
                    nynorskList.add(idx++, Locale.getInstance("no", "NO", ""));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2919
                    nynorskList.add(idx++, Locale.getInstance("no", "", ""));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2920
                    return nynorskList;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2921
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2922
                // Special handling for Chinese
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2923
                else if (language.equals("zh")) {
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52766
diff changeset
  2924
                    if (script.isEmpty() && !region.isEmpty()) {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2925
                        // Supply script for users who want to use zh_Hans/zh_Hant
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2926
                        // as bundle names (recommended for Java7+)
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2927
                        switch (region) {
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2928
                        case "TW":
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2929
                        case "HK":
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2930
                        case "MO":
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2931
                            script = "Hant";
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2932
                            break;
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2933
                        case "CN":
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2934
                        case "SG":
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2935
                            script = "Hans";
11130
c7093e306a34 7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents: 10419
diff changeset
  2936
                            break;
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2937
                        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2938
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2939
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2940
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2941
                return getDefaultList(language, script, region, variant);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2942
            }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2943
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2944
            private static List<Locale> getDefaultList(String language, String script, String region, String variant) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2945
                List<String> variants = null;
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2946
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52766
diff changeset
  2947
                if (!variant.isEmpty()) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2948
                    variants = new LinkedList<>();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2949
                    int idx = variant.length();
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2950
                    while (idx != -1) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2951
                        variants.add(variant.substring(0, idx));
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2952
                        idx = variant.lastIndexOf('_', --idx);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2953
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2954
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2955
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2956
                List<Locale> list = new LinkedList<>();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2957
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2958
                if (variants != null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2959
                    for (String v : variants) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2960
                        list.add(Locale.getInstance(language, script, region, v, null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2961
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2962
                }
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52766
diff changeset
  2963
                if (!region.isEmpty()) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2964
                    list.add(Locale.getInstance(language, script, region, "", null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2965
                }
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52766
diff changeset
  2966
                if (!script.isEmpty()) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2967
                    list.add(Locale.getInstance(language, script, "", "", null));
42930
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2968
                    // Special handling for Chinese
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2969
                    if (language.equals("zh")) {
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52766
diff changeset
  2970
                        if (region.isEmpty()) {
42930
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2971
                            // Supply region(country) for users who still package Chinese
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2972
                            // bundles using old convension.
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2973
                            switch (script) {
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2974
                                case "Hans":
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2975
                                    region = "CN";
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2976
                                    break;
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2977
                                case "Hant":
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2978
                                    region = "TW";
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2979
                                    break;
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2980
                            }
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2981
                        }
b878b7b2788e 8167143: CLDR timezone parsing does not work for all locales
rgoel
parents: 42769
diff changeset
  2982
                    }
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2983
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2984
                    // With script, after truncating variant, region and script,
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2985
                    // start over without script.
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2986
                    if (variants != null) {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2987
                        for (String v : variants) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2988
                            list.add(Locale.getInstance(language, "", region, v, null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2989
                        }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2990
                    }
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52766
diff changeset
  2991
                    if (!region.isEmpty()) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2992
                        list.add(Locale.getInstance(language, "", region, "", null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2993
                    }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2994
                }
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 52766
diff changeset
  2995
                if (!language.isEmpty()) {
9224
75c0420badef 7028818: (lc) Lazily initialize locale extension
okutsu
parents: 6660
diff changeset
  2996
                    list.add(Locale.getInstance(language, "", "", "", null));
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2997
                }
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2998
                // Add root locale at the end
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  2999
                list.add(Locale.ROOT);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3000
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3001
                return list;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3002
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3005
        /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3006
         * Returns a {@code Locale} to be used as a fallback locale for
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3007
         * further resource bundle searches by the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3008
         * {@code ResourceBundle.getBundle} factory method. This method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3009
         * is called from the factory method every time when no resulting
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3010
         * resource bundle has been found for {@code baseName} and
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3011
         * {@code locale}, where locale is either the parameter for
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3012
         * {@code ResourceBundle.getBundle} or the previous fallback
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3013
         * locale returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3014
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3015
         * <p>The method returns {@code null} if no further fallback
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3016
         * search is desired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3017
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3018
         * <p>The default implementation returns the {@linkplain
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3019
         * Locale#getDefault() default {@code Locale}} if the given
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3020
         * {@code locale} isn't the default one.  Otherwise,
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3021
         * {@code null} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3022
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3023
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3024
         *        the base name of the resource bundle, a fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3025
         *        qualified class name for which
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3026
         *        {@code ResourceBundle.getBundle} has been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3027
         *        unable to find any resource bundles (except for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3028
         *        base bundle)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3029
         * @param locale
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3030
         *        the {@code Locale} for which
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3031
         *        {@code ResourceBundle.getBundle} has been
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3032
         *        unable to find any resource bundles (except for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3033
         *        base bundle)
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3034
         * @return a {@code Locale} for the fallback search,
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3035
         *        or {@code null} if no further fallback search
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3036
         *        is desired.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3037
         * @throws    NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3038
         *        if {@code baseName} or {@code locale}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3039
         *        is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3040
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3041
        public Locale getFallbackLocale(String baseName, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3042
            if (baseName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3043
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3044
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3045
            Locale defaultLocale = Locale.getDefault();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3046
            return locale.equals(defaultLocale) ? null : defaultLocale;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3047
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3049
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3050
         * Instantiates a resource bundle for the given bundle name of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3051
         * given format and locale, using the given class loader if
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3052
         * necessary. This method returns {@code null} if there is no
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3053
         * resource bundle available for the given parameters. If a resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3054
         * bundle can't be instantiated due to an unexpected error, the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3055
         * error must be reported by throwing an {@code Error} or
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3056
         * {@code Exception} rather than simply returning
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3057
         * {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3058
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3059
         * <p>If the {@code reload} flag is {@code true}, it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3060
         * indicates that this method is being called because the previously
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3061
         * loaded resource bundle has expired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3062
         *
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3063
         * @implSpec
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3064
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3065
         * Resource bundles in named modules are subject to the encapsulation
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3066
         * rules specified by {@link Module#getResourceAsStream Module.getResourceAsStream}.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3067
         * A resource bundle in a named module visible to the given class loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3068
         * is accessible when the package of the resource file corresponding
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3069
         * to the resource bundle is open unconditionally.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3070
         *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3071
         * <p>The default implementation instantiates a
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3072
         * {@code ResourceBundle} as follows.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3073
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3074
         * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3075
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3076
         * <li>The bundle name is obtained by calling {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3077
         * #toBundleName(String, Locale) toBundleName(baseName,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3078
         * locale)}.</li>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3079
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3080
         * <li>If {@code format} is {@code "java.class"}, the
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3081
         * {@link Class} specified by the bundle name is loaded with the
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3082
         * given class loader. If the {@code Class} is found and accessible
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3083
         * then the {@code ResourceBundle} is instantiated.  The
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3084
         * resource bundle is accessible if the package of the bundle class file
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3085
         * is open unconditionally; otherwise, {@code IllegalAccessException}
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3086
         * will be thrown.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3087
         * Note that the {@code reload} flag is ignored for loading
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3088
         * class-based resource bundles in this default implementation.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3089
         * </li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3090
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3091
         * <li>If {@code format} is {@code "java.properties"},
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3092
         * {@link #toResourceName(String, String) toResourceName(bundlename,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3093
         * "properties")} is called to get the resource name.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3094
         * If {@code reload} is {@code true}, {@link
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3095
         * ClassLoader#getResource(String) load.getResource} is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3096
         * to get a {@link URL} for creating a {@link
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3097
         * URLConnection}. This {@code URLConnection} is used to
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3098
         * {@linkplain URLConnection#setUseCaches(boolean) disable the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3099
         * caches} of the underlying resource loading layers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3100
         * and to {@linkplain URLConnection#getInputStream() get an
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3101
         * {@code InputStream}}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3102
         * Otherwise, {@link ClassLoader#getResourceAsStream(String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3103
         * loader.getResourceAsStream} is called to get an {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3104
         * InputStream}. Then, a {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3105
         * PropertyResourceBundle} is constructed with the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3106
         * {@code InputStream}.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3107
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3108
         * <li>If {@code format} is neither {@code "java.class"}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3109
         * nor {@code "java.properties"}, an
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3110
         * {@code IllegalArgumentException} is thrown.</li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3111
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3112
         * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3113
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3114
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3115
         *        the base bundle name of the resource bundle, a fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3116
         *        qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3117
         * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3118
         *        the locale for which the resource bundle should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3119
         *        instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3120
         * @param format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3121
         *        the resource bundle format to be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3122
         * @param loader
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3123
         *        the {@code ClassLoader} to use to load the bundle
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3124
         * @param reload
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3125
         *        the flag to indicate bundle reloading; {@code true}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3126
         *        if reloading an expired resource bundle,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3127
         *        {@code false} otherwise
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3128
         * @return the resource bundle instance,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3129
         *        or {@code null} if none could be found.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3130
         * @throws    NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3131
         *        if {@code bundleName}, {@code locale},
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3132
         *        {@code format}, or {@code loader} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3133
         *        {@code null}, or if {@code null} is returned by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3134
         *        {@link #toBundleName(String, Locale) toBundleName}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3135
         * @throws    IllegalArgumentException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3136
         *        if {@code format} is unknown, or if the resource
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3137
         *        found for the given parameters contains malformed data.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3138
         * @throws    ClassCastException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3139
         *        if the loaded class cannot be cast to {@code ResourceBundle}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3140
         * @throws    IllegalAccessException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3141
         *        if the class or its nullary constructor is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3142
         *        accessible.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3143
         * @throws    InstantiationException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3144
         *        if the instantiation of a class fails for some other
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3145
         *        reason.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3146
         * @throws    ExceptionInInitializerError
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3147
         *        if the initialization provoked by this method fails.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3148
         * @throws    SecurityException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3149
         *        If a security manager is present and creation of new
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3150
         *        instances is denied. See {@link Class#newInstance()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3151
         *        for details.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3152
         * @throws    IOException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3153
         *        if an error occurred when reading resources using
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3154
         *        any I/O operations
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3155
         * @see java.util.spi.ResourceBundleProvider#getBundle(String, Locale)
43712
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  3156
         * @revised 9
5dfd0950317c 8173393: Module system implementation refresh (2/2017)
alanb
parents: 43337
diff changeset
  3157
         * @spec JPMS
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3158
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3159
        public ResourceBundle newBundle(String baseName, Locale locale, String format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3160
                                        ClassLoader loader, boolean reload)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3161
                    throws IllegalAccessException, InstantiationException, IOException {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3162
            /*
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3163
             * Legacy mechanism to locate resource bundle in unnamed module only
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3164
             * that is visible to the given loader and accessible to the given caller.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3165
             */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3166
            String bundleName = toBundleName(baseName, locale);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3167
            ResourceBundle bundle = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3168
            if (format.equals("java.class")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3169
                try {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3170
                    Class<?> c = loader.loadClass(bundleName);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3171
                    // If the class isn't a ResourceBundle subclass, throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3172
                    // ClassCastException.
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3173
                    if (ResourceBundle.class.isAssignableFrom(c)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3174
                        @SuppressWarnings("unchecked")
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3175
                        Class<ResourceBundle> bundleClass = (Class<ResourceBundle>)c;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3176
                        Module m = bundleClass.getModule();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3177
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3178
                        // To access a resource bundle in a named module,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3179
                        // either class-based or properties-based, the resource
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3180
                        // bundle must be opened unconditionally,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3181
                        // same rule as accessing a resource file.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3182
                        if (m.isNamed() && !m.isOpen(bundleClass.getPackageName())) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3183
                            throw new IllegalAccessException("unnamed module can't load " +
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3184
                                bundleClass.getName() + " in " + m.toString());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3185
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3186
                        try {
52766
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3187
                            Constructor<ResourceBundle> ctor = AccessController.doPrivileged(
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3188
                                new PrivilegedExceptionAction<>() {
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3189
                                    @Override
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3190
                                    public Constructor<ResourceBundle> run() throws NoSuchMethodException {
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3191
                                        return bundleClass.getDeclaredConstructor();
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3192
                                    }
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3193
                                });
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3194
                            if (!Modifier.isPublic(ctor.getModifiers())) {
52766
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3195
                                throw new IllegalAccessException("no-arg constructor in " +
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3196
                                    bundleClass.getName() + " is not publicly accessible.");
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3197
                            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3198
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3199
                            // java.base may not be able to read the bundleClass's module.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3200
                            PrivilegedAction<Void> pa1 = () -> { ctor.setAccessible(true); return null; };
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3201
                            AccessController.doPrivileged(pa1);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3202
                            bundle = ctor.newInstance((Object[]) null);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3203
                        } catch (InvocationTargetException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3204
                            uncheckedThrow(e);
52766
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3205
                        } catch (PrivilegedActionException e) {
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3206
                            assert e.getException() instanceof NoSuchMethodException;
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3207
                            throw new InstantiationException("public no-arg constructor " +
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3208
                                "does not exist in " + bundleClass.getName());
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3209
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3210
                    } else {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3211
                        throw new ClassCastException(c.getName()
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3212
                                + " cannot be cast to ResourceBundle");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3213
                    }
52766
26b7f3038e27 8214170: ResourceBundle.Control.newBundle should throw IllegalAccessException when constructor of the resource bundle is not public.
naoto
parents: 52427
diff changeset
  3214
                } catch (ClassNotFoundException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3216
            } else if (format.equals("java.properties")) {
27083
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3217
                final String resourceName = toResourceName0(bundleName, "properties");
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3218
                if (resourceName == null) {
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3219
                    return bundle;
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3220
                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3221
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3222
                final boolean reloadFlag = reload;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3223
                InputStream stream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3224
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3225
                    stream = AccessController.doPrivileged(
29986
97167d851fc4 8078467: Update core libraries to use diamond with anonymous classes
darcy
parents: 28059
diff changeset
  3226
                        new PrivilegedExceptionAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3227
                            public InputStream run() throws IOException {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3228
                                URL url = loader.getResource(resourceName);
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3229
                                if (url == null) return null;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3230
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3231
                                URLConnection connection = url.openConnection();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3232
                                if (reloadFlag) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3233
                                    // Disable caches to get fresh data for
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3234
                                    // reloading.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3235
                                    connection.setUseCaches(false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3236
                                }
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3237
                                return connection.getInputStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3238
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3239
                        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3240
                } catch (PrivilegedActionException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3241
                    throw (IOException) e.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3242
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3243
                if (stream != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3244
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3245
                        bundle = new PropertyResourceBundle(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3246
                    } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3247
                        stream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3248
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3249
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3250
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3251
                throw new IllegalArgumentException("unknown format: " + format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3253
            return bundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3254
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3255
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3256
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3257
         * Returns the time-to-live (TTL) value for resource bundles that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3258
         * are loaded under this
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3259
         * {@code ResourceBundle.Control}. Positive time-to-live values
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3260
         * specify the number of milliseconds a bundle can remain in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3261
         * cache without being validated against the source data from which
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3262
         * it was constructed. The value 0 indicates that a bundle must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3263
         * validated each time it is retrieved from the cache. {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3264
         * #TTL_DONT_CACHE} specifies that loaded resource bundles are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3265
         * put in the cache. {@link #TTL_NO_EXPIRATION_CONTROL} specifies
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3266
         * that loaded resource bundles are put in the cache with no
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3267
         * expiration control.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3268
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3269
         * <p>The expiration affects only the bundle loading process by the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3270
         * {@code ResourceBundle.getBundle} factory method.  That is,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3271
         * if the factory method finds a resource bundle in the cache that
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3272
         * has expired, the factory method calls the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3273
         * #needsReload(String, Locale, String, ClassLoader, ResourceBundle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3274
         * long) needsReload} method to determine whether the resource
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3275
         * bundle needs to be reloaded. If {@code needsReload} returns
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3276
         * {@code true}, the cached resource bundle instance is removed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3277
         * from the cache. Otherwise, the instance stays in the cache,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3278
         * updated with the new TTL value returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3279
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3280
         * <p>All cached resource bundles are subject to removal from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3281
         * cache due to memory constraints of the runtime environment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3282
         * Returning a large positive value doesn't mean to lock loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3283
         * resource bundles in the cache.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3284
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3285
         * <p>The default implementation returns {@link #TTL_NO_EXPIRATION_CONTROL}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3286
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3287
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3288
         *        the base name of the resource bundle for which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3289
         *        expiration value is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3290
         * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3291
         *        the locale of the resource bundle for which the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3292
         *        expiration value is specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3293
         * @return the time (0 or a positive millisecond offset from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3294
         *        cached time) to get loaded bundles expired in the cache,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3295
         *        {@link #TTL_NO_EXPIRATION_CONTROL} to disable the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3296
         *        expiration control, or {@link #TTL_DONT_CACHE} to disable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3297
         *        caching.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3298
         * @throws    NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3299
         *        if {@code baseName} or {@code locale} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3300
         *        {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3301
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3302
        public long getTimeToLive(String baseName, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3303
            if (baseName == null || locale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3304
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3306
            return TTL_NO_EXPIRATION_CONTROL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3309
        /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3310
         * Determines if the expired {@code bundle} in the cache needs
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3311
         * to be reloaded based on the loading time given by
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3312
         * {@code loadTime} or some other criteria. The method returns
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3313
         * {@code true} if reloading is required; {@code false}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3314
         * otherwise. {@code loadTime} is a millisecond offset since
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3315
         * the <a href="Calendar.html#Epoch"> {@code Calendar}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3316
         * Epoch</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3317
         *
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3318
         * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3319
         * The calling {@code ResourceBundle.getBundle} factory method
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3320
         * calls this method on the {@code ResourceBundle.Control}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3321
         * instance used for its current invocation, not on the instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3322
         * used in the invocation that originally loaded the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3323
         * bundle.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3324
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3325
         * <p>The default implementation compares {@code loadTime} and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3326
         * the last modified time of the source data of the resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3327
         * bundle. If it's determined that the source data has been modified
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3328
         * since {@code loadTime}, {@code true} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3329
         * returned. Otherwise, {@code false} is returned. This
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3330
         * implementation assumes that the given {@code format} is the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3331
         * same string as its file suffix if it's not one of the default
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3332
         * formats, {@code "java.class"} or
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3333
         * {@code "java.properties"}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3334
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3335
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3336
         *        the base bundle name of the resource bundle, a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3337
         *        fully qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3338
         * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3339
         *        the locale for which the resource bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3340
         *        should be instantiated
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3341
         * @param format
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3342
         *        the resource bundle format to be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3343
         * @param loader
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3344
         *        the {@code ClassLoader} to use to load the bundle
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3345
         * @param bundle
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3346
         *        the resource bundle instance that has been expired
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3347
         *        in the cache
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3348
         * @param loadTime
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3349
         *        the time when {@code bundle} was loaded and put
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3350
         *        in the cache
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3351
         * @return {@code true} if the expired bundle needs to be
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3352
         *        reloaded; {@code false} otherwise.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3353
         * @throws    NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3354
         *        if {@code baseName}, {@code locale},
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3355
         *        {@code format}, {@code loader}, or
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3356
         *        {@code bundle} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3357
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3358
        public boolean needsReload(String baseName, Locale locale,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3359
                                   String format, ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3360
                                   ResourceBundle bundle, long loadTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3361
            if (bundle == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3362
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3363
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3364
            if (format.equals("java.class") || format.equals("java.properties")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3365
                format = format.substring(5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3367
            boolean result = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3368
            try {
27083
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3369
                String resourceName = toResourceName0(toBundleName(baseName, locale), format);
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3370
                if (resourceName == null) {
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3371
                    return result;
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3372
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3373
                URL url = loader.getResource(resourceName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3374
                if (url != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3375
                    long lastModified = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3376
                    URLConnection connection = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3377
                    if (connection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3378
                        // disable caches to get the correct data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3379
                        connection.setUseCaches(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3380
                        if (connection instanceof JarURLConnection) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3381
                            JarEntry ent = ((JarURLConnection)connection).getJarEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3382
                            if (ent != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3383
                                lastModified = ent.getTime();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3384
                                if (lastModified == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3385
                                    lastModified = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3386
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3387
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3388
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3389
                            lastModified = connection.getLastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3390
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3391
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3392
                    result = lastModified >= loadTime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3393
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3394
            } catch (NullPointerException npe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3395
                throw npe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3396
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3397
                // ignore other exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3398
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3399
            return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3401
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3402
        /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3403
         * Converts the given {@code baseName} and {@code locale}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3404
         * to the bundle name. This method is called from the default
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3405
         * implementation of the {@link #newBundle(String, Locale, String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3406
         * ClassLoader, boolean) newBundle} and {@link #needsReload(String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3407
         * Locale, String, ClassLoader, ResourceBundle, long) needsReload}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3408
         * methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3409
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3410
         * <p>This implementation returns the following value:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3411
         * <pre>
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3412
         *     baseName + "_" + language + "_" + script + "_" + country + "_" + variant
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3413
         * </pre>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3414
         * where {@code language}, {@code script}, {@code country},
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3415
         * and {@code variant} are the language, script, country, and variant
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3416
         * values of {@code locale}, respectively. Final component values that
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3417
         * are empty Strings are omitted along with the preceding '_'.  When the
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20865
diff changeset
  3418
         * script is empty, the script value is omitted along with the preceding '_'.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3419
         * If all of the values are empty strings, then {@code baseName}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3420
         * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3421
         *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3422
         * <p>For example, if {@code baseName} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3423
         * {@code "baseName"} and {@code locale} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3424
         * <code>Locale("ja",&nbsp;"",&nbsp;"XX")</code>, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3425
         * <code>"baseName_ja_&thinsp;_XX"</code> is returned. If the given
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3426
         * locale is {@code Locale("en")}, then
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3427
         * {@code "baseName_en"} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3428
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3429
         * <p>Overriding this method allows applications to use different
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3430
         * conventions in the organization and packaging of localized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3431
         * resources.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3432
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3433
         * @param baseName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3434
         *        the base name of the resource bundle, a fully
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3435
         *        qualified class name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3436
         * @param locale
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3437
         *        the locale for which a resource bundle should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3438
         *        loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3439
         * @return the bundle name for the resource bundle
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3440
         * @throws    NullPointerException
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3441
         *        if {@code baseName} or {@code locale}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
  3442
         *        is {@code null}
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3443
         * @see java.util.spi.AbstractResourceBundleProvider#toBundleName(String, Locale)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3444
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3445
        public String toBundleName(String baseName, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3446
            if (locale == Locale.ROOT) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3447
                return baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3448
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3449
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3450
            String language = locale.getLanguage();
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3451
            String script = locale.getScript();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3452
            String country = locale.getCountry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3453
            String variant = locale.getVariant();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3454
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3455
            if (language == "" && country == "" && variant == "") {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3456
                return baseName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3457
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3458
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3459
            StringBuilder sb = new StringBuilder(baseName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3460
            sb.append('_');
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3461
            if (script != "") {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3462
                if (variant != "") {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3463
                    sb.append(language).append('_').append(script).append('_').append(country).append('_').append(variant);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3464
                } else if (country != "") {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3465
                    sb.append(language).append('_').append(script).append('_').append(country);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3466
                } else {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3467
                    sb.append(language).append('_').append(script);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3468
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3469
            } else {
6501
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3470
                if (variant != "") {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3471
                    sb.append(language).append('_').append(country).append('_').append(variant);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3472
                } else if (country != "") {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3473
                    sb.append(language).append('_').append(country);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3474
                } else {
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3475
                    sb.append(language);
684810d882b3 6875847: Java Locale Enhancement
naoto
parents: 5506
diff changeset
  3476
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3478
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3480
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3481
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3482
        /**
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  3483
         * Converts the given {@code bundleName} to the form required
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3484
         * by the {@link ClassLoader#getResource ClassLoader.getResource}
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  3485
         * method by replacing all occurrences of {@code '.'} in
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  3486
         * {@code bundleName} with {@code '/'} and appending a
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  3487
         * {@code '.'} and the given file {@code suffix}. For
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  3488
         * example, if {@code bundleName} is
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  3489
         * {@code "foo.bar.MyResources_ja_JP"} and {@code suffix}
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  3490
         * is {@code "properties"}, then
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  3491
         * {@code "foo/bar/MyResources_ja_JP.properties"} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3492
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3493
         * @param bundleName
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3494
         *        the bundle name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3495
         * @param suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3496
         *        the file type suffix
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3497
         * @return the converted resource name
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 54206
diff changeset
  3498
         * @throws    NullPointerException
46900
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  3499
         *         if {@code bundleName} or {@code suffix}
e92e67ed12b4 8186466: Fix accessibility and other minor issues in java.base
jjg
parents: 45439
diff changeset
  3500
         *         is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3501
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3502
        public final String toResourceName(String bundleName, String suffix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3503
            StringBuilder sb = new StringBuilder(bundleName.length() + 1 + suffix.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3504
            sb.append(bundleName.replace('.', '/')).append('.').append(suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3505
            return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3506
        }
27083
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3507
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3508
        private String toResourceName0(String bundleName, String suffix) {
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3509
            // application protocol check
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3510
            if (bundleName.contains("://")) {
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3511
                return null;
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3512
            } else {
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3513
                return toResourceName(bundleName, suffix);
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3514
            }
cbf7fcda2e3f 8044274: Proper property processing
naoto
parents: 25859
diff changeset
  3515
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3517
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3518
    @SuppressWarnings("unchecked")
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3519
    private static <T extends Throwable> void uncheckedThrow(Throwable t) throws T {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3520
        if (t != null)
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3521
            throw (T)t;
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3522
        else
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3523
            throw new Error("Unknown Exception");
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3524
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 29986
diff changeset
  3525
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3526
    private static class SingleFormatControl extends Control {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3527
        private static final Control PROPERTIES_ONLY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3528
            = new SingleFormatControl(FORMAT_PROPERTIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3529
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3530
        private static final Control CLASS_ONLY
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3531
            = new SingleFormatControl(FORMAT_CLASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3532
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3533
        private final List<String> formats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3535
        protected SingleFormatControl(List<String> formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3536
            this.formats = formats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3539
        public List<String> getFormats(String baseName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3540
            if (baseName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3541
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3542
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3543
            return formats;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3544
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3545
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3547
    private static final class NoFallbackControl extends SingleFormatControl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3548
        private static final Control NO_FALLBACK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3549
            = new NoFallbackControl(FORMAT_DEFAULT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3550
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3551
        private static final Control PROPERTIES_ONLY_NO_FALLBACK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3552
            = new NoFallbackControl(FORMAT_PROPERTIES);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3553
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3554
        private static final Control CLASS_ONLY_NO_FALLBACK
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3555
            = new NoFallbackControl(FORMAT_CLASS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3557
        protected NoFallbackControl(List<String> formats) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3558
            super(formats);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3560
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3561
        public Locale getFallbackLocale(String baseName, Locale locale) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3562
            if (baseName == null || locale == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3563
                throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3565
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3567
    }
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3568
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3569
    private static class ResourceBundleProviderHelper {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3570
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3571
         * Returns a new ResourceBundle instance of the given bundleClass
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3572
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3573
        static ResourceBundle newResourceBundle(Class<? extends ResourceBundle> bundleClass) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3574
            try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3575
                @SuppressWarnings("unchecked")
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3576
                Constructor<? extends ResourceBundle> ctor =
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3577
                    bundleClass.getConstructor();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3578
                if (!Modifier.isPublic(ctor.getModifiers())) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3579
                    return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3580
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3581
                // java.base may not be able to read the bundleClass's module.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3582
                PrivilegedAction<Void> pa = () -> { ctor.setAccessible(true); return null;};
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3583
                AccessController.doPrivileged(pa);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3584
                try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3585
                    return ctor.newInstance((Object[]) null);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3586
                } catch (InvocationTargetException e) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3587
                    uncheckedThrow(e);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3588
                } catch (InstantiationException | IllegalAccessException e) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3589
                    throw new InternalError(e);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3590
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3591
            } catch (NoSuchMethodException e) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3592
                throw new InternalError(e);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3593
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3594
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3595
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3596
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3597
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3598
         * Loads a {@code ResourceBundle} of the given {@code bundleName} local to
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3599
         * the given {@code module}. If not found, search the bundle class
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3600
         * that is visible from the module's class loader.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3601
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3602
         * The caller module is used for access check only.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3603
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3604
        static ResourceBundle loadResourceBundle(Module callerModule,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3605
                                                 Module module,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3606
                                                 String baseName,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3607
                                                 Locale locale)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3608
        {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3609
            String bundleName = Control.INSTANCE.toBundleName(baseName, locale);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3610
            try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3611
                PrivilegedAction<Class<?>> pa = () -> Class.forName(module, bundleName);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3612
                Class<?> c = AccessController.doPrivileged(pa, null, GET_CLASSLOADER_PERMISSION);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3613
                trace("local in %s %s caller %s: %s%n", module, bundleName, callerModule, c);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3614
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3615
                if (c == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3616
                    // if not found from the given module, locate resource bundle
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3617
                    // that is visible to the module's class loader
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3618
                    ClassLoader loader = getLoader(module);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3619
                    if (loader != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3620
                        c = Class.forName(bundleName, false, loader);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3621
                    } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3622
                        c = BootLoader.loadClassOrNull(bundleName);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3623
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3624
                    trace("loader for %s %s caller %s: %s%n", module, bundleName, callerModule, c);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3625
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3626
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3627
                if (c != null && ResourceBundle.class.isAssignableFrom(c)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3628
                    @SuppressWarnings("unchecked")
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3629
                    Class<ResourceBundle> bundleClass = (Class<ResourceBundle>) c;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3630
                    Module m = bundleClass.getModule();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3631
                    if (!isAccessible(callerModule, m, bundleClass.getPackageName())) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3632
                        trace("   %s does not have access to %s/%s%n", callerModule,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3633
                              m.getName(), bundleClass.getPackageName());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3634
                        return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3635
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3636
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3637
                    return newResourceBundle(bundleClass);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3638
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3639
            } catch (ClassNotFoundException e) {}
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3640
            return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3641
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3642
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3643
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3644
         * Tests if resources of the given package name from the given module are
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3645
         * open to the caller module.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3646
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3647
        static boolean isAccessible(Module callerModule, Module module, String pn) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3648
            if (!module.isNamed() || callerModule == module)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3649
                return true;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3650
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3651
            return module.isOpen(pn, callerModule);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3652
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3653
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3654
        /**
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3655
         * Loads properties of the given {@code bundleName} local in the given
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3656
         * {@code module}.  If the .properties is not found or not open
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3657
         * to the caller module to access, it will find the resource that
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3658
         * is visible to the module's class loader.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3659
         *
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3660
         * The caller module is used for access check only.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3661
         */
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3662
        static ResourceBundle loadPropertyResourceBundle(Module callerModule,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3663
                                                         Module module,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3664
                                                         String baseName,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3665
                                                         Locale locale)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3666
            throws IOException
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3667
        {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3668
            String bundleName = Control.INSTANCE.toBundleName(baseName, locale);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3669
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3670
            PrivilegedAction<InputStream> pa = () -> {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3671
                try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3672
                    String resourceName = Control.INSTANCE
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3673
                        .toResourceName0(bundleName, "properties");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3674
                    if (resourceName == null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3675
                        return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3676
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3677
                    trace("local in %s %s caller %s%n", module, resourceName, callerModule);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3678
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3679
                    // if the package is in the given module but not opened
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3680
                    // locate it from the given module first.
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3681
                    String pn = toPackageName(bundleName);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3682
                    trace("   %s/%s is accessible to %s : %s%n",
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3683
                            module.getName(), pn, callerModule,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3684
                            isAccessible(callerModule, module, pn));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3685
                    if (isAccessible(callerModule, module, pn)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3686
                        InputStream in = module.getResourceAsStream(resourceName);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3687
                        if (in != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3688
                            return in;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3689
                        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3690
                    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3691
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3692
                    ClassLoader loader = module.getClassLoader();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3693
                    trace("loader for %s %s caller %s%n", module, resourceName, callerModule);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3694
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3695
                    try {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3696
                        if (loader != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3697
                            return loader.getResourceAsStream(resourceName);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3698
                        } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3699
                            URL url = BootLoader.findResource(resourceName);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3700
                            if (url != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3701
                                return url.openStream();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3702
                            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3703
                        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3704
                    } catch (Exception e) {}
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3705
                    return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3706
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3707
                } catch (IOException e) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3708
                    throw new UncheckedIOException(e);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3709
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3710
            };
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3711
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3712
            try (InputStream stream = AccessController.doPrivileged(pa)) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3713
                if (stream != null) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3714
                    return new PropertyResourceBundle(stream);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3715
                } else {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3716
                    return null;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3717
                }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3718
            } catch (UncheckedIOException e) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3719
                throw e.getCause();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3720
            }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3721
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3722
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3723
        private static String toPackageName(String bundleName) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3724
            int i = bundleName.lastIndexOf('.');
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3725
            return i != -1 ? bundleName.substring(0, i) : "";
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3726
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3727
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3728
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3729
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3730
    private static final boolean TRACE_ON = Boolean.valueOf(
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3731
        GetPropertyAction.privilegedGetProperty("resource.bundle.debug", "false"));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3732
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3733
    private static void trace(String format, Object... params) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3734
        if (TRACE_ON)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3735
            System.out.format(format, params);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 41482
diff changeset
  3736
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  3737
}