author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58288 | 48e480e56aad |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54206 | 2 |
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
/* |
|
27 |
* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved |
|
28 |
* (C) Copyright IBM Corp. 1996 - 1999 - All Rights Reserved |
|
29 |
* |
|
30 |
* The original version of this source code and documentation |
|
31 |
* is copyrighted and owned by Taligent, Inc., a wholly-owned |
|
32 |
* subsidiary of IBM. These materials are provided under terms |
|
33 |
* of a License Agreement between Taligent and Sun. This technology |
|
34 |
* is protected by multiple US and International patents. |
|
35 |
* |
|
36 |
* This notice and attribution to Taligent may not be removed. |
|
37 |
* Taligent is a registered trademark of Taligent, Inc. |
|
38 |
* |
|
39 |
*/ |
|
40 |
||
41 |
package java.util; |
|
42 |
||
43 |
import java.io.IOException; |
|
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 | 47 |
import java.lang.ref.ReferenceQueue; |
48 |
import java.lang.ref.SoftReference; |
|
49 |
import java.lang.ref.WeakReference; |
|
36511 | 50 |
import java.lang.reflect.Constructor; |
51 |
import java.lang.reflect.InvocationTargetException; |
|
52 |
import java.lang.reflect.Modifier; |
|
2 | 53 |
import java.net.JarURLConnection; |
54 |
import java.net.URL; |
|
55 |
import java.net.URLConnection; |
|
56 |
import java.security.AccessController; |
|
57 |
import java.security.PrivilegedAction; |
|
58 |
import java.security.PrivilegedActionException; |
|
59 |
import java.security.PrivilegedExceptionAction; |
|
60 |
import java.util.concurrent.ConcurrentHashMap; |
|
61 |
import java.util.concurrent.ConcurrentMap; |
|
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 | 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 | 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 | 74 |
import sun.util.locale.BaseLocale; |
75 |
import sun.util.locale.LocaleObjectCache; |
|
36511 | 76 |
import static sun.security.util.SecurityConstants.GET_CLASSLOADER_PERMISSION; |
6501 | 77 |
|
2 | 78 |
|
79 |
/** |
|
80 |
* |
|
6501 | 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 | 83 |
* load it from the resource bundle that is appropriate for the current user's |
84 |
* locale. In this way, you can write program code that is largely independent |
|
85 |
* of the user's locale isolating most, if not all, of the locale-specific |
|
2 | 86 |
* information in resource bundles. |
87 |
* |
|
88 |
* <p> |
|
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 | 91 |
* <LI> be easily localized, or translated, into different languages |
92 |
* <LI> handle multiple locales at once |
|
93 |
* <LI> be easily modified later to support even more locales |
|
94 |
* </UL> |
|
95 |
* |
|
96 |
* <P> |
|
97 |
* Resource bundles belong to families whose members share a common base |
|
98 |
* name, but whose names also have additional components that identify |
|
99 |
* their locales. For example, the base name of a family of resource |
|
100 |
* bundles might be "MyResources". The family should have a default |
|
101 |
* resource bundle which simply has the same name as its family - |
|
102 |
* "MyResources" - and will be used as the bundle of last resort if a |
|
103 |
* specific locale is not supported. The family can then provide as |
|
104 |
* many locale-specific members as needed, for example a German one |
|
105 |
* named "MyResources_de". |
|
106 |
* |
|
107 |
* <P> |
|
108 |
* Each resource bundle in a family contains the same items, but the items have |
|
109 |
* been translated for the locale represented by that resource bundle. |
|
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 | 113 |
* "MyResources_de" it may contain "Abbrechen". |
114 |
* |
|
115 |
* <P> |
|
116 |
* If there are different resources for different countries, you |
|
117 |
* can make specializations: for example, "MyResources_de_CH" contains objects for |
|
118 |
* the German language (de) in Switzerland (CH). If you want to only |
|
119 |
* modify some of the resources |
|
120 |
* in the specialization, you can do so. |
|
121 |
* |
|
122 |
* <P> |
|
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 | 125 |
* {@link #getBundle(java.lang.String, java.util.Locale) getBundle} |
126 |
* method: |
|
127 |
* <blockquote> |
|
128 |
* <pre> |
|
129 |
* ResourceBundle myResources = |
|
130 |
* ResourceBundle.getBundle("MyResources", currentLocale); |
|
131 |
* </pre> |
|
132 |
* </blockquote> |
|
133 |
* |
|
134 |
* <P> |
|
135 |
* Resource bundles contain key/value pairs. The keys uniquely |
|
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 | 138 |
* two key/value pairs: |
139 |
* <blockquote> |
|
140 |
* <pre> |
|
141 |
* public class MyResources extends ListResourceBundle { |
|
142 |
* protected Object[][] getContents() { |
|
143 |
* return new Object[][] { |
|
144 |
* // LOCALIZE THE SECOND STRING OF EACH ARRAY (e.g., "OK") |
|
145 |
* {"OkKey", "OK"}, |
|
146 |
* {"CancelKey", "Cancel"}, |
|
147 |
* // END OF MATERIAL TO LOCALIZE |
|
148 |
* }; |
|
149 |
* } |
|
150 |
* } |
|
151 |
* </pre> |
|
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 | 154 |
* In this example, the keys are "OkKey" and "CancelKey". |
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 | 157 |
* they don't have to be. The values can be any type of object. |
158 |
* |
|
159 |
* <P> |
|
160 |
* You retrieve an object from resource bundle using the appropriate |
|
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 | 163 |
* <blockquote> |
164 |
* <pre> |
|
165 |
* button1 = new Button(myResources.getString("OkKey")); |
|
166 |
* button2 = new Button(myResources.getString("CancelKey")); |
|
167 |
* </pre> |
|
168 |
* </blockquote> |
|
169 |
* The getter methods all require the key as an argument and return |
|
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 | 172 |
* |
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 | 178 |
* have to cast the result to the appropriate type. For example: |
179 |
* <blockquote> |
|
180 |
* <pre> |
|
181 |
* int[] myIntegers = (int[]) myResources.getObject("intList"); |
|
182 |
* </pre> |
|
183 |
* </blockquote> |
|
184 |
* |
|
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 | 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 | 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 | 192 |
* its resources. |
193 |
* |
|
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 | 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 | 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 | 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 | 218 |
* <h3><a id="service-providers">Resource bundles as service providers</a></h3> |
36511 | 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 | 235 |
* <h3><a id="other-modules">Resource bundles in other modules and class path</a></h3> |
36511 | 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 | 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 | 258 |
* <h3>Resource bundles in automatic modules</h3> |
36511 | 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 | 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 | 273 |
* |
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 | 277 |
* instance. You can implement your own subclass in order to enable |
278 |
* non-standard resource bundle formats, change the search strategy, or |
|
279 |
* define caching parameters. Refer to the descriptions of the class and the |
|
280 |
* {@link #getBundle(String, Locale, ClassLoader, Control) getBundle} |
|
281 |
* factory method for details. |
|
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 | 303 |
* <h2>Cache Management</h2> |
2 | 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 | 306 |
* methods are cached by default, and the factory methods return the same |
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 | 309 |
* lifetime of cached resource bundle instances using time-to-live values, |
310 |
* or specify not to cache resource bundle instances. Refer to the |
|
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 | 313 |
* #clearCache(ClassLoader) clearCache}, {@link |
314 |
* Control#getTimeToLive(String, Locale) |
|
315 |
* ResourceBundle.Control.getTimeToLive}, and {@link |
|
316 |
* Control#needsReload(String, Locale, String, ClassLoader, ResourceBundle, |
|
317 |
* long) ResourceBundle.Control.needsReload} for details. |
|
318 |
* |
|
54206 | 319 |
* <h2>Example</h2> |
2 | 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 | 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 | 326 |
* key with the same value (as for the okKey below). |
327 |
* <blockquote> |
|
328 |
* <pre> |
|
329 |
* // default (English language, United States) |
|
330 |
* public class MyResources extends ResourceBundle { |
|
331 |
* public Object handleGetObject(String key) { |
|
332 |
* if (key.equals("okKey")) return "Ok"; |
|
333 |
* if (key.equals("cancelKey")) return "Cancel"; |
|
334 |
* return null; |
|
335 |
* } |
|
336 |
* |
|
337 |
* public Enumeration<String> getKeys() { |
|
338 |
* return Collections.enumeration(keySet()); |
|
339 |
* } |
|
340 |
* |
|
341 |
* // Overrides handleKeySet() so that the getKeys() implementation |
|
342 |
* // can rely on the keySet() value. |
|
343 |
* protected Set<String> handleKeySet() { |
|
344 |
* return new HashSet<String>(Arrays.asList("okKey", "cancelKey")); |
|
345 |
* } |
|
346 |
* } |
|
347 |
* |
|
348 |
* // German language |
|
349 |
* public class MyResources_de extends MyResources { |
|
350 |
* public Object handleGetObject(String key) { |
|
351 |
* // don't need okKey, since parent level handles it. |
|
352 |
* if (key.equals("cancelKey")) return "Abbrechen"; |
|
353 |
* return null; |
|
354 |
* } |
|
355 |
* |
|
356 |
* protected Set<String> handleKeySet() { |
|
357 |
* return new HashSet<String>(Arrays.asList("cancelKey")); |
|
358 |
* } |
|
359 |
* } |
|
360 |
* </pre> |
|
361 |
* </blockquote> |
|
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 | 368 |
* |
369 |
* @see ListResourceBundle |
|
370 |
* @see PropertyResourceBundle |
|
371 |
* @see MissingResourceException |
|
36511 | 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 | 376 |
*/ |
377 |
public abstract class ResourceBundle { |
|
378 |
||
379 |
/** initial size of the bundle cache */ |
|
380 |
private static final int INITIAL_CACHE_SIZE = 32; |
|
381 |
||
36511 | 382 |
static { |
383 |
SharedSecrets.setJavaUtilResourceBundleAccess( |
|
384 |
new JavaUtilResourceBundleAccess() { |
|
385 |
@Override |
|
386 |
public void setParent(ResourceBundle bundle, |
|
387 |
ResourceBundle parent) { |
|
388 |
bundle.setParent(parent); |
|
389 |
} |
|
390 |
||
391 |
@Override |
|
392 |
public ResourceBundle getParent(ResourceBundle bundle) { |
|
393 |
return bundle.parent; |
|
394 |
} |
|
395 |
||
396 |
@Override |
|
397 |
public void setLocale(ResourceBundle bundle, Locale locale) { |
|
398 |
bundle.locale = locale; |
|
399 |
} |
|
400 |
||
401 |
@Override |
|
402 |
public void setName(ResourceBundle bundle, String name) { |
|
403 |
bundle.name = name; |
|
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 | 418 |
}); |
419 |
} |
|
420 |
||
2 | 421 |
/** constant indicating that no resource bundle exists */ |
422 |
private static final ResourceBundle NONEXISTENT_BUNDLE = new ResourceBundle() { |
|
423 |
public Enumeration<String> getKeys() { return null; } |
|
424 |
protected Object handleGetObject(String key) { return null; } |
|
425 |
public String toString() { return "NONEXISTENT_BUNDLE"; } |
|
426 |
}; |
|
427 |
||
428 |
||
429 |
/** |
|
430 |
* The cache is a map from cache keys (with bundle base name, locale, and |
|
431 |
* class loader) to either a resource bundle or NONEXISTENT_BUNDLE wrapped by a |
|
432 |
* BundleReference. |
|
433 |
* |
|
434 |
* The cache is a ConcurrentMap, allowing the cache to be searched |
|
435 |
* concurrently by multiple threads. This will also allow the cache keys |
|
436 |
* to be reclaimed along with the ClassLoaders they reference. |
|
437 |
* |
|
438 |
* This variable would be better named "cache", but we keep the old |
|
439 |
* name for compatibility with some workarounds for bug 4212439. |
|
440 |
*/ |
|
441 |
private static final ConcurrentMap<CacheKey, BundleReference> cacheList |
|
9224 | 442 |
= new ConcurrentHashMap<>(INITIAL_CACHE_SIZE); |
2 | 443 |
|
444 |
/** |
|
445 |
* Queue for reference objects referring to class loaders or bundles. |
|
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 | 448 |
|
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 | 468 |
* The parent bundle of this bundle. |
469 |
* The parent bundle is searched by {@link #getObject getObject} |
|
470 |
* when this bundle does not contain a particular resource. |
|
471 |
*/ |
|
472 |
protected ResourceBundle parent = null; |
|
473 |
||
474 |
/** |
|
475 |
* The locale for this bundle. |
|
476 |
*/ |
|
477 |
private Locale locale = null; |
|
478 |
||
479 |
/** |
|
480 |
* The base bundle name for this bundle. |
|
481 |
*/ |
|
482 |
private String name; |
|
483 |
||
484 |
/** |
|
485 |
* The flag indicating this bundle has expired in the cache. |
|
486 |
*/ |
|
487 |
private volatile boolean expired; |
|
488 |
||
489 |
/** |
|
490 |
* The back link to the cache key. null if this bundle isn't in |
|
491 |
* the cache (yet) or has expired. |
|
492 |
*/ |
|
493 |
private volatile CacheKey cacheKey; |
|
494 |
||
495 |
/** |
|
496 |
* A Set of the keys contained only in this ResourceBundle. |
|
497 |
*/ |
|
498 |
private volatile Set<String> keySet; |
|
499 |
||
500 |
/** |
|
501 |
* Sole constructor. (For invocation by subclass constructors, typically |
|
502 |
* implicit.) |
|
503 |
*/ |
|
504 |
public ResourceBundle() { |
|
505 |
} |
|
506 |
||
507 |
/** |
|
508 |
* Gets a string for the given key from this resource bundle or one of its parents. |
|
509 |
* Calling this method is equivalent to calling |
|
510 |
* <blockquote> |
|
511 |
* <code>(String) {@link #getObject(java.lang.String) getObject}(key)</code>. |
|
512 |
* </blockquote> |
|
513 |
* |
|
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 | 518 |
* @return the string for the given key |
519 |
*/ |
|
520 |
public final String getString(String key) { |
|
521 |
return (String) getObject(key); |
|
522 |
} |
|
523 |
||
524 |
/** |
|
525 |
* Gets a string array for the given key from this resource bundle or one of its parents. |
|
526 |
* Calling this method is equivalent to calling |
|
527 |
* <blockquote> |
|
528 |
* <code>(String[]) {@link #getObject(java.lang.String) getObject}(key)</code>. |
|
529 |
* </blockquote> |
|
530 |
* |
|
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 | 535 |
* @return the string array for the given key |
536 |
*/ |
|
537 |
public final String[] getStringArray(String key) { |
|
538 |
return (String[]) getObject(key); |
|
539 |
} |
|
540 |
||
541 |
/** |
|
542 |
* Gets an object for the given key from this resource bundle or one of its parents. |
|
543 |
* This method first tries to obtain the object from this resource bundle using |
|
544 |
* {@link #handleGetObject(java.lang.String) handleGetObject}. |
|
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 | 547 |
* If still not successful, it throws a MissingResourceException. |
548 |
* |
|
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 | 552 |
* @return the object for the given key |
553 |
*/ |
|
554 |
public final Object getObject(String key) { |
|
555 |
Object obj = handleGetObject(key); |
|
556 |
if (obj == null) { |
|
557 |
if (parent != null) { |
|
558 |
obj = parent.getObject(key); |
|
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 | 561 |
throw new MissingResourceException("Can't find resource for bundle " |
562 |
+this.getClass().getName() |
|
563 |
+", key "+key, |
|
564 |
this.getClass().getName(), |
|
565 |
key); |
|
11130
c7093e306a34
7117487: Warnings Cleanup: some i18n classes in java.util and sun.util
okutsu
parents:
10419
diff
changeset
|
566 |
} |
2 | 567 |
} |
568 |
return obj; |
|
569 |
} |
|
570 |
||
571 |
/** |
|
572 |
* Returns the locale of this resource bundle. This method can be used after a |
|
573 |
* call to getBundle() to determine whether the resource bundle returned really |
|
574 |
* corresponds to the requested locale or is a fallback. |
|
575 |
* |
|
576 |
* @return the locale of this resource bundle |
|
577 |
*/ |
|
578 |
public Locale getLocale() { |
|
579 |
return locale; |
|
580 |
} |
|
581 |
||
36511 | 582 |
private static ClassLoader getLoader(Module module) { |
583 |
PrivilegedAction<ClassLoader> pa = module::getClassLoader; |
|
584 |
return AccessController.doPrivileged(pa); |
|
585 |
} |
|
586 |
||
2 | 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 | 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 | 594 |
return loader == null ? ClassLoader.getPlatformClassLoader() : loader; |
2 | 595 |
} |
596 |
||
597 |
/** |
|
598 |
* Sets the parent bundle of this bundle. |
|
599 |
* The parent bundle is searched by {@link #getObject getObject} |
|
600 |
* when this bundle does not contain a particular resource. |
|
601 |
* |
|
602 |
* @param parent this bundle's parent bundle. |
|
603 |
*/ |
|
604 |
protected void setParent(ResourceBundle parent) { |
|
605 |
assert parent != NONEXISTENT_BUNDLE; |
|
606 |
this.parent = parent; |
|
607 |
} |
|
608 |
||
609 |
/** |
|
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 | 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 | 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 | 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 | 624 |
|
625 |
// bundle format which is necessary for calling |
|
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 | 628 |
|
629 |
// These time values are in CacheKey so that NONEXISTENT_BUNDLE |
|
630 |
// doesn't need to be cloned for caching. |
|
631 |
||
632 |
// The time when the bundle has been loaded |
|
633 |
private volatile long loadTime; |
|
634 |
||
635 |
// The time when the bundle expires in the cache, or either |
|
636 |
// Control.TTL_DONT_CACHE or Control.TTL_NO_EXPIRATION_CONTROL. |
|
637 |
private volatile long expirationTime; |
|
638 |
||
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 | 641 |
|
36511 | 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 | 645 |
|
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 | 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 | 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 | 652 |
|
2 | 653 |
this.name = baseName; |
654 |
this.locale = locale; |
|
36511 | 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 | 674 |
} |
675 |
||
676 |
String getName() { |
|
677 |
return name; |
|
678 |
} |
|
679 |
||
680 |
Locale getLocale() { |
|
681 |
return locale; |
|
682 |
} |
|
683 |
||
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 | 686 |
return this; |
687 |
} |
|
688 |
||
36511 | 689 |
Module getModule() { |
690 |
return moduleRef.get(); |
|
691 |
} |
|
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 | 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 | 702 |
return providers; |
703 |
} |
|
704 |
||
705 |
boolean hasProviders() { |
|
39768
8de55c86daa2
8161203: ResourceBundle.getBundle performance regression
okutsu
parents:
37363
diff
changeset
|
706 |
return getProviders() != null; |
36511 | 707 |
} |
708 |
||
709 |
boolean callerHasProvider() { |
|
710 |
return callerHasProvider == Boolean.TRUE; |
|
711 |
} |
|
712 |
||
713 |
@Override |
|
2 | 714 |
public boolean equals(Object other) { |
715 |
if (this == other) { |
|
716 |
return true; |
|
717 |
} |
|
718 |
try { |
|
719 |
final CacheKey otherEntry = (CacheKey)other; |
|
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 | 722 |
return false; |
723 |
} |
|
724 |
//are the names the same? |
|
725 |
if (!name.equals(otherEntry.name)) { |
|
726 |
return false; |
|
727 |
} |
|
728 |
// are the locales the same? |
|
729 |
if (!locale.equals(otherEntry.locale)) { |
|
730 |
return false; |
|
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 | 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 | 737 |
} catch (NullPointerException | ClassCastException e) { |
2 | 738 |
} |
739 |
return false; |
|
740 |
} |
|
741 |
||
36511 | 742 |
@Override |
2 | 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 | 745 |
} |
746 |
||
747 |
String getFormat() { |
|
748 |
return format; |
|
749 |
} |
|
750 |
||
751 |
void setFormat(String format) { |
|
752 |
this.format = format; |
|
753 |
} |
|
754 |
||
755 |
private void setCause(Throwable cause) { |
|
756 |
if (this.cause == null) { |
|
757 |
this.cause = cause; |
|
758 |
} else { |
|
759 |
// Override the cause if the previous one is |
|
760 |
// ClassNotFoundException. |
|
761 |
if (this.cause instanceof ClassNotFoundException) { |
|
762 |
this.cause = cause; |
|
763 |
} |
|
764 |
} |
|
765 |
} |
|
766 |
||
767 |
private Throwable getCause() { |
|
768 |
return cause; |
|
769 |
} |
|
770 |
||
36511 | 771 |
@Override |
2 | 772 |
public String toString() { |
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 | 776 |
l = "__" + locale.getVariant(); |
777 |
} else { |
|
778 |
l = "\"\""; |
|
779 |
} |
|
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 | 787 |
} |
788 |
} |
|
789 |
||
790 |
/** |
|
791 |
* The common interface to get a CacheKey in LoaderReference and |
|
792 |
* BundleReference. |
|
793 |
*/ |
|
794 |
private static interface CacheKeyReference { |
|
795 |
public CacheKey getCacheKey(); |
|
796 |
} |
|
797 |
||
798 |
/** |
|
36511 | 799 |
* References to a CacheKey element as a WeakReference so that it can be |
800 |
* garbage collected when nobody else is using it. |
|
2 | 801 |
*/ |
36511 | 802 |
private static class KeyElementReference<T> extends WeakReference<T> |
803 |
implements CacheKeyReference { |
|
804 |
private final CacheKey cacheKey; |
|
2 | 805 |
|
36511 | 806 |
KeyElementReference(T referent, ReferenceQueue<Object> q, CacheKey key) { |
2 | 807 |
super(referent, q); |
808 |
cacheKey = key; |
|
809 |
} |
|
810 |
||
36511 | 811 |
@Override |
2 | 812 |
public CacheKey getCacheKey() { |
813 |
return cacheKey; |
|
814 |
} |
|
815 |
} |
|
816 |
||
817 |
/** |
|
818 |
* References to bundles are soft references so that they can be garbage |
|
819 |
* collected when they have no hard references. |
|
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 | 823 |
private final CacheKey cacheKey; |
2 | 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 | 826 |
super(referent, q); |
827 |
cacheKey = key; |
|
828 |
} |
|
829 |
||
36511 | 830 |
@Override |
2 | 831 |
public CacheKey getCacheKey() { |
832 |
return cacheKey; |
|
833 |
} |
|
834 |
} |
|
835 |
||
836 |
/** |
|
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 | 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 | 841 |
* </blockquote> |
842 |
* |
|
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 | 847 |
* if no resource bundle for the specified base name can be found |
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 | 852 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
13029
diff
changeset
|
853 |
@CallerSensitive |
2 | 854 |
public static final ResourceBundle getBundle(String baseName) |
855 |
{ |
|
36511 | 856 |
Class<?> caller = Reflection.getCallerClass(); |
2 | 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 | 859 |
} |
860 |
||
861 |
/** |
|
862 |
* Returns a resource bundle using the specified base name, the |
|
863 |
* default locale and the specified control. Calling this method |
|
864 |
* is equivalent to calling |
|
865 |
* <pre> |
|
866 |
* getBundle(baseName, Locale.getDefault(), |
|
867 |
* this.getClass().getClassLoader(), control), |
|
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 | 871 |
* #getBundle(String, Locale, ClassLoader, Control) getBundle} for the |
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 | 874 |
* |
875 |
* @param baseName |
|
876 |
* the base name of the resource bundle, a fully qualified class |
|
877 |
* name |
|
878 |
* @param control |
|
879 |
* the control which gives information for the resource bundle |
|
880 |
* loading process |
|
36511 | 881 |
* @return a resource bundle for the given base name and the default locale |
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 | 885 |
* @throws MissingResourceException |
886 |
* if no resource bundle for the specified base name can be found |
|
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 | 891 |
* needed. |
892 |
* @throws UnsupportedOperationException |
|
893 |
* if this method is called in a named module |
|
2 | 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 | 897 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
13029
diff
changeset
|
898 |
@CallerSensitive |
2 | 899 |
public static final ResourceBundle getBundle(String baseName, |
900 |
Control control) { |
|
36511 | 901 |
Class<?> caller = Reflection.getCallerClass(); |
902 |
Locale targetLocale = Locale.getDefault(); |
|
903 |
checkNamedModule(caller); |
|
904 |
return getBundleImpl(baseName, targetLocale, caller, control); |
|
2 | 905 |
} |
906 |
||
907 |
/** |
|
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 | 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 | 912 |
* </blockquote> |
913 |
* |
|
914 |
* @param baseName |
|
915 |
* the base name of the resource bundle, a fully qualified class name |
|
916 |
* @param locale |
|
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 | 921 |
* if no resource bundle for the specified base name can be found |
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 | 926 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
13029
diff
changeset
|
927 |
@CallerSensitive |
2 | 928 |
public static final ResourceBundle getBundle(String baseName, |
929 |
Locale locale) |
|
930 |
{ |
|
36511 | 931 |
Class<?> caller = Reflection.getCallerClass(); |
2 | 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 | 934 |
} |
935 |
||
936 |
/** |
|
937 |
* Gets a resource bundle using the specified base name and the default locale |
|
938 |
* on behalf of the specified module. This method is equivalent to calling |
|
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 | 941 |
* </blockquote> |
942 |
* |
|
943 |
* @param baseName the base name of the resource bundle, |
|
944 |
* a fully qualified class name |
|
945 |
* @param module the module for which the resource bundle is searched |
|
946 |
* @throws NullPointerException |
|
947 |
* if {@code baseName} or {@code module} is {@code null} |
|
948 |
* @throws SecurityException |
|
949 |
* if a security manager exists and the caller is not the specified |
|
950 |
* module and doesn't have {@code RuntimePermission("getClassLoader")} |
|
951 |
* @throws MissingResourceException |
|
952 |
* if no resource bundle for the specified base name can be found in the |
|
953 |
* specified module |
|
954 |
* @return a resource bundle for the given base name and the default locale |
|
955 |
* @since 9 |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
43337
diff
changeset
|
956 |
* @spec JPMS |
36511 | 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 | 960 |
*/ |
961 |
@CallerSensitive |
|
962 |
public static ResourceBundle getBundle(String baseName, Module module) { |
|
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 | 966 |
} |
967 |
||
968 |
/** |
|
969 |
* Gets a resource bundle using the specified base name and locale |
|
970 |
* on behalf of the specified module. |
|
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 | 986 |
* <p> |
987 |
* If the given {@code module} is an unnamed module, then this method is |
|
988 |
* equivalent to calling {@link #getBundle(String, Locale, ClassLoader) |
|
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 | 994 |
* |
995 |
* @param baseName the base name of the resource bundle, |
|
996 |
* a fully qualified class name |
|
997 |
* @param targetLocale the locale for which a resource bundle is desired |
|
998 |
* @param module the module for which the resource bundle is searched |
|
999 |
* @throws NullPointerException |
|
1000 |
* if {@code baseName}, {@code targetLocale}, or {@code module} is |
|
1001 |
* {@code null} |
|
1002 |
* @throws SecurityException |
|
1003 |
* if a security manager exists and the caller is not the specified |
|
1004 |
* module and doesn't have {@code RuntimePermission("getClassLoader")} |
|
1005 |
* @throws MissingResourceException |
|
1006 |
* if no resource bundle for the specified base name and locale can |
|
1007 |
* be found in the specified {@code module} |
|
1008 |
* @return a resource bundle for the given base name and locale in the module |
|
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 | 1013 |
*/ |
1014 |
@CallerSensitive |
|
1015 |
public static ResourceBundle getBundle(String baseName, Locale targetLocale, Module module) { |
|
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 | 1018 |
} |
1019 |
||
1020 |
/** |
|
1021 |
* Returns a resource bundle using the specified base name, target |
|
1022 |
* locale and control, and the caller's class loader. Calling this |
|
1023 |
* method is equivalent to calling |
|
1024 |
* <pre> |
|
1025 |
* getBundle(baseName, targetLocale, this.getClass().getClassLoader(), |
|
1026 |
* control), |
|
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 | 1030 |
* #getBundle(String, Locale, ClassLoader, Control) getBundle} for the |
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 | 1033 |
* |
1034 |
* @param baseName |
|
1035 |
* the base name of the resource bundle, a fully qualified |
|
1036 |
* class name |
|
1037 |
* @param targetLocale |
|
1038 |
* the locale for which a resource bundle is desired |
|
1039 |
* @param control |
|
1040 |
* the control which gives information for the resource |
|
1041 |
* bundle loading process |
|
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 | 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 | 1047 |
* @throws MissingResourceException |
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 | 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 | 1054 |
* needed. |
1055 |
* @throws UnsupportedOperationException |
|
1056 |
* if this method is called in a named module |
|
2 | 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 | 1060 |
*/ |
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
13029
diff
changeset
|
1061 |
@CallerSensitive |
2 | 1062 |
public static final ResourceBundle getBundle(String baseName, Locale targetLocale, |
1063 |
Control control) { |
|
36511 | 1064 |
Class<?> caller = Reflection.getCallerClass(); |
1065 |
checkNamedModule(caller); |
|
1066 |
return getBundleImpl(baseName, targetLocale, caller, control); |
|
2 | 1067 |
} |
1068 |
||
1069 |
/** |
|
6501 | 1070 |
* Gets a resource bundle using the specified base name, locale, and class |
1071 |
* loader. |
|
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 | 1089 |
* |
36511 | 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 | 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 | 1094 |
* the default locale (obtained from {@link java.util.Locale#getDefault() |
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 | 1097 |
* locale's language, script, country, and variant are all empty strings, |
1098 |
* then the base name is the only candidate bundle name. Otherwise, a list |
|
1099 |
* of candidate locales is generated from the attribute values of the |
|
1100 |
* specified locale (language, script, country and variant) and appended to |
|
1101 |
* the base name. Typically, this will look like the following: |
|
1102 |
* |
|
1103 |
* <pre> |
|
1104 |
* baseName + "_" + language + "_" + script + "_" + country + "_" + variant |
|
1105 |
* baseName + "_" + language + "_" + script + "_" + country |
|
1106 |
* baseName + "_" + language + "_" + script |
|
1107 |
* baseName + "_" + language + "_" + country + "_" + variant |
|
1108 |
* baseName + "_" + language + "_" + country |
|
1109 |
* baseName + "_" + language |
|
1110 |
* </pre> |
|
2 | 1111 |
* |
6501 | 1112 |
* <p>Candidate bundle names where the final component is an empty string |
1113 |
* are omitted, along with the underscore. For example, if country is an |
|
1114 |
* empty string, the second and the fifth candidate bundle names above |
|
1115 |
* would be omitted. Also, if script is an empty string, the candidate names |
|
1116 |
* including script are omitted. For example, a locale with language "de" |
|
1117 |
* and variant "JAVA" will produce candidate names with base name |
|
1118 |
* "MyResource" below. |
|
1119 |
* |
|
1120 |
* <pre> |
|
1121 |
* MyResource_de__JAVA |
|
1122 |
* MyResource_de |
|
1123 |
* </pre> |
|
1124 |
* |
|
1125 |
* In the case that the variant contains one or more underscores ('_'), a |
|
1126 |
* sequence of bundle names generated by truncating the last underscore and |
|
1127 |
* the part following it is inserted after a candidate bundle name with the |
|
1128 |
* original variant. For example, for a locale with language "en", script |
|
1129 |
* "Latn, country "US" and variant "WINDOWS_VISTA", and bundle base name |
|
1130 |
* "MyResource", the list of candidate bundle names below is generated: |
|
1131 |
* |
|
1132 |
* <pre> |
|
1133 |
* MyResource_en_Latn_US_WINDOWS_VISTA |
|
1134 |
* MyResource_en_Latn_US_WINDOWS |
|
1135 |
* MyResource_en_Latn_US |
|
1136 |
* MyResource_en_Latn |
|
1137 |
* MyResource_en_US_WINDOWS_VISTA |
|
1138 |
* MyResource_en_US_WINDOWS |
|
1139 |
* MyResource_en_US |
|
1140 |
* MyResource_en |
|
1141 |
* </pre> |
|
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 | 1144 |
* candidate bundle names contains extra names, or the order of bundle names |
1145 |
* is slightly modified. See the description of the default implementation |
|
1146 |
* of {@link Control#getCandidateLocales(String, Locale) |
|
1147 |
* getCandidateLocales} for details.</blockquote> |
|
2 | 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 | 1150 |
* to find the first one for which it can <em>instantiate</em> an actual |
1151 |
* resource bundle. It uses the default controls' {@link Control#getFormats |
|
1152 |
* getFormats} method, which generates two bundle names for each generated |
|
1153 |
* name, the first a class name and the second a properties file name. For |
|
1154 |
* each candidate bundle name, it attempts to create a resource bundle: |
|
1155 |
* |
|
1156 |
* <ul><li>First, it attempts to load a class using the generated class name. |
|
1157 |
* If such a class can be found and loaded using the specified class |
|
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 | 1160 |
* new instance of this class and uses it as the <em>result resource |
1161 |
* bundle</em>. |
|
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 | 1164 |
* resource file using the generated properties file name. It generates a |
1165 |
* path name from the candidate bundle name by replacing all "." characters |
|
1166 |
* with "/" and appending the string ".properties". It attempts to find a |
|
1167 |
* "resource" with this name using {@link |
|
1168 |
* java.lang.ClassLoader#getResource(java.lang.String) |
|
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 | 1171 |
* resource bundle, it is just a container of data, such as a file.) If it |
1172 |
* finds a "resource", it attempts to create a new {@link |
|
1173 |
* PropertyResourceBundle} instance from its contents. If successful, this |
|
1174 |
* instance becomes the <em>result resource bundle</em>. </ul> |
|
2 | 1175 |
* |
6501 | 1176 |
* <p>This continues until a result resource bundle is instantiated or the |
1177 |
* list of candidate bundle names is exhausted. If no matching resource |
|
1178 |
* bundle is found, the default control's {@link Control#getFallbackLocale |
|
1179 |
* getFallbackLocale} method is called, which returns the current default |
|
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 | 1182 |
* |
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 | 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 | 1188 |
* has a parent (perhaps because it was returned from a cache) the chain is |
1189 |
* complete. |
|
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 | 1192 |
* candidate locale list that was used during the pass that generated the |
1193 |
* result resource bundle. (As before, candidate bundle names where the |
|
1194 |
* final component is an empty string are omitted.) When it comes to the |
|
1195 |
* end of the candidate list, it tries the plain bundle name. With each of the |
|
1196 |
* candidate bundle names it attempts to instantiate a resource bundle (first |
|
1197 |
* looking for a class and then a properties file, as described above). |
|
1198 |
* |
|
1199 |
* <p>Whenever it succeeds, it calls the previously instantiated resource |
|
2 | 1200 |
* bundle's {@link #setParent(java.util.ResourceBundle) setParent} method |
6501 | 1201 |
* with the new resource bundle. This continues until the list of names |
1202 |
* is exhausted or the current bundle already has a non-null parent. |
|
1203 |
* |
|
1204 |
* <p>Once the parent chain is complete, the bundle is returned. |
|
2 | 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 | 1207 |
* bundles and might return the same resource bundle instance multiple times. |
2 | 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 | 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 | 1213 |
* path name (using "/") instead of a fully qualified class name (using |
1214 |
* "."). |
|
2 | 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 | 1218 |
* <p> |
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 | 1229 |
* |
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 | 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 | 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 | 1236 |
* instantiate resource bundles as follows: |
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 | 1250 |
* </table> |
1251 |
* |
|
1252 |
* <p>The file MyResources_fr_CH.properties is never used because it is |
|
1253 |
* hidden by the MyResources_fr_CH.class. Likewise, MyResources.properties |
|
1254 |
* is also hidden by MyResources.class. |
|
2 | 1255 |
* |
36511 | 1256 |
* @apiNote If the caller module is a named module and the given |
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 | 1259 |
* find resource bundles from named modules. |
1260 |
* Use {@link #getBundle(String, Locale, Module)} to load resource bundles |
|
1261 |
* on behalf on a specific module instead. |
|
1262 |
* |
|
2 | 1263 |
* @param baseName the base name of the resource bundle, a fully qualified class name |
1264 |
* @param locale the locale for which a resource bundle is desired |
|
1265 |
* @param loader the class loader from which to load the resource bundle |
|
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 | 1270 |
* if no resource bundle for the specified base name can be found |
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 | 1275 |
*/ |
36511 | 1276 |
@CallerSensitive |
2 | 1277 |
public static ResourceBundle getBundle(String baseName, Locale locale, |
1278 |
ClassLoader loader) |
|
1279 |
{ |
|
1280 |
if (loader == null) { |
|
1281 |
throw new NullPointerException(); |
|
1282 |
} |
|
36511 | 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 | 1285 |
} |
1286 |
||
1287 |
/** |
|
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 | 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 | 1295 |
* |
1296 |
* <ol> |
|
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 | 1300 |
* found in the cache and the time-to-live periods of the instance and |
1301 |
* all of its parent instances have not expired, the instance is returned |
|
1302 |
* to the caller. Otherwise, this factory method proceeds with the |
|
1303 |
* loading process below.</li> |
|
1304 |
* |
|
1305 |
* <li>The {@link ResourceBundle.Control#getFormats(String) |
|
1306 |
* control.getFormats} method is called to get resource bundle formats |
|
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 | 1309 |
* designate class-based and {@linkplain PropertyResourceBundle |
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 | 1312 |
* and must not be used for application-defined formats. Other strings |
1313 |
* designate application-defined formats.</li> |
|
1314 |
* |
|
1315 |
* <li>The {@link ResourceBundle.Control#getCandidateLocales(String, |
|
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 | 1318 |
* which resource bundles are searched.</li> |
1319 |
* |
|
1320 |
* <li>The {@link ResourceBundle.Control#newBundle(String, Locale, |
|
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 | 1323 |
* candidate locale, and a format. (Refer to the note on the cache |
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 | 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 | 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 | 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 | 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 | 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 | 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 | 1348 |
* </tr> |
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 | 1353 |
* </tr> |
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 | 1358 |
* </tr> |
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 | 1363 |
* </tr> |
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 | 1373 |
* </tr> |
18829
ec84f0c313b0
8020409: Clean up doclint problems in java.util package, part 1
bpb
parents:
18156
diff
changeset
|
1374 |
* </tbody> |
2 | 1375 |
* </table> |
1376 |
* </li> |
|
1377 |
* |
|
1378 |
* <li>If the previous step has found no resource bundle, proceed to |
|
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 | 1382 |
* has been found that is a base bundle, but the candidate locale list |
1383 |
* contained locales other than Locale(""), put the bundle on hold and |
|
1384 |
* proceed to Step 6. If a bundle has been found that is not a base |
|
1385 |
* bundle, proceed to Step 7.</li> |
|
1386 |
* |
|
1387 |
* <li>The {@link ResourceBundle.Control#getFallbackLocale(String, |
|
1388 |
* Locale) control.getFallbackLocale} method is called to get a fallback |
|
1389 |
* locale (alternative to the current target locale) to try further |
|
1390 |
* finding a resource bundle. If the method returns a non-null locale, |
|
1391 |
* it becomes the next target locale and the loading process starts over |
|
1392 |
* from Step 3. Otherwise, if a base bundle was found and put on hold in |
|
1393 |
* a previous Step 5, it is returned to the caller now. Otherwise, a |
|
1394 |
* MissingResourceException is thrown.</li> |
|
1395 |
* |
|
1396 |
* <li>At this point, we have found a resource bundle that's not the |
|
1397 |
* base bundle. If this bundle set its parent during its instantiation, |
|
1398 |
* it is returned to the caller. Otherwise, its <a |
|
1399 |
* href="./ResourceBundle.html#parent_chain">parent chain</a> is |
|
1400 |
* instantiated based on the list of candidate locales from which it was |
|
1401 |
* found. Finally, the bundle is returned to the caller.</li> |
|
1402 |
* </ol> |
|
1403 |
* |
|
1404 |
* <p>During the resource bundle loading process above, this factory |
|
1405 |
* method looks up the cache before calling the {@link |
|
1406 |
* Control#newBundle(String, Locale, String, ClassLoader, boolean) |
|
1407 |
* control.newBundle} method. If the time-to-live period of the |
|
1408 |
* resource bundle found in the cache has expired, the factory method |
|
1409 |
* calls the {@link ResourceBundle.Control#needsReload(String, Locale, |
|
1410 |
* String, ClassLoader, ResourceBundle, long) control.needsReload} |
|
1411 |
* method to determine whether the resource bundle needs to be reloaded. |
|
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 | 1415 |
* method puts a dummy resource bundle in the cache as a mark of |
1416 |
* nonexistent resource bundles in order to avoid lookup overhead for |
|
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 | 1419 |
* |
1420 |
* <p>All resource bundles loaded are cached by default. Refer to |
|
1421 |
* {@link Control#getTimeToLive(String,Locale) |
|
1422 |
* control.getTimeToLive} for details. |
|
1423 |
* |
|
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 | 1426 |
* |
1427 |
* <p>Conditions: |
|
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 | 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 | 1435 |
* </ul> |
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 | 1438 |
* the following sequence. |
1439 |
* |
|
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 | 1447 |
* </ul> |
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 | 1452 |
* Control#getFallbackLocale(String, Locale) |
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 | 1455 |
* tries loading a bundle in the following sequence. |
1456 |
* |
|
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 | 1462 |
* </ul> |
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 | 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 | 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 | 1472 |
* |
1473 |
* @param baseName |
|
1474 |
* the base name of the resource bundle, a fully qualified |
|
1475 |
* class name |
|
1476 |
* @param targetLocale |
|
1477 |
* the locale for which a resource bundle is desired |
|
1478 |
* @param loader |
|
1479 |
* the class loader from which to load the resource bundle |
|
1480 |
* @param control |
|
1481 |
* the control which gives information for the resource |
|
1482 |
* bundle loading process |
|
1483 |
* @return a resource bundle for the given base name and locale |
|
36511 | 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 | 1488 |
* @throws MissingResourceException |
1489 |
* if no resource bundle for the specified base name can be found |
|
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 | 1494 |
* needed. |
1495 |
* @throws UnsupportedOperationException |
|
1496 |
* if this method is called in a named module |
|
2 | 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 | 1500 |
*/ |
36511 | 1501 |
@CallerSensitive |
2 | 1502 |
public static ResourceBundle getBundle(String baseName, Locale targetLocale, |
1503 |
ClassLoader loader, Control control) { |
|
1504 |
if (loader == null || control == null) { |
|
1505 |
throw new NullPointerException(); |
|
1506 |
} |
|
36511 | 1507 |
Class<?> caller = Reflection.getCallerClass(); |
1508 |
checkNamedModule(caller); |
|
1509 |
return getBundleImpl(baseName, targetLocale, caller, loader, control); |
|
2 | 1510 |
} |
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 | 1545 |
private static void checkNamedModule(Class<?> caller) { |
1546 |
if (caller.getModule().isNamed()) { |
|
1547 |
throw new UnsupportedOperationException( |
|
1548 |
"ResourceBundle.Control not supported in named modules"); |
|
1549 |
} |
|
1550 |
} |
|
1551 |
||
1552 |
private static ResourceBundle getBundleImpl(String baseName, |
|
1553 |
Locale locale, |
|
1554 |
Class<?> caller, |
|
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 | 1557 |
} |
1558 |
||
1559 |
/** |
|
1560 |
* This method will find resource bundles using the legacy mechanism |
|
1561 |
* if the caller is unnamed module or the given class loader is |
|
1562 |
* not the class loader of the caller module getting the resource |
|
1563 |
* bundle, i.e. find the class that is visible to the class loader |
|
1564 |
* and properties from unnamed module. |
|
1565 |
* |
|
1566 |
* The module-aware resource bundle lookup mechanism will load |
|
1567 |
* the service providers using the service loader mechanism |
|
1568 |
* as well as properties local in the caller module. |
|
1569 |
*/ |
|
1570 |
private static ResourceBundle getBundleImpl(String baseName, |
|
1571 |
Locale locale, |
|
1572 |
Class<?> caller, |
|
1573 |
ClassLoader loader, |
|
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 | 1594 |
} |
1595 |
||
1596 |
private static ResourceBundle getBundleFromModule(Class<?> caller, |
|
1597 |
Module module, |
|
1598 |
String baseName, |
|
1599 |
Locale locale, |
|
1600 |
Control control) { |
|
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 | 1604 |
SecurityManager sm = System.getSecurityManager(); |
1605 |
if (sm != null) { |
|
1606 |
sm.checkPermission(GET_CLASSLOADER_PERMISSION); |
|
1607 |
} |
|
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 | 1610 |
} |
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 | 1615 |
Locale locale, |
1616 |
Control control) { |
|
2 | 1617 |
if (locale == null || control == null) { |
1618 |
throw new NullPointerException(); |
|
1619 |
} |
|
1620 |
||
36511 | 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 | 1623 |
// process. We have to make sure that the locale is set before |
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 | 1626 |
ResourceBundle bundle = null; |
1627 |
||
1628 |
// Quick lookup of the cache. |
|
1629 |
BundleReference bundleRef = cacheList.get(cacheKey); |
|
1630 |
if (bundleRef != null) { |
|
1631 |
bundle = bundleRef.get(); |
|
1632 |
bundleRef = null; |
|
1633 |
} |
|
1634 |
||
1635 |
// If this bundle and all of its parents are valid (not expired), |
|
1636 |
// then return this bundle. If any of the bundles is expired, we |
|
1637 |
// don't call control.needsReload here but instead drop into the |
|
1638 |
// complete loading process below. |
|
1639 |
if (isValidBundle(bundle) && hasValidParentChain(bundle)) { |
|
1640 |
return bundle; |
|
1641 |
} |
|
1642 |
||
1643 |
// No valid bundle was found in the cache, so we need to load the |
|
1644 |
// resource bundle and its parents. |
|
1645 |
||
1646 |
boolean isKnownControl = (control == Control.INSTANCE) || |
|
1647 |
(control instanceof SingleFormatControl); |
|
1648 |
List<String> formats = control.getFormats(baseName); |
|
1649 |
if (!isKnownControl && !checkList(formats)) { |
|
1650 |
throw new IllegalArgumentException("Invalid Control: getFormats"); |
|
1651 |
} |
|
1652 |
||
1653 |
ResourceBundle baseBundle = null; |
|
1654 |
for (Locale targetLocale = locale; |
|
1655 |
targetLocale != null; |
|
1656 |
targetLocale = control.getFallbackLocale(baseName, targetLocale)) { |
|
1657 |
List<Locale> candidateLocales = control.getCandidateLocales(baseName, targetLocale); |
|
1658 |
if (!isKnownControl && !checkList(candidateLocales)) { |
|
1659 |
throw new IllegalArgumentException("Invalid Control: getCandidateLocales"); |
|
1660 |
} |
|
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 | 1664 |
|
1665 |
// If the loaded bundle is the base bundle and exactly for the |
|
1666 |
// requested locale or the only candidate locale, then take the |
|
1667 |
// bundle as the resulting one. If the loaded bundle is the base |
|
1668 |
// bundle, it's put on hold until we finish processing all |
|
1669 |
// fallback locales. |
|
1670 |
if (isValidBundle(bundle)) { |
|
1671 |
boolean isBaseBundle = Locale.ROOT.equals(bundle.locale); |
|
1672 |
if (!isBaseBundle || bundle.locale.equals(locale) |
|
1673 |
|| (candidateLocales.size() == 1 |
|
1674 |
&& bundle.locale.equals(candidateLocales.get(0)))) { |
|
1675 |
break; |
|
1676 |
} |
|
1677 |
||
1678 |
// If the base bundle has been loaded, keep the reference in |
|
1679 |
// baseBundle so that we can avoid any redundant loading in case |
|
1680 |
// the control specify not to cache bundles. |
|
1681 |
if (isBaseBundle && baseBundle == null) { |
|
1682 |
baseBundle = bundle; |
|
1683 |
} |
|
1684 |
} |
|
1685 |
} |
|
1686 |
||
1687 |
if (bundle == null) { |
|
1688 |
if (baseBundle == null) { |
|
1689 |
throwMissingResourceException(baseName, locale, cacheKey.getCause()); |
|
1690 |
} |
|
1691 |
bundle = baseBundle; |
|
1692 |
} |
|
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 | 1699 |
return bundle; |
1700 |
} |
|
1701 |
||
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 | 1704 |
* not having null in its elements. |
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 | 1708 |
if (valid) { |
1709 |
int size = a.size(); |
|
1710 |
for (int i = 0; valid && i < size; i++) { |
|
1711 |
valid = (a.get(i) != null); |
|
1712 |
} |
|
1713 |
} |
|
1714 |
return valid; |
|
1715 |
} |
|
1716 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1717 |
private static ResourceBundle findBundle(Module callerModule, |
36511 | 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 | 1725 |
Locale targetLocale = candidateLocales.get(index); |
1726 |
ResourceBundle parent = null; |
|
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 | 1730 |
control, baseBundle); |
1731 |
} else if (baseBundle != null && Locale.ROOT.equals(targetLocale)) { |
|
1732 |
return baseBundle; |
|
1733 |
} |
|
1734 |
||
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 | 1737 |
// resource bundles have been nulled out, remove all related |
1738 |
// information from the cache. |
|
1739 |
Object ref; |
|
1740 |
while ((ref = referenceQueue.poll()) != null) { |
|
1741 |
cacheList.remove(((CacheKeyReference)ref).getCacheKey()); |
|
1742 |
} |
|
1743 |
||
1744 |
// flag indicating the resource bundle has expired in the cache |
|
1745 |
boolean expiredBundle = false; |
|
1746 |
||
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 | 1749 |
cacheKey.setLocale(targetLocale); |
1750 |
ResourceBundle bundle = findBundleInCache(cacheKey, control); |
|
1751 |
if (isValidBundle(bundle)) { |
|
1752 |
expiredBundle = bundle.expired; |
|
1753 |
if (!expiredBundle) { |
|
1754 |
// If its parent is the one asked for by the candidate |
|
1755 |
// locales (the runtime lookup path), we can take the cached |
|
1756 |
// one. (If it's not identical, then we'd have to check the |
|
1757 |
// parent's parents to be consistent with what's been |
|
1758 |
// requested.) |
|
1759 |
if (bundle.parent == parent) { |
|
1760 |
return bundle; |
|
1761 |
} |
|
1762 |
// Otherwise, remove the cached one since we can't keep |
|
1763 |
// the same bundles having different parents. |
|
1764 |
BundleReference bundleRef = cacheList.get(cacheKey); |
|
1765 |
if (bundleRef != null && bundleRef.get() == bundle) { |
|
1766 |
cacheList.remove(cacheKey, bundleRef); |
|
1767 |
} |
|
1768 |
} |
|
1769 |
} |
|
1770 |
||
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 | 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 | 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 | 1790 |
} |
1791 |
return parent; |
|
1792 |
} |
|
1793 |
||
36511 | 1794 |
private static final String UNKNOWN_FORMAT = ""; |
1795 |
||
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1796 |
|
36511 | 1797 |
/* |
1798 |
* Loads a ResourceBundle in named modules |
|
1799 |
*/ |
|
1800 |
private static ResourceBundle loadBundle(CacheKey cacheKey, |
|
1801 |
List<String> formats, |
|
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 | 1805 |
String baseName = cacheKey.getName(); |
1806 |
Locale targetLocale = cacheKey.getLocale(); |
|
1807 |
||
1808 |
ResourceBundle bundle = null; |
|
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 | 1831 |
if (bundle != null) { |
1832 |
cacheKey.setFormat(UNKNOWN_FORMAT); |
|
1833 |
} |
|
1834 |
} |
|
42338
a60f280f803c
8169069: Module system implementation refresh (11/2016)
alanb
parents:
41482
diff
changeset
|
1835 |
|
36511 | 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 | 1838 |
if (bundle == null && !cacheKey.callerHasProvider()) { |
1839 |
for (String format : formats) { |
|
1840 |
try { |
|
1841 |
switch (format) { |
|
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 | 1846 |
break; |
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 | 1850 |
break; |
1851 |
default: |
|
1852 |
throw new InternalError("unexpected format: " + format); |
|
1853 |
} |
|
1854 |
||
1855 |
if (bundle != null) { |
|
1856 |
cacheKey.setFormat(format); |
|
1857 |
break; |
|
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 | 1860 |
cacheKey.setCause(e); |
1861 |
} |
|
1862 |
} |
|
1863 |
} |
|
1864 |
return bundle; |
|
1865 |
} |
|
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 | 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 | 1893 |
} |
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 | 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 | 1911 |
// Use the class loader of the getBundle caller so that the caller's |
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 | 1914 |
new PrivilegedAction<>() { |
1915 |
@Override |
|
1916 |
public Class<ResourceBundleProvider> run() { |
|
1917 |
try { |
|
1918 |
Class<?> c = Class.forName(providerName, false, loader); |
|
1919 |
if (ResourceBundleProvider.class.isAssignableFrom(c)) { |
|
1920 |
@SuppressWarnings("unchecked") |
|
1921 |
Class<ResourceBundleProvider> s = (Class<ResourceBundleProvider>) c; |
|
1922 |
return s; |
|
1923 |
} |
|
1924 |
} catch (ClassNotFoundException e) {} |
|
1925 |
return null; |
|
1926 |
} |
|
1927 |
}); |
|
1928 |
} |
|
1929 |
||
1930 |
/** |
|
1931 |
* Loads ResourceBundle from service providers. |
|
1932 |
*/ |
|
1933 |
private static ResourceBundle loadBundleFromProviders(String baseName, |
|
1934 |
Locale locale, |
|
1935 |
ServiceLoader<ResourceBundleProvider> providers, |
|
1936 |
CacheKey cacheKey) |
|
1937 |
{ |
|
1938 |
if (providers == null) return null; |
|
1939 |
||
1940 |
return AccessController.doPrivileged( |
|
1941 |
new PrivilegedAction<>() { |
|
1942 |
public ResourceBundle run() { |
|
1943 |
for (Iterator<ResourceBundleProvider> itr = providers.iterator(); itr.hasNext(); ) { |
|
1944 |
try { |
|
1945 |
ResourceBundleProvider provider = itr.next(); |
|
1946 |
if (cacheKey != null && cacheKey.callerHasProvider == null |
|
1947 |
&& cacheKey.getModule() == provider.getClass().getModule()) { |
|
1948 |
cacheKey.callerHasProvider = Boolean.TRUE; |
|
1949 |
} |
|
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 | 1952 |
if (bundle != null) { |
1953 |
return bundle; |
|
1954 |
} |
|
1955 |
} catch (ServiceConfigurationError | SecurityException e) { |
|
1956 |
if (cacheKey != null) { |
|
1957 |
cacheKey.setCause(e); |
|
1958 |
} |
|
1959 |
} |
|
1960 |
} |
|
1961 |
if (cacheKey != null && cacheKey.callerHasProvider == null) { |
|
1962 |
cacheKey.callerHasProvider = Boolean.FALSE; |
|
1963 |
} |
|
1964 |
return null; |
|
1965 |
} |
|
1966 |
}); |
|
1967 |
||
1968 |
} |
|
1969 |
||
1970 |
/* |
|
1971 |
* Legacy mechanism to load resource bundles |
|
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 | 1977 |
|
1978 |
// Here we actually load the bundle in the order of formats |
|
1979 |
// specified by the getFormats() value. |
|
1980 |
Locale targetLocale = cacheKey.getLocale(); |
|
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 | 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 | 1992 |
try { |
36511 | 1993 |
// ResourceBundle.Control.newBundle may be overridden |
2 | 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 | 1997 |
// We need to handle the LinkageError case due to |
1998 |
// inconsistent case-sensitivity in ClassLoader. |
|
1999 |
// See 6572242 for details. |
|
2000 |
cacheKey.setCause(error); |
|
2001 |
} |
|
2002 |
if (bundle != null) { |
|
2003 |
// Set the format in the cache key so that it can be |
|
2004 |
// used when calling needsReload later. |
|
2005 |
cacheKey.setFormat(format); |
|
2006 |
bundle.name = cacheKey.getName(); |
|
2007 |
bundle.locale = targetLocale; |
|
2008 |
// Bundle provider might reuse instances. So we should make |
|
2009 |
// sure to clear the expired flag here. |
|
2010 |
bundle.expired = false; |
|
2011 |
break; |
|
2012 |
} |
|
2013 |
} |
|
2014 |
||
2015 |
return bundle; |
|
2016 |
} |
|
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 | 2019 |
return bundle != null && bundle != NONEXISTENT_BUNDLE; |
2020 |
} |
|
2021 |
||
2022 |
/** |
|
2023 |
* Determines whether any of resource bundles in the parent chain, |
|
2024 |
* including the leaf, have expired. |
|
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 | 2027 |
long now = System.currentTimeMillis(); |
2028 |
while (bundle != null) { |
|
2029 |
if (bundle.expired) { |
|
2030 |
return false; |
|
2031 |
} |
|
2032 |
CacheKey key = bundle.cacheKey; |
|
2033 |
if (key != null) { |
|
2034 |
long expirationTime = key.expirationTime; |
|
2035 |
if (expirationTime >= 0 && expirationTime <= now) { |
|
2036 |
return false; |
|
2037 |
} |
|
2038 |
} |
|
2039 |
bundle = bundle.parent; |
|
2040 |
} |
|
2041 |
return true; |
|
2042 |
} |
|
2043 |
||
2044 |
/** |
|
2045 |
* Throw a MissingResourceException with proper message |
|
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 | 2050 |
// If the cause is a MissingResourceException, avoid creating |
2051 |
// a long chain. (6355009) |
|
2052 |
if (cause instanceof MissingResourceException) { |
|
2053 |
cause = null; |
|
2054 |
} |
|
2055 |
throw new MissingResourceException("Can't find bundle for base name " |
|
2056 |
+ baseName + ", locale " + locale, |
|
2057 |
baseName + "_" + locale, // className |
|
2058 |
"", // key |
|
2059 |
cause); |
|
2060 |
} |
|
2061 |
||
2062 |
/** |
|
2063 |
* Finds a bundle in the cache. Any expired bundles are marked as |
|
2064 |
* `expired' and removed from the cache upon return. |
|
2065 |
* |
|
2066 |
* @param cacheKey the key to look up the cache |
|
2067 |
* @param control the Control to be used for the expiration control |
|
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 | 2070 |
* upon return if the bundle in the cache has expired. |
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 | 2074 |
BundleReference bundleRef = cacheList.get(cacheKey); |
2075 |
if (bundleRef == null) { |
|
2076 |
return null; |
|
2077 |
} |
|
2078 |
ResourceBundle bundle = bundleRef.get(); |
|
2079 |
if (bundle == null) { |
|
2080 |
return null; |
|
2081 |
} |
|
2082 |
ResourceBundle p = bundle.parent; |
|
2083 |
assert p != NONEXISTENT_BUNDLE; |
|
2084 |
// If the parent has expired, then this one must also expire. We |
|
2085 |
// check only the immediate parent because the actual loading is |
|
2086 |
// done from the root (base) to leaf (child) and the purpose of |
|
2087 |
// checking is to propagate expiration towards the leaf. For |
|
2088 |
// example, if the requested locale is ja_JP_JP and there are |
|
2089 |
// bundles for all of the candidates in the cache, we have a list, |
|
2090 |
// |
|
2091 |
// base <- ja <- ja_JP <- ja_JP_JP |
|
2092 |
// |
|
2093 |
// If ja has expired, then it will reload ja and the list becomes a |
|
2094 |
// tree. |
|
2095 |
// |
|
2096 |
// base <- ja (new) |
|
2097 |
// " <- ja (expired) <- ja_JP <- ja_JP_JP |
|
2098 |
// |
|
2099 |
// When looking up ja_JP in the cache, it finds ja_JP in the cache |
|
2100 |
// which references to the expired ja. Then, ja_JP is marked as |
|
2101 |
// expired and removed from the cache. This will be propagated to |
|
2102 |
// ja_JP_JP. |
|
2103 |
// |
|
2104 |
// Now, it's possible, for example, that while loading new ja_JP, |
|
2105 |
// someone else has started loading the same bundle and finds the |
|
2106 |
// base bundle has expired. Then, what we get from the first |
|
2107 |
// getBundle call includes the expired base bundle. However, if |
|
2108 |
// someone else didn't start its loading, we wouldn't know if the |
|
2109 |
// base bundle has expired at the end of the loading process. The |
|
2110 |
// expiration control doesn't guarantee that the returned bundle and |
|
2111 |
// its parents haven't expired. |
|
2112 |
// |
|
2113 |
// We could check the entire parent chain to see if there's any in |
|
2114 |
// the chain that has expired. But this process may never end. An |
|
2115 |
// extreme case would be that getTimeToLive returns 0 and |
|
2116 |
// needsReload always returns true. |
|
2117 |
if (p != null && p.expired) { |
|
2118 |
assert bundle != NONEXISTENT_BUNDLE; |
|
2119 |
bundle.expired = true; |
|
2120 |
bundle.cacheKey = null; |
|
2121 |
cacheList.remove(cacheKey, bundleRef); |
|
2122 |
bundle = null; |
|
2123 |
} else { |
|
2124 |
CacheKey key = bundleRef.getCacheKey(); |
|
2125 |
long expirationTime = key.expirationTime; |
|
2126 |
if (!bundle.expired && expirationTime >= 0 && |
|
2127 |
expirationTime <= System.currentTimeMillis()) { |
|
2128 |
// its TTL period has expired. |
|
2129 |
if (bundle != NONEXISTENT_BUNDLE) { |
|
2130 |
// Synchronize here to call needsReload to avoid |
|
2131 |
// redundant concurrent calls for the same bundle. |
|
2132 |
synchronized (bundle) { |
|
2133 |
expirationTime = key.expirationTime; |
|
2134 |
if (!bundle.expired && expirationTime >= 0 && |
|
2135 |
expirationTime <= System.currentTimeMillis()) { |
|
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 | 2146 |
} catch (Exception e) { |
2147 |
cacheKey.setCause(e); |
|
2148 |
} |
|
2149 |
if (bundle.expired) { |
|
2150 |
// If the bundle needs to be reloaded, then |
|
2151 |
// remove the bundle from the cache, but |
|
2152 |
// return the bundle with the expired flag |
|
2153 |
// on. |
|
2154 |
bundle.cacheKey = null; |
|
2155 |
cacheList.remove(cacheKey, bundleRef); |
|
2156 |
} else { |
|
2157 |
// Update the expiration control info. and reuse |
|
2158 |
// the same bundle instance |
|
2159 |
setExpirationTime(key, control); |
|
2160 |
} |
|
2161 |
} |
|
2162 |
} |
|
2163 |
} else { |
|
2164 |
// We just remove NONEXISTENT_BUNDLE from the cache. |
|
2165 |
cacheList.remove(cacheKey, bundleRef); |
|
2166 |
bundle = null; |
|
2167 |
} |
|
2168 |
} |
|
2169 |
} |
|
2170 |
return bundle; |
|
2171 |
} |
|
2172 |
||
2173 |
/** |
|
2174 |
* Put a new bundle in the cache. |
|
2175 |
* |
|
2176 |
* @param cacheKey the key for the resource bundle |
|
2177 |
* @param bundle the resource bundle to be put in the cache |
|
2178 |
* @return the ResourceBundle for the cacheKey; if someone has put |
|
2179 |
* the bundle before this call, the one found in the cache is |
|
2180 |
* returned. |
|
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 | 2185 |
setExpirationTime(cacheKey, control); |
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 | 2188 |
BundleReference bundleRef = new BundleReference(bundle, referenceQueue, key); |
2189 |
bundle.cacheKey = key; |
|
2190 |
||
2191 |
// Put the bundle in the cache if it's not been in the cache. |
|
2192 |
BundleReference result = cacheList.putIfAbsent(key, bundleRef); |
|
2193 |
||
2194 |
// If someone else has put the same bundle in the cache before |
|
2195 |
// us and it has not expired, we should use the one in the cache. |
|
2196 |
if (result != null) { |
|
2197 |
ResourceBundle rb = result.get(); |
|
2198 |
if (rb != null && !rb.expired) { |
|
2199 |
// Clear the back link to the cache key |
|
2200 |
bundle.cacheKey = null; |
|
2201 |
bundle = rb; |
|
2202 |
// Clear the reference in the BundleReference so that |
|
2203 |
// it won't be enqueued. |
|
2204 |
bundleRef.clear(); |
|
2205 |
} else { |
|
2206 |
// Replace the invalid (garbage collected or expired) |
|
2207 |
// instance with the valid one. |
|
2208 |
cacheList.put(key, bundleRef); |
|
2209 |
} |
|
2210 |
} |
|
2211 |
} |
|
2212 |
return bundle; |
|
2213 |
} |
|
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 | 2216 |
long ttl = control.getTimeToLive(cacheKey.getName(), |
2217 |
cacheKey.getLocale()); |
|
2218 |
if (ttl >= 0) { |
|
2219 |
// If any expiration time is specified, set the time to be |
|
2220 |
// expired in the cache. |
|
2221 |
long now = System.currentTimeMillis(); |
|
2222 |
cacheKey.loadTime = now; |
|
2223 |
cacheKey.expirationTime = now + ttl; |
|
2224 |
} else if (ttl >= Control.TTL_NO_EXPIRATION_CONTROL) { |
|
2225 |
cacheKey.expirationTime = ttl; |
|
2226 |
} else { |
|
2227 |
throw new IllegalArgumentException("Invalid Control: TTL=" + ttl); |
|
2228 |
} |
|
2229 |
} |
|
2230 |
||
2231 |
/** |
|
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 | 2234 |
* |
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 | 2238 |
* @see ResourceBundle.Control#getTimeToLive(String,Locale) |
2239 |
*/ |
|
16906
44dfee24cb71
8010117: Annotate jdk caller sensitive methods with @sun.reflect.CallerSensitive
mchung
parents:
13029
diff
changeset
|
2240 |
@CallerSensitive |
2 | 2241 |
public static final void clearCache() { |
36511 | 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 | 2246 |
} |
2247 |
||
2248 |
/** |
|
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 | 2251 |
* |
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 | 2254 |
* @since 1.6 |
2255 |
* @see ResourceBundle.Control#getTimeToLive(String,Locale) |
|
2256 |
*/ |
|
2257 |
public static final void clearCache(ClassLoader loader) { |
|
36511 | 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 | 2266 |
} |
2267 |
||
2268 |
/** |
|
2269 |
* Gets an object for the given key from this resource bundle. |
|
2270 |
* Returns null if this resource bundle does not contain an |
|
2271 |
* object for the given key. |
|
2272 |
* |
|
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 | 2275 |
* @return the object for the given key, or null |
2276 |
*/ |
|
2277 |
protected abstract Object handleGetObject(String key); |
|
2278 |
||
2279 |
/** |
|
2280 |
* Returns an enumeration of the keys. |
|
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 | 2284 |
*/ |
2285 |
public abstract Enumeration<String> getKeys(); |
|
2286 |
||
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 | 2290 |
* |
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 | 2298 |
* @since 1.6 |
2299 |
*/ |
|
2300 |
public boolean containsKey(String key) { |
|
2301 |
if (key == null) { |
|
2302 |
throw new NullPointerException(); |
|
2303 |
} |
|
2304 |
for (ResourceBundle rb = this; rb != null; rb = rb.parent) { |
|
2305 |
if (rb.handleKeySet().contains(key)) { |
|
2306 |
return true; |
|
2307 |
} |
|
2308 |
} |
|
2309 |
return false; |
|
2310 |
} |
|
2311 |
||
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 | 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 | 2318 |
* @since 1.6 |
2319 |
*/ |
|
2320 |
public Set<String> keySet() { |
|
9224 | 2321 |
Set<String> keys = new HashSet<>(); |
2 | 2322 |
for (ResourceBundle rb = this; rb != null; rb = rb.parent) { |
2323 |
keys.addAll(rb.handleKeySet()); |
|
2324 |
} |
|
2325 |
return keys; |
|
2326 |
} |
|
2327 |
||
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 | 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 | 2333 |
* keys returned by the {@link #getKeys() getKeys} method except |
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 | 2339 |
* override this method for faster handling. |
2 | 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 | 2343 |
* @since 1.6 |
2344 |
*/ |
|
2345 |
protected Set<String> handleKeySet() { |
|
2346 |
if (keySet == null) { |
|
2347 |
synchronized (this) { |
|
2348 |
if (keySet == null) { |
|
9224 | 2349 |
Set<String> keys = new HashSet<>(); |
2 | 2350 |
Enumeration<String> enumKeys = getKeys(); |
2351 |
while (enumKeys.hasMoreElements()) { |
|
2352 |
String key = enumKeys.nextElement(); |
|
2353 |
if (handleGetObject(key) != null) { |
|
2354 |
keys.add(key); |
|
2355 |
} |
|
2356 |
} |
|
2357 |
keySet = keys; |
|
2358 |
} |
|
2359 |
} |
|
2360 |
} |
|
2361 |
return keySet; |
|
2362 |
} |
|
2363 |
||
2364 |
||
2365 |
||
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 | 2368 |
* that are invoked by the {@link ResourceBundle#getBundle(String, |
2369 |
* Locale, ClassLoader, Control) ResourceBundle.getBundle} factory |
|
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 | 2372 |
* methods for loading resource bundles. The default implementation of |
2373 |
* the callback methods provides the information necessary for the |
|
2374 |
* factory methods to perform the <a |
|
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 | 2384 |
* |
2385 |
* <p>In addition to the callback methods, the {@link |
|
2386 |
* #toBundleName(String, Locale) toBundleName} and {@link |
|
2387 |
* #toResourceName(String, String) toResourceName} methods are defined |
|
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 | 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 | 2393 |
* name separators. |
2394 |
* |
|
2395 |
* <p>Two factory methods, {@link #getControl(List)} and {@link |
|
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 | 2398 |
* variations of the default bundle loading process. |
2399 |
* |
|
2400 |
* <p>The formats returned by the {@link Control#getFormats(String) |
|
2401 |
* getFormats} method and candidate locales returned by the {@link |
|
2402 |
* ResourceBundle.Control#getCandidateLocales(String, Locale) |
|
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 | 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 | 2410 |
* second call will return the class-based one that has been cached |
2411 |
* during the first call. |
|
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 | 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 | 2417 |
* implementations of the methods are thread-safe. |
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 | 2422 |
* customize the bundle loading process. The following are examples of |
2423 |
* changing the default bundle loading process. |
|
2424 |
* |
|
2425 |
* <p><b>Example 1</b> |
|
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 | 2428 |
* up only properties-based resources. |
2429 |
* |
|
2430 |
* <pre> |
|
2431 |
* import java.util.*; |
|
2432 |
* import static java.util.ResourceBundle.Control.*; |
|
2433 |
* ... |
|
2434 |
* ResourceBundle bundle = |
|
2435 |
* ResourceBundle.getBundle("MyResources", new Locale("fr", "CH"), |
|
2436 |
* ResourceBundle.Control.getControl(FORMAT_PROPERTIES)); |
|
2437 |
* </pre> |
|
2438 |
* |
|
2439 |
* Given the resource bundles in the <a |
|
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 | 2447 |
* |
2448 |
* <p><b>Example 2</b> |
|
2449 |
* |
|
2450 |
* <p>The following is an example of loading XML-based bundles |
|
2451 |
* using {@link Properties#loadFromXML(java.io.InputStream) |
|
2452 |
* Properties.loadFromXML}. |
|
2453 |
* |
|
2454 |
* <pre> |
|
2455 |
* ResourceBundle rb = ResourceBundle.getBundle("Messages", |
|
2456 |
* new ResourceBundle.Control() { |
|
2457 |
* public List<String> getFormats(String baseName) { |
|
2458 |
* if (baseName == null) |
|
2459 |
* throw new NullPointerException(); |
|
2460 |
* return Arrays.asList("xml"); |
|
2461 |
* } |
|
2462 |
* public ResourceBundle newBundle(String baseName, |
|
2463 |
* Locale locale, |
|
2464 |
* String format, |
|
2465 |
* ClassLoader loader, |
|
2466 |
* boolean reload) |
|
2467 |
* throws IllegalAccessException, |
|
2468 |
* InstantiationException, |
|
2469 |
* IOException { |
|
2470 |
* if (baseName == null || locale == null |
|
2471 |
* || format == null || loader == null) |
|
2472 |
* throw new NullPointerException(); |
|
2473 |
* ResourceBundle bundle = null; |
|
2474 |
* if (format.equals("xml")) { |
|
2475 |
* String bundleName = toBundleName(baseName, locale); |
|
2476 |
* String resourceName = toResourceName(bundleName, format); |
|
2477 |
* InputStream stream = null; |
|
2478 |
* if (reload) { |
|
2479 |
* URL url = loader.getResource(resourceName); |
|
2480 |
* if (url != null) { |
|
2481 |
* URLConnection connection = url.openConnection(); |
|
2482 |
* if (connection != null) { |
|
2483 |
* // Disable caches to get fresh data for |
|
2484 |
* // reloading. |
|
2485 |
* connection.setUseCaches(false); |
|
2486 |
* stream = connection.getInputStream(); |
|
2487 |
* } |
|
2488 |
* } |
|
2489 |
* } else { |
|
2490 |
* stream = loader.getResourceAsStream(resourceName); |
|
2491 |
* } |
|
2492 |
* if (stream != null) { |
|
2493 |
* BufferedInputStream bis = new BufferedInputStream(stream); |
|
2494 |
* bundle = new XMLResourceBundle(bis); |
|
2495 |
* bis.close(); |
|
2496 |
* } |
|
2497 |
* } |
|
2498 |
* return bundle; |
|
2499 |
* } |
|
2500 |
* }); |
|
2501 |
* |
|
2502 |
* ... |
|
2503 |
* |
|
2504 |
* private static class XMLResourceBundle extends ResourceBundle { |
|
2505 |
* private Properties props; |
|
2506 |
* XMLResourceBundle(InputStream stream) throws IOException { |
|
2507 |
* props = new Properties(); |
|
2508 |
* props.loadFromXML(stream); |
|
2509 |
* } |
|
2510 |
* protected Object handleGetObject(String key) { |
|
2511 |
* return props.getProperty(key); |
|
2512 |
* } |
|
2513 |
* public Enumeration<String> getKeys() { |
|
2514 |
* ... |
|
2515 |
* } |
|
2516 |
* } |
|
2517 |
* </pre> |
|
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 | 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 | 2524 |
* |
2 | 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 | 2528 |
* @see java.util.spi.ResourceBundleProvider |
2 | 2529 |
*/ |
2530 |
public static class Control { |
|
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 | 2535 |
* |
2536 |
* @see #getFormats(String) |
|
2537 |
*/ |
|
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 | 2540 |
|
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 | 2544 |
* |
2545 |
* @see #getFormats(String) |
|
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 | 2548 |
|
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 | 2552 |
* |
2553 |
* @see #getFormats(String) |
|
2554 |
*/ |
|
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 | 2557 |
|
2558 |
/** |
|
2559 |
* The time-to-live constant for not caching loaded resource bundle |
|
2560 |
* instances. |
|
2561 |
* |
|
2562 |
* @see #getTimeToLive(String, Locale) |
|
2563 |
*/ |
|
2564 |
public static final long TTL_DONT_CACHE = -1; |
|
2565 |
||
2566 |
/** |
|
2567 |
* The time-to-live constant for disabling the expiration control |
|
2568 |
* for loaded resource bundle instances in the cache. |
|
2569 |
* |
|
2570 |
* @see #getTimeToLive(String, Locale) |
|
2571 |
*/ |
|
2572 |
public static final long TTL_NO_EXPIRATION_CONTROL = -2; |
|
2573 |
||
2574 |
private static final Control INSTANCE = new Control(); |
|
2575 |
||
2576 |
/** |
|
2577 |
* Sole constructor. (For invocation by subclass constructors, |
|
2578 |
* typically implicit.) |
|
2579 |
*/ |
|
2580 |
protected Control() { |
|
2581 |
} |
|
2582 |
||
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 | 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 | 2587 |
* one of {@link Control#FORMAT_PROPERTIES}, {@link |
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 | 2590 |
* instances returned by this method are singletons and thread-safe. |
2591 |
* |
|
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 | 2594 |
* except that this method returns a singleton. |
2595 |
* |
|
2596 |
* @param formats |
|
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 | 2605 |
*/ |
2606 |
public static final Control getControl(List<String> formats) { |
|
2607 |
if (formats.equals(Control.FORMAT_PROPERTIES)) { |
|
2608 |
return SingleFormatControl.PROPERTIES_ONLY; |
|
2609 |
} |
|
2610 |
if (formats.equals(Control.FORMAT_CLASS)) { |
|
2611 |
return SingleFormatControl.CLASS_ONLY; |
|
2612 |
} |
|
2613 |
if (formats.equals(Control.FORMAT_DEFAULT)) { |
|
2614 |
return Control.INSTANCE; |
|
2615 |
} |
|
2616 |
throw new IllegalArgumentException(); |
|
2617 |
} |
|
2618 |
||
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 | 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 | 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 | 2625 |
* be equal to one of {@link Control#FORMAT_PROPERTIES}, {@link |
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 | 2628 |
* method are singletons and thread-safe. |
2629 |
* |
|
2630 |
* @param formats |
|
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 | 2640 |
*/ |
2641 |
public static final Control getNoFallbackControl(List<String> formats) { |
|
2642 |
if (formats.equals(Control.FORMAT_DEFAULT)) { |
|
2643 |
return NoFallbackControl.NO_FALLBACK; |
|
2644 |
} |
|
2645 |
if (formats.equals(Control.FORMAT_PROPERTIES)) { |
|
2646 |
return NoFallbackControl.PROPERTIES_ONLY_NO_FALLBACK; |
|
2647 |
} |
|
2648 |
if (formats.equals(Control.FORMAT_CLASS)) { |
|
2649 |
return NoFallbackControl.CLASS_ONLY_NO_FALLBACK; |
|
2650 |
} |
|
2651 |
throw new IllegalArgumentException(); |
|
2652 |
} |
|
2653 |
||
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 | 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 | 2658 |
* factory method tries to load resource bundles with formats in the |
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 | 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 | 2665 |
* must not be used by application-defined formats. |
2666 |
* |
|
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 | 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 | 2671 |
* |
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 | 2674 |
* looks up first class-based resource bundles, then |
2675 |
* properties-based ones. |
|
2676 |
* |
|
2677 |
* @param baseName |
|
2678 |
* the base name of the resource bundle, a fully qualified class |
|
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 | 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 | 2684 |
* @see #FORMAT_DEFAULT |
2685 |
* @see #FORMAT_CLASS |
|
2686 |
* @see #FORMAT_PROPERTIES |
|
2687 |
*/ |
|
2688 |
public List<String> getFormats(String baseName) { |
|
2689 |
if (baseName == null) { |
|
2690 |
throw new NullPointerException(); |
|
2691 |
} |
|
2692 |
return FORMAT_DEFAULT; |
|
2693 |
} |
|
2694 |
||
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 | 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 | 2701 |
* |
2702 |
* <p>The sequence of the candidate locales also corresponds to the |
|
2703 |
* runtime resource lookup path (also known as the <I>parent |
|
2704 |
* chain</I>), if the corresponding resource bundles for the |
|
2705 |
* candidate locales exist and their parents are not defined by |
|
2706 |
* loaded resource bundles themselves. The last element of the list |
|
2707 |
* must be a {@linkplain Locale#ROOT root locale} if it is desired to |
|
2708 |
* have the base bundle as the terminal of the parent chain. |
|
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 | 2714 |
* the base bundle as the resulting resource bundle. |
2715 |
* |
|
6501 | 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 | 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 | 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 | 2723 |
* description below, <em>L</em>, <em>S</em>, <em>C</em> and <em>V</em> |
2724 |
* respectively represent non-empty language, script, country, and |
|
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 | 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 | 2729 |
* final component values are empty strings are omitted. |
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 | 2733 |
* one by one as below: |
2734 |
* |
|
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 | 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 | 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 | 2746 |
* |
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 | 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 | 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 | 2761 |
* full variant value in the original list. For example, if |
6501 | 2762 |
* the variant consists of two subtags <em>V1</em> and <em>V2</em>: |
2763 |
* |
|
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 | 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 | 2777 |
* language "zh" (Chinese) and an empty script value, either "Hans" (Simplified) or |
2778 |
* "Hant" (Traditional) might be supplied, depending on the country. |
|
2779 |
* When the country is "CN" (China) or "SG" (Singapore), "Hans" is supplied. |
|
2780 |
* When the country is "HK" (Hong Kong SAR China), "MO" (Macau SAR China), |
|
2781 |
* or "TW" (Taiwan), "Hant" is supplied. For all other countries or when the country |
|
2782 |
* is empty, no script is supplied. For example, for <code>Locale("zh", "CN") |
|
2783 |
* </code>, the candidate list will be: |
|
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 | 2790 |
* </ul> |
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 | 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 | 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 | 2803 |
* Nynorsk. When a locale's language is "nn", the standard candidate |
2804 |
* list is generated up to [<em>L</em>("nn")], and then the following |
|
2805 |
* candidates are added: |
|
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 | 2811 |
* </ul> |
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 | 2815 |
* followed. |
2816 |
* |
|
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å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 | 2821 |
* language code "no" and "nb" are interleaved, first using the |
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 | 2824 |
* candidate list: |
2825 |
* |
|
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 | 2834 |
* </ul> |
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 | 2837 |
* except that locales with "no" would appear before the corresponding |
2838 |
* locales with "nb".</li> |
|
2839 |
* </ol> |
|
2840 |
* |
|
2 | 2841 |
* <p>The default implementation uses an {@link ArrayList} that |
2842 |
* overriding implementations may modify before returning it to the |
|
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 | 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 | 2848 |
* <code>Locale("ja", "", "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 | 2850 |
* <pre> |
2851 |
* Locale("ja", "", "XX") |
|
2852 |
* Locale("ja") |
|
2853 |
* Locale.ROOT |
|
2854 |
* </pre> |
|
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 | 2857 |
* lookup path (parent chain) is: |
18156 | 2858 |
* <pre>{@code |
2 | 2859 |
* Messages_ja -> Messages |
18156 | 2860 |
* }</pre> |
2 | 2861 |
* |
2862 |
* @param baseName |
|
2863 |
* the base name of the resource bundle, a fully |
|
2864 |
* qualified class name |
|
2865 |
* @param locale |
|
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 | 2872 |
*/ |
2873 |
public List<Locale> getCandidateLocales(String baseName, Locale locale) { |
|
2874 |
if (baseName == null) { |
|
2875 |
throw new NullPointerException(); |
|
2876 |
} |
|
9224 | 2877 |
return new ArrayList<>(CANDIDATES_CACHE.get(locale.getBaseLocale())); |
6501 | 2878 |
} |
2879 |
||
2880 |
private static final CandidateListCache CANDIDATES_CACHE = new CandidateListCache(); |
|
2881 |
||
2882 |
private static class CandidateListCache extends LocaleObjectCache<BaseLocale, List<Locale>> { |
|
2883 |
protected List<Locale> createObject(BaseLocale base) { |
|
2884 |
String language = base.getLanguage(); |
|
2885 |
String script = base.getScript(); |
|
2886 |
String region = base.getRegion(); |
|
2887 |
String variant = base.getVariant(); |
|
2 | 2888 |
|
6501 | 2889 |
// Special handling for Norwegian |
2890 |
boolean isNorwegianBokmal = false; |
|
2891 |
boolean isNorwegianNynorsk = false; |
|
2892 |
if (language.equals("no")) { |
|
2893 |
if (region.equals("NO") && variant.equals("NY")) { |
|
2894 |
variant = ""; |
|
2895 |
isNorwegianNynorsk = true; |
|
2896 |
} else { |
|
2897 |
isNorwegianBokmal = true; |
|
2898 |
} |
|
2899 |
} |
|
2900 |
if (language.equals("nb") || isNorwegianBokmal) { |
|
2901 |
List<Locale> tmpList = getDefaultList("nb", script, region, variant); |
|
2902 |
// Insert a locale replacing "nb" with "no" for every list entry |
|
9224 | 2903 |
List<Locale> bokmalList = new LinkedList<>(); |
6501 | 2904 |
for (Locale l : tmpList) { |
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 | 2907 |
break; |
2908 |
} |
|
2909 |
bokmalList.add(Locale.getInstance("no", l.getScript(), l.getCountry(), |
|
9224 | 2910 |
l.getVariant(), null)); |
6501 | 2911 |
} |
2912 |
return bokmalList; |
|
2913 |
} else if (language.equals("nn") || isNorwegianNynorsk) { |
|
2914 |
// Insert no_NO_NY, no_NO, no after nn |
|
2915 |
List<Locale> nynorskList = getDefaultList("nn", script, region, variant); |
|
2916 |
int idx = nynorskList.size() - 1; |
|
2917 |
nynorskList.add(idx++, Locale.getInstance("no", "NO", "NY")); |
|
2918 |
nynorskList.add(idx++, Locale.getInstance("no", "NO", "")); |
|
2919 |
nynorskList.add(idx++, Locale.getInstance("no", "", "")); |
|
2920 |
return nynorskList; |
|
2921 |
} |
|
2922 |
// Special handling for Chinese |
|
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 | 2925 |
// Supply script for users who want to use zh_Hans/zh_Hant |
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 | 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 | 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 | 2937 |
} |
2938 |
} |
|
2939 |
} |
|
2940 |
||
2941 |
return getDefaultList(language, script, region, variant); |
|
2 | 2942 |
} |
6501 | 2943 |
|
2944 |
private static List<Locale> getDefaultList(String language, String script, String region, String variant) { |
|
2945 |
List<String> variants = null; |
|
2946 |
||
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52766
diff
changeset
|
2947 |
if (!variant.isEmpty()) { |
9224 | 2948 |
variants = new LinkedList<>(); |
6501 | 2949 |
int idx = variant.length(); |
2950 |
while (idx != -1) { |
|
2951 |
variants.add(variant.substring(0, idx)); |
|
2952 |
idx = variant.lastIndexOf('_', --idx); |
|
2953 |
} |
|
2954 |
} |
|
2955 |
||
9224 | 2956 |
List<Locale> list = new LinkedList<>(); |
6501 | 2957 |
|
2958 |
if (variants != null) { |
|
2959 |
for (String v : variants) { |
|
9224 | 2960 |
list.add(Locale.getInstance(language, script, region, v, null)); |
6501 | 2961 |
} |
2962 |
} |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52766
diff
changeset
|
2963 |
if (!region.isEmpty()) { |
9224 | 2964 |
list.add(Locale.getInstance(language, script, region, "", null)); |
6501 | 2965 |
} |
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52766
diff
changeset
|
2966 |
if (!script.isEmpty()) { |
9224 | 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 | 2983 |
|
2984 |
// With script, after truncating variant, region and script, |
|
2985 |
// start over without script. |
|
2986 |
if (variants != null) { |
|
2987 |
for (String v : variants) { |
|
9224 | 2988 |
list.add(Locale.getInstance(language, "", region, v, null)); |
6501 | 2989 |
} |
2990 |
} |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52766
diff
changeset
|
2991 |
if (!region.isEmpty()) { |
9224 | 2992 |
list.add(Locale.getInstance(language, "", region, "", null)); |
6501 | 2993 |
} |
2994 |
} |
|
53018
8bf9268df0e2
8215281: Use String.isEmpty() when applicable in java.base
redestad
parents:
52766
diff
changeset
|
2995 |
if (!language.isEmpty()) { |
9224 | 2996 |
list.add(Locale.getInstance(language, "", "", "", null)); |
6501 | 2997 |
} |
2998 |
// Add root locale at the end |
|
2999 |
list.add(Locale.ROOT); |
|
3000 |
||
3001 |
return list; |
|
2 | 3002 |
} |
3003 |
} |
|
3004 |
||
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 | 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 | 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 | 3013 |
* locale returned by this method. |
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 | 3016 |
* search is desired. |
3017 |
* |
|
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 | 3022 |
* |
3023 |
* @param baseName |
|
3024 |
* the base name of the resource bundle, a fully |
|
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 | 3027 |
* unable to find any resource bundles (except for the |
3028 |
* base bundle) |
|
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 | 3032 |
* unable to find any resource bundles (except for the |
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 | 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 | 3040 |
*/ |
3041 |
public Locale getFallbackLocale(String baseName, Locale locale) { |
|
3042 |
if (baseName == null) { |
|
3043 |
throw new NullPointerException(); |
|
3044 |
} |
|
3045 |
Locale defaultLocale = Locale.getDefault(); |
|
3046 |
return locale.equals(defaultLocale) ? null : defaultLocale; |
|
3047 |
} |
|
3048 |
||
3049 |
/** |
|
3050 |
* Instantiates a resource bundle for the given bundle name of the |
|
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 | 3053 |
* resource bundle available for the given parameters. If a resource |
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 | 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 | 3060 |
* indicates that this method is being called because the previously |
3061 |
* loaded resource bundle has expired. |
|
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 | 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 | 3073 |
* |
3074 |
* <ul> |
|
3075 |
* |
|
3076 |
* <li>The bundle name is obtained by calling {@link |
|
3077 |
* #toBundleName(String, Locale) toBundleName(baseName, |
|
3078 |
* locale)}.</li> |
|
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 | 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 | 3092 |
* {@link #toResourceName(String, String) toResourceName(bundlename, |
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 | 3095 |
* ClassLoader#getResource(String) load.getResource} is called |
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 | 3098 |
* {@linkplain URLConnection#setUseCaches(boolean) disable the |
3099 |
* caches} of the underlying resource loading layers, |
|
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 | 3102 |
* Otherwise, {@link ClassLoader#getResourceAsStream(String) |
3103 |
* loader.getResourceAsStream} is called to get an {@link |
|
3104 |
* InputStream}. Then, a {@link |
|
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 | 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 | 3111 |
* |
3112 |
* </ul> |
|
3113 |
* |
|
3114 |
* @param baseName |
|
3115 |
* the base bundle name of the resource bundle, a fully |
|
3116 |
* qualified class name |
|
3117 |
* @param locale |
|
3118 |
* the locale for which the resource bundle should be |
|
3119 |
* instantiated |
|
3120 |
* @param format |
|
3121 |
* the resource bundle format to be loaded |
|
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 | 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 | 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 | 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 | 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 | 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 | 3141 |
* if the class or its nullary constructor is not |
3142 |
* accessible. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54206
diff
changeset
|
3143 |
* @throws InstantiationException |
2 | 3144 |
* if the instantiation of a class fails for some other |
3145 |
* reason. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54206
diff
changeset
|
3146 |
* @throws ExceptionInInitializerError |
2 | 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 | 3149 |
* If a security manager is present and creation of new |
3150 |
* instances is denied. See {@link Class#newInstance()} |
|
3151 |
* for details. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
54206
diff
changeset
|
3152 |
* @throws IOException |
2 | 3153 |
* if an error occurred when reading resources using |
3154 |
* any I/O operations |
|
36511 | 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 | 3158 |
*/ |
3159 |
public ResourceBundle newBundle(String baseName, Locale locale, String format, |
|
3160 |
ClassLoader loader, boolean reload) |
|
3161 |
throws IllegalAccessException, InstantiationException, IOException { |
|
36511 | 3162 |
/* |
3163 |
* Legacy mechanism to locate resource bundle in unnamed module only |
|
3164 |
* that is visible to the given loader and accessible to the given caller. |
|
3165 |
*/ |
|
2 | 3166 |
String bundleName = toBundleName(baseName, locale); |
3167 |
ResourceBundle bundle = null; |
|
3168 |
if (format.equals("java.class")) { |
|
3169 |
try { |
|
36511 | 3170 |
Class<?> c = loader.loadClass(bundleName); |
2 | 3171 |
// If the class isn't a ResourceBundle subclass, throw a |
3172 |
// ClassCastException. |
|
36511 | 3173 |
if (ResourceBundle.class.isAssignableFrom(c)) { |
3174 |
@SuppressWarnings("unchecked") |
|
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 | 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 | 3185 |
} |
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 | 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 | 3197 |
} |
3198 |
||
3199 |
// java.base may not be able to read the bundleClass's module. |
|
3200 |
PrivilegedAction<Void> pa1 = () -> { ctor.setAccessible(true); return null; }; |
|
3201 |
AccessController.doPrivileged(pa1); |
|
3202 |
bundle = ctor.newInstance((Object[]) null); |
|
3203 |
} catch (InvocationTargetException e) { |
|
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 | 3209 |
} |
2 | 3210 |
} else { |
36511 | 3211 |
throw new ClassCastException(c.getName() |
3212 |
+ " cannot be cast to ResourceBundle"); |
|
2 | 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 | 3215 |
} |
3216 |
} else if (format.equals("java.properties")) { |
|
27083 | 3217 |
final String resourceName = toResourceName0(bundleName, "properties"); |
3218 |
if (resourceName == null) { |
|
3219 |
return bundle; |
|
3220 |
} |
|
36511 | 3221 |
|
2 | 3222 |
final boolean reloadFlag = reload; |
3223 |
InputStream stream = null; |
|
3224 |
try { |
|
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 | 3227 |
public InputStream run() throws IOException { |
36511 | 3228 |
URL url = loader.getResource(resourceName); |
3229 |
if (url == null) return null; |
|
3230 |
||
3231 |
URLConnection connection = url.openConnection(); |
|
2 | 3232 |
if (reloadFlag) { |
36511 | 3233 |
// Disable caches to get fresh data for |
3234 |
// reloading. |
|
3235 |
connection.setUseCaches(false); |
|
2 | 3236 |
} |
36511 | 3237 |
return connection.getInputStream(); |
2 | 3238 |
} |
3239 |
}); |
|
3240 |
} catch (PrivilegedActionException e) { |
|
3241 |
throw (IOException) e.getException(); |
|
3242 |
} |
|
3243 |
if (stream != null) { |
|
3244 |
try { |
|
3245 |
bundle = new PropertyResourceBundle(stream); |
|
3246 |
} finally { |
|
3247 |
stream.close(); |
|
3248 |
} |
|
3249 |
} |
|
3250 |
} else { |
|
3251 |
throw new IllegalArgumentException("unknown format: " + format); |
|
3252 |
} |
|
3253 |
return bundle; |
|
3254 |
} |
|
3255 |
||
3256 |
/** |
|
3257 |
* Returns the time-to-live (TTL) value for resource bundles that |
|
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 | 3260 |
* specify the number of milliseconds a bundle can remain in the |
3261 |
* cache without being validated against the source data from which |
|
3262 |
* it was constructed. The value 0 indicates that a bundle must be |
|
3263 |
* validated each time it is retrieved from the cache. {@link |
|
3264 |
* #TTL_DONT_CACHE} specifies that loaded resource bundles are not |
|
3265 |
* put in the cache. {@link #TTL_NO_EXPIRATION_CONTROL} specifies |
|
3266 |
* that loaded resource bundles are put in the cache with no |
|
3267 |
* expiration control. |
|
3268 |
* |
|
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 | 3271 |
* if the factory method finds a resource bundle in the cache that |
3272 |
* has expired, the factory method calls the {@link |
|
3273 |
* #needsReload(String, Locale, String, ClassLoader, ResourceBundle, |
|
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 | 3277 |
* from the cache. Otherwise, the instance stays in the cache, |
3278 |
* updated with the new TTL value returned by this method. |
|
3279 |
* |
|
3280 |
* <p>All cached resource bundles are subject to removal from the |
|
3281 |
* cache due to memory constraints of the runtime environment. |
|
3282 |
* Returning a large positive value doesn't mean to lock loaded |
|
3283 |
* resource bundles in the cache. |
|
3284 |
* |
|
3285 |
* <p>The default implementation returns {@link #TTL_NO_EXPIRATION_CONTROL}. |
|
3286 |
* |
|
3287 |
* @param baseName |
|
3288 |
* the base name of the resource bundle for which the |
|
3289 |
* expiration value is specified. |
|
3290 |
* @param locale |
|
3291 |
* the locale of the resource bundle for which the |
|
3292 |
* expiration value is specified. |
|
3293 |
* @return the time (0 or a positive millisecond offset from the |
|
3294 |
* cached time) to get loaded bundles expired in the cache, |
|
3295 |
* {@link #TTL_NO_EXPIRATION_CONTROL} to disable the |
|
3296 |
* expiration control, or {@link #TTL_DONT_CACHE} to disable |
|
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 | 3301 |
*/ |
3302 |
public long getTimeToLive(String baseName, Locale locale) { |
|
3303 |
if (baseName == null || locale == null) { |
|
3304 |
throw new NullPointerException(); |
|
3305 |
} |
|
3306 |
return TTL_NO_EXPIRATION_CONTROL; |
|
3307 |
} |
|
3308 |
||
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 | 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 | 3316 |
* Epoch</a>. |
3317 |
* |
|
36511 | 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 | 3321 |
* instance used for its current invocation, not on the instance |
3322 |
* used in the invocation that originally loaded the resource |
|
3323 |
* bundle. |
|
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 | 3326 |
* the last modified time of the source data of the resource |
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 | 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 | 3334 |
* |
3335 |
* @param baseName |
|
3336 |
* the base bundle name of the resource bundle, a |
|
3337 |
* fully qualified class name |
|
3338 |
* @param locale |
|
3339 |
* the locale for which the resource bundle |
|
3340 |
* should be instantiated |
|
3341 |
* @param format |
|
3342 |
* the resource bundle format to be loaded |
|
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 | 3345 |
* @param bundle |
3346 |
* the resource bundle instance that has been expired |
|
3347 |
* in the cache |
|
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 | 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 | 3357 |
*/ |
3358 |
public boolean needsReload(String baseName, Locale locale, |
|
3359 |
String format, ClassLoader loader, |
|
3360 |
ResourceBundle bundle, long loadTime) { |
|
3361 |
if (bundle == null) { |
|
3362 |
throw new NullPointerException(); |
|
3363 |
} |
|
3364 |
if (format.equals("java.class") || format.equals("java.properties")) { |
|
3365 |
format = format.substring(5); |
|
3366 |
} |
|
3367 |
boolean result = false; |
|
3368 |
try { |
|
27083 | 3369 |
String resourceName = toResourceName0(toBundleName(baseName, locale), format); |
3370 |
if (resourceName == null) { |
|
3371 |
return result; |
|
3372 |
} |
|
2 | 3373 |
URL url = loader.getResource(resourceName); |
3374 |
if (url != null) { |
|
3375 |
long lastModified = 0; |
|
3376 |
URLConnection connection = url.openConnection(); |
|
3377 |
if (connection != null) { |
|
3378 |
// disable caches to get the correct data |
|
3379 |
connection.setUseCaches(false); |
|
3380 |
if (connection instanceof JarURLConnection) { |
|
3381 |
JarEntry ent = ((JarURLConnection)connection).getJarEntry(); |
|
3382 |
if (ent != null) { |
|
3383 |
lastModified = ent.getTime(); |
|
3384 |
if (lastModified == -1) { |
|
3385 |
lastModified = 0; |
|
3386 |
} |
|
3387 |
} |
|
3388 |
} else { |
|
3389 |
lastModified = connection.getLastModified(); |
|
3390 |
} |
|
3391 |
} |
|
3392 |
result = lastModified >= loadTime; |
|
3393 |
} |
|
3394 |
} catch (NullPointerException npe) { |
|
3395 |
throw npe; |
|
3396 |
} catch (Exception e) { |
|
3397 |
// ignore other exceptions |
|
3398 |
} |
|
3399 |
return result; |
|
3400 |
} |
|
3401 |
||
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 | 3404 |
* to the bundle name. This method is called from the default |
3405 |
* implementation of the {@link #newBundle(String, Locale, String, |
|
3406 |
* ClassLoader, boolean) newBundle} and {@link #needsReload(String, |
|
3407 |
* Locale, String, ClassLoader, ResourceBundle, long) needsReload} |
|
3408 |
* methods. |
|
3409 |
* |
|
3410 |
* <p>This implementation returns the following value: |
|
3411 |
* <pre> |
|
6501 | 3412 |
* baseName + "_" + language + "_" + script + "_" + country + "_" + variant |
2 | 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 | 3417 |
* are empty Strings are omitted along with the preceding '_'. When the |
21278 | 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 | 3420 |
* is returned. |
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 | 3424 |
* <code>Locale("ja", "", "XX")</code>, then |
3425 |
* <code>"baseName_ja_ _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 | 3428 |
* |
3429 |
* <p>Overriding this method allows applications to use different |
|
3430 |
* conventions in the organization and packaging of localized |
|
3431 |
* resources. |
|
3432 |
* |
|
3433 |
* @param baseName |
|
3434 |
* the base name of the resource bundle, a fully |
|
3435 |
* qualified class name |
|
3436 |
* @param locale |
|
3437 |
* the locale for which a resource bundle should be |
|
3438 |
* loaded |
|
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 | 3443 |
* @see java.util.spi.AbstractResourceBundleProvider#toBundleName(String, Locale) |
2 | 3444 |
*/ |
3445 |
public String toBundleName(String baseName, Locale locale) { |
|
3446 |
if (locale == Locale.ROOT) { |
|
3447 |
return baseName; |
|
3448 |
} |
|
3449 |
||
3450 |
String language = locale.getLanguage(); |
|
6501 | 3451 |
String script = locale.getScript(); |
2 | 3452 |
String country = locale.getCountry(); |
3453 |
String variant = locale.getVariant(); |
|
3454 |
||
3455 |
if (language == "" && country == "" && variant == "") { |
|
3456 |
return baseName; |
|
3457 |
} |
|
3458 |
||
3459 |
StringBuilder sb = new StringBuilder(baseName); |
|
3460 |
sb.append('_'); |
|
6501 | 3461 |
if (script != "") { |
3462 |
if (variant != "") { |
|
3463 |
sb.append(language).append('_').append(script).append('_').append(country).append('_').append(variant); |
|
3464 |
} else if (country != "") { |
|
3465 |
sb.append(language).append('_').append(script).append('_').append(country); |
|
3466 |
} else { |
|
3467 |
sb.append(language).append('_').append(script); |
|
3468 |
} |
|
2 | 3469 |
} else { |
6501 | 3470 |
if (variant != "") { |
3471 |
sb.append(language).append('_').append(country).append('_').append(variant); |
|
3472 |
} else if (country != "") { |
|
3473 |
sb.append(language).append('_').append(country); |
|
3474 |
} else { |
|
3475 |
sb.append(language); |
|
3476 |
} |
|
2 | 3477 |
} |
3478 |
return sb.toString(); |
|
3479 |
||
3480 |
} |
|
3481 |
||
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 | 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 | 3492 |
* |
3493 |
* @param bundleName |
|
3494 |
* the bundle name |
|
3495 |
* @param suffix |
|
3496 |
* the file type suffix |
|
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 | 3501 |
*/ |
3502 |
public final String toResourceName(String bundleName, String suffix) { |
|
3503 |
StringBuilder sb = new StringBuilder(bundleName.length() + 1 + suffix.length()); |
|
3504 |
sb.append(bundleName.replace('.', '/')).append('.').append(suffix); |
|
3505 |
return sb.toString(); |
|
3506 |
} |
|
27083 | 3507 |
|
3508 |
private String toResourceName0(String bundleName, String suffix) { |
|
3509 |
// application protocol check |
|
3510 |
if (bundleName.contains("://")) { |
|
3511 |
return null; |
|
3512 |
} else { |
|
3513 |
return toResourceName(bundleName, suffix); |
|
3514 |
} |
|
3515 |
} |
|
2 | 3516 |
} |
3517 |
||
36511 | 3518 |
@SuppressWarnings("unchecked") |
3519 |
private static <T extends Throwable> void uncheckedThrow(Throwable t) throws T { |
|
3520 |
if (t != null) |
|
3521 |
throw (T)t; |
|
3522 |
else |
|
3523 |
throw new Error("Unknown Exception"); |
|
3524 |
} |
|
3525 |
||
2 | 3526 |
private static class SingleFormatControl extends Control { |
3527 |
private static final Control PROPERTIES_ONLY |
|
3528 |
= new SingleFormatControl(FORMAT_PROPERTIES); |
|
3529 |
||
3530 |
private static final Control CLASS_ONLY |
|
3531 |
= new SingleFormatControl(FORMAT_CLASS); |
|
3532 |
||
3533 |
private final List<String> formats; |
|
3534 |
||
3535 |
protected SingleFormatControl(List<String> formats) { |
|
3536 |
this.formats = formats; |
|
3537 |
} |
|
3538 |
||
3539 |
public List<String> getFormats(String baseName) { |
|
3540 |
if (baseName == null) { |
|
3541 |
throw new NullPointerException(); |
|
3542 |
} |
|
3543 |
return formats; |
|
3544 |
} |
|
3545 |
} |
|
3546 |
||
3547 |
private static final class NoFallbackControl extends SingleFormatControl { |
|
3548 |
private static final Control NO_FALLBACK |
|
3549 |
= new NoFallbackControl(FORMAT_DEFAULT); |
|
3550 |
||
3551 |
private static final Control PROPERTIES_ONLY_NO_FALLBACK |
|
3552 |
= new NoFallbackControl(FORMAT_PROPERTIES); |
|
3553 |
||
3554 |
private static final Control CLASS_ONLY_NO_FALLBACK |
|
3555 |
= new NoFallbackControl(FORMAT_CLASS); |
|
3556 |
||
3557 |
protected NoFallbackControl(List<String> formats) { |
|
3558 |
super(formats); |
|
3559 |
} |
|
3560 |
||
3561 |
public Locale getFallbackLocale(String baseName, Locale locale) { |
|
3562 |
if (baseName == null || locale == null) { |
|
3563 |
throw new NullPointerException(); |
|
3564 |
} |
|
3565 |
return null; |
|
3566 |
} |
|
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 | 3737 |
} |