202 * if it's simultaneously used by multiple threads. The default implementations |
202 * if it's simultaneously used by multiple threads. The default implementations |
203 * of the non-abstract methods in this class, and the methods in the direct |
203 * of the non-abstract methods in this class, and the methods in the direct |
204 * known concrete subclasses {@code ListResourceBundle} and |
204 * known concrete subclasses {@code ListResourceBundle} and |
205 * {@code PropertyResourceBundle} are thread-safe. |
205 * {@code PropertyResourceBundle} are thread-safe. |
206 * |
206 * |
207 * <h3><a id="bundleprovider">Resource Bundles in Named Modules</a></h3> |
207 * <h3><a id="resource-bundle-modules">Resource Bundles and Named Modules</a></h3> |
208 * |
208 * |
209 * When resource bundles are deployed in named modules, the following |
209 * Resource bundles can be deployed in modules in the following ways: |
210 * module-specific requirements and restrictions are applied. |
210 * |
211 * |
211 * <h4>Resource bundles together with an application</h4> |
212 * <ul> |
212 * |
213 * <li>Code in a named module that calls {@link #getBundle(String, Locale)} |
213 * Resource bundles can be deployed together with an application in the same |
214 * will locate resource bundles in the caller's module (<em>caller module</em>).</li> |
214 * module. In that case, the resource bundles are loaded |
215 * <li>If resource bundles are deployed in named modules separate from |
215 * by code in the module by calling the {@link #getBundle(String)} |
216 * the caller module, those resource bundles need to be loaded from service |
216 * or {@link #getBundle(String, Locale)} method. |
217 * providers of {@link ResourceBundleProvider}. The caller module must declare |
217 * |
218 * "{@code uses}" and the service interface name is the concatenation of the |
218 * <h4><a id="service-providers">Resource bundles as service providers</a></h4> |
219 * package name of the base name, string "{@code .spi.}", the simple class |
219 * |
220 * name of the base name, and the string "{@code Provider}". The |
220 * Resource bundles can be deployed in one or more <em>service provider modules</em> |
221 * <em>bundle provider modules</em> containing resource bundles must |
221 * and they can be located using {@link ServiceLoader}. |
222 * declare "{@code provides}" with the service interface name and |
222 * A {@linkplain ResourceBundleProvider service} interface or class must be |
223 * its implementation class name. For example, if the base name is |
223 * defined. The caller module declares that it uses the service, the service |
224 * "{@code com.example.app.MyResources}", the caller module must declare |
224 * provider modules declare that they provide implementations of the service. |
225 * "{@code uses com.example.app.spi.MyResourcesProvider;}" and a module containing resource |
225 * Refer to {@link ResourceBundleProvider} for developing resource bundle |
226 * bundles must declare "{@code provides com.example.app.spi.MyResourcesProvider |
226 * services and deploying resource bundle providers. |
227 * with com.example.app.internal.MyResourcesProviderImpl;}" |
227 * The module obtaining the resource bundle can be a resource bundle |
228 * where {@code com.example.app.internal.MyResourcesProviderImpl} is an |
228 * provider itself; in which case this module only locates the resource bundle |
229 * implementation class of {@code com.example.app.spi.MyResourcesProvider}.</li> |
229 * via service provider mechanism. |
230 * <li>If you want to use non-standard formats in named modules, such as XML, |
230 * |
231 * {@link ResourceBundleProvider} needs to be used.</li> |
231 * <p>A {@linkplain ResourceBundleProvider resource bundle provider} can |
232 * <li>The {@code getBundle} method with a {@code ClassLoader} may not be able to |
232 * provide resource bundles in any format such XML which replaces the need |
233 * find resource bundles using the given {@code ClassLoader} in named modules. |
233 * of {@link Control ResourceBundle.Control}. |
234 * The {@code getBundle} method with a {@code Module} can be used, instead.</li> |
234 * |
235 * <li>{@code ResourceBundle.Control} is <em>not</em> supported in named modules. |
235 * <h4><a id="other-modules">Resource bundles in other modules and class path</a></h4> |
236 * If the {@code getBundle} method with a {@code ResourceBundle.Control} is called |
236 * |
237 * in a named module, the method will throw an {@code UnsupportedOperationException}. |
237 * Resource bundles in a named module may be <em>encapsulated</em> so that |
238 * Any service providers of {@link ResourceBundleControlProvider} are ignored in |
238 * it cannot be located by code in other modules. Resource bundles |
239 * named modules. |
239 * in unnamed modules and class path are open for any module to access. |
240 * </li> |
240 * Resource bundle follows the resource encapsulation rules as specified |
241 * </ul> |
241 * in {@link Module#getResourceAsStream(String)}. |
242 * |
242 * |
243 * <h3><a id="RBP_support">ResourceBundleProvider Service Providers</a></h3> |
243 * <p>The {@code getBundle} factory methods with no {@code Control} parameter |
244 * |
244 * locate and load resource bundles from |
245 * The {@code getBundle} factory methods load service providers of |
245 * {@linkplain ResourceBundleProvider service providers}. |
246 * {@link ResourceBundleProvider}, if available, using {@link ServiceLoader}. |
246 * It may continue the search as if calling {@link Module#getResourceAsStream(String)} |
247 * The service type is designated by |
247 * to find the named resource from a given module and calling |
248 * {@code <package name> + ".spi." + <simple name> + "Provider"}. For |
248 * {@link ClassLoader#getResourceAsStream(String)}; refer to |
249 * example, if the base name is "{@code com.example.app.MyResources}", the service |
249 * the specification of the {@code getBundle} method for details. |
250 * type is {@code com.example.app.spi.MyResourcesProvider}. |
250 * Only non-encapsulated resource bundles of "{@code java.class}" |
251 * <p> |
251 * or "{@code java.properties}" format are searched. |
252 * In named modules, the loaded service providers for the given base name are |
252 * |
253 * used to load resource bundles. If no service provider is available, or if |
253 * <p>If the caller module is a |
254 * none of the service providers returns a resource bundle and the caller module |
254 * <a href="{@docRoot}/java/util/spi/ResourceBundleProvider.html#obtain-resource-bundle"> |
255 * doesn't have its own service provider, the {@code getBundle} factory method |
255 * resource bundle provider</a>, it does not fall back to the |
256 * searches for resource bundles that are local in the caller module and that |
256 * class loader search. |
257 * are visible to the class loader of the caller module. The resource bundle |
257 * |
258 * formats for local module searching are "java.class" and "java.properties". |
258 * <h4>Resource bundles in automatic modules</h4> |
|
259 * |
|
260 * A common format of resource bundles is in {@linkplain PropertyResourceBundle |
|
261 * .properties} file format. Typically {@code .properties} resource bundles |
|
262 * are packaged in a JAR file. Resource bundle only JAR file can be readily |
|
263 * deployed as an <a href="{@docRoot}/java/lang/module/ModuleFinder.html#automatic-modules"> |
|
264 * automatic module</a>. For example, if the JAR file contains the |
|
265 * entry "{@code p/q/Foo_ja.properties}" and no {@code .class} entry, |
|
266 * when resolved and defined as an automatic module, no package is derived |
|
267 * for this module. This allows resource bundles in {@code .properties} |
|
268 * format packaged in one or more JAR files that may contain entries |
|
269 * in the same directory and can be resolved successfully as |
|
270 * automatic modules. |
259 * |
271 * |
260 * <h3>ResourceBundle.Control</h3> |
272 * <h3>ResourceBundle.Control</h3> |
261 * |
273 * |
262 * The {@link ResourceBundle.Control} class provides information necessary |
274 * The {@link ResourceBundle.Control} class provides information necessary |
263 * to perform the bundle loading process by the <code>getBundle</code> |
275 * to perform the bundle loading process by the <code>getBundle</code> |
265 * instance. You can implement your own subclass in order to enable |
277 * instance. You can implement your own subclass in order to enable |
266 * non-standard resource bundle formats, change the search strategy, or |
278 * non-standard resource bundle formats, change the search strategy, or |
267 * define caching parameters. Refer to the descriptions of the class and the |
279 * define caching parameters. Refer to the descriptions of the class and the |
268 * {@link #getBundle(String, Locale, ClassLoader, Control) getBundle} |
280 * {@link #getBundle(String, Locale, ClassLoader, Control) getBundle} |
269 * factory method for details. |
281 * factory method for details. |
|
282 * |
|
283 * <p> {@link ResourceBundle.Control} is designed for an application deployed |
|
284 * in an unnamed module, for example to support resource bundles in |
|
285 * non-standard formats or package localized resources in a non-traditional |
|
286 * convention. {@link ResourceBundleProvider} is the replacement for |
|
287 * {@code ResourceBundle.Control} when migrating to modules. |
|
288 * {@code UnsupportedOperationException} will be thrown when a factory |
|
289 * method that takes the {@code ResourceBundle.Control} parameter is called. |
270 * |
290 * |
271 * <p><a id="modify_default_behavior">For the {@code getBundle} factory</a> |
291 * <p><a id="modify_default_behavior">For the {@code getBundle} factory</a> |
272 * methods that take no {@link Control} instance, their <a |
292 * methods that take no {@link Control} instance, their <a |
273 * href="#default_behavior"> default behavior</a> of resource bundle loading |
293 * href="#default_behavior"> default behavior</a> of resource bundle loading |
274 * can be modified with custom {@link |
294 * can be modified with custom {@link |
813 } |
833 } |
814 } |
834 } |
815 |
835 |
816 /** |
836 /** |
817 * Gets a resource bundle using the specified base name, the default locale, |
837 * Gets a resource bundle using the specified base name, the default locale, |
818 * and the caller's class loader. Calling this method is equivalent to calling |
838 * and the caller module. Calling this method is equivalent to calling |
819 * <blockquote> |
839 * <blockquote> |
820 * <code>getBundle(baseName, Locale.getDefault(), this.getClass().getClassLoader())</code>, |
840 * <code>getBundle(baseName, Locale.getDefault(), callerModule)</code>, |
821 * </blockquote> |
841 * </blockquote> |
822 * except that <code>getClassLoader()</code> is run with the security |
|
823 * privileges of <code>ResourceBundle</code>. |
|
824 * See {@link #getBundle(String, Locale, ClassLoader) getBundle} |
|
825 * for a complete description of the search and instantiation strategy. |
|
826 * |
842 * |
827 * @param baseName the base name of the resource bundle, a fully qualified class name |
843 * @param baseName the base name of the resource bundle, a fully qualified class name |
828 * @exception java.lang.NullPointerException |
844 * @exception java.lang.NullPointerException |
829 * if <code>baseName</code> is <code>null</code> |
845 * if <code>baseName</code> is <code>null</code> |
830 * @exception MissingResourceException |
846 * @exception MissingResourceException |
831 * if no resource bundle for the specified base name can be found |
847 * if no resource bundle for the specified base name can be found |
832 * @return a resource bundle for the given base name and the default locale |
848 * @return a resource bundle for the given base name and the default locale |
|
849 * |
|
850 * @see <a href="#default_behavior">Resource Bundle Search and Loading Strategy</a> |
|
851 * @see <a href="#resource-bundle-modules">Resource Bundles and Named Modules</a> |
833 */ |
852 */ |
834 @CallerSensitive |
853 @CallerSensitive |
835 public static final ResourceBundle getBundle(String baseName) |
854 public static final ResourceBundle getBundle(String baseName) |
836 { |
855 { |
837 Class<?> caller = Reflection.getCallerClass(); |
856 Class<?> caller = Reflection.getCallerClass(); |
885 return getBundleImpl(baseName, targetLocale, caller, control); |
904 return getBundleImpl(baseName, targetLocale, caller, control); |
886 } |
905 } |
887 |
906 |
888 /** |
907 /** |
889 * Gets a resource bundle using the specified base name and locale, |
908 * Gets a resource bundle using the specified base name and locale, |
890 * and the caller's class loader. Calling this method is equivalent to calling |
909 * and the caller module. Calling this method is equivalent to calling |
891 * <blockquote> |
910 * <blockquote> |
892 * <code>getBundle(baseName, locale, this.getClass().getClassLoader())</code>, |
911 * <code>getBundle(baseName, locale, callerModule)</code>, |
893 * </blockquote> |
912 * </blockquote> |
894 * except that <code>getClassLoader()</code> is run with the security |
|
895 * privileges of <code>ResourceBundle</code>. |
|
896 * See {@link #getBundle(String, Locale, ClassLoader) getBundle} |
|
897 * for a complete description of the search and instantiation strategy. |
|
898 * |
913 * |
899 * @param baseName |
914 * @param baseName |
900 * the base name of the resource bundle, a fully qualified class name |
915 * the base name of the resource bundle, a fully qualified class name |
901 * @param locale |
916 * @param locale |
902 * the locale for which a resource bundle is desired |
917 * the locale for which a resource bundle is desired |
903 * @exception NullPointerException |
918 * @exception NullPointerException |
904 * if <code>baseName</code> or <code>locale</code> is <code>null</code> |
919 * if <code>baseName</code> or <code>locale</code> is <code>null</code> |
905 * @exception MissingResourceException |
920 * @exception MissingResourceException |
906 * if no resource bundle for the specified base name can be found |
921 * if no resource bundle for the specified base name can be found |
907 * @return a resource bundle for the given base name and locale |
922 * @return a resource bundle for the given base name and locale |
|
923 * |
|
924 * @see <a href="#default_behavior">Resource Bundle Search and Loading Strategy</a> |
|
925 * @see <a href="#resource-bundle-modules">Resource Bundles and Named Modules</a> |
908 */ |
926 */ |
909 @CallerSensitive |
927 @CallerSensitive |
910 public static final ResourceBundle getBundle(String baseName, |
928 public static final ResourceBundle getBundle(String baseName, |
911 Locale locale) |
929 Locale locale) |
912 { |
930 { |
919 * Gets a resource bundle using the specified base name and the default locale |
937 * Gets a resource bundle using the specified base name and the default locale |
920 * on behalf of the specified module. This method is equivalent to calling |
938 * on behalf of the specified module. This method is equivalent to calling |
921 * <blockquote> |
939 * <blockquote> |
922 * <code>getBundle(baseName, Locale.getDefault(), module)</code> |
940 * <code>getBundle(baseName, Locale.getDefault(), module)</code> |
923 * </blockquote> |
941 * </blockquote> |
924 * |
|
925 * <p> Resource bundles in named modules may be encapsulated. When |
|
926 * the resource bundle is loaded from a provider, the caller module |
|
927 * must have an appropriate <i>uses</i> clause in its <i>module descriptor</i> |
|
928 * to declare that the module uses implementations of |
|
929 * {@code <package name> + ".spi." + <simple name> + "Provider"}. |
|
930 * Otherwise, it will load the resource bundles that are local in the |
|
931 * given module or that are visible to the class loader of the given module |
|
932 * (refer to the <a href="#bundleprovider">Resource Bundles in Named Modules</a> |
|
933 * section for details). |
|
934 * When the resource bundle is loaded from the specified module, it is |
|
935 * subject to the encapsulation rules specified by |
|
936 * {@link Module#getResourceAsStream Module.getResourceAsStream}. |
|
937 * |
942 * |
938 * @param baseName the base name of the resource bundle, |
943 * @param baseName the base name of the resource bundle, |
939 * a fully qualified class name |
944 * a fully qualified class name |
940 * @param module the module for which the resource bundle is searched |
945 * @param module the module for which the resource bundle is searched |
941 * @throws NullPointerException |
946 * @throws NullPointerException |
961 /** |
968 /** |
962 * Gets a resource bundle using the specified base name and locale |
969 * Gets a resource bundle using the specified base name and locale |
963 * on behalf of the specified module. |
970 * on behalf of the specified module. |
964 * |
971 * |
965 * <p> Resource bundles in named modules may be encapsulated. When |
972 * <p> Resource bundles in named modules may be encapsulated. When |
966 * the resource bundle is loaded from a provider, the caller module |
973 * the resource bundle is loaded from a |
|
974 * {@linkplain ResourceBundleProvider service provider}, the caller module |
967 * must have an appropriate <i>uses</i> clause in its <i>module descriptor</i> |
975 * must have an appropriate <i>uses</i> clause in its <i>module descriptor</i> |
968 * to declare that the module uses implementations of |
976 * to declare that the module uses of {@link ResourceBundleProvider} |
969 * {@code <package name> + ".spi." + <simple name> + "Provider"}. |
977 * for the named resource bundle. |
970 * Otherwise, it will load the resource bundles that are local in the |
978 * Otherwise, it will load the resource bundles that are local in the |
971 * given module or that are visible to the class loader of the given module |
979 * given module as if calling {@link Module#getResourceAsStream(String)} |
972 * (refer to the <a href="#bundleprovider">Resource Bundles in Named Modules</a> |
980 * or that are visible to the class loader of the given module |
973 * section for details). |
981 * as if calling {@link ClassLoader#getResourceAsStream(String)}. |
974 * When the resource bundle is loaded from the specified module, it is |
982 * When the resource bundle is loaded from the specified module, it is |
975 * subject to the encapsulation rules specified by |
983 * subject to the encapsulation rules specified by |
976 * {@link Module#getResourceAsStream Module.getResourceAsStream}. |
984 * {@link Module#getResourceAsStream Module.getResourceAsStream}. |
977 * |
985 * |
978 * <p> |
986 * <p> |
1058 |
1068 |
1059 /** |
1069 /** |
1060 * Gets a resource bundle using the specified base name, locale, and class |
1070 * Gets a resource bundle using the specified base name, locale, and class |
1061 * loader. |
1071 * loader. |
1062 * |
1072 * |
1063 * <p>This method behaves the same as calling |
1073 * <p>When this method is called from a named module and the given |
1064 * {@link #getBundle(String, Locale, ClassLoader, Control)} passing a |
1074 * loader is the class loader of the caller module, this is equivalent |
1065 * default instance of {@link Control} unless another {@link Control} is |
1075 * to calling: |
1066 * provided with the {@link ResourceBundleControlProvider} SPI. Refer to the |
1076 * <blockquote><pre> |
|
1077 * getBundle(baseName, targetLocale, callerModule) |
|
1078 * </pre></blockquote> |
|
1079 * |
|
1080 * otherwise, this is equivalent to calling: |
|
1081 * <blockquote><pre> |
|
1082 * getBundle(baseName, targetLocale, loader, control) |
|
1083 * </pre></blockquote> |
|
1084 * where {@code control} is the default instance of {@link Control} unless |
|
1085 * a {@code Control} instance is provided by |
|
1086 * {@link ResourceBundleControlProvider} SPI. Refer to the |
1067 * description of <a href="#modify_default_behavior">modifying the default |
1087 * description of <a href="#modify_default_behavior">modifying the default |
1068 * behavior</a>. |
1088 * behavior</a>. The following describes the default behavior. |
1069 * |
|
1070 * <p><a id="default_behavior">The following describes the default |
|
1071 * behavior</a>. |
|
1072 * |
1089 * |
1073 * <p> |
1090 * <p> |
1074 * Resource bundles in a named module are private to that module. If |
1091 * <b><a id="default_behavior">Resource Bundle Search and Loading Strategy</a></b> |
1075 * the caller is in a named module, this method will find resource bundles |
|
1076 * from the service providers of {@link java.util.spi.ResourceBundleProvider} |
|
1077 * if any. Otherwise, it will load the resource bundles that are visible to |
|
1078 * the given {@code loader} (refer to the |
|
1079 * <a href="#bundleprovider">Resource Bundles in Named Modules</a> section |
|
1080 * for details). |
|
1081 * If the caller is in a named module and the given {@code loader} is |
|
1082 * different than the caller's class loader, or if the caller is not in |
|
1083 * a named module, this method will not find resource bundles from named |
|
1084 * modules. |
|
1085 * |
1092 * |
1086 * <p><code>getBundle</code> uses the base name, the specified locale, and |
1093 * <p><code>getBundle</code> uses the base name, the specified locale, and |
1087 * the default locale (obtained from {@link java.util.Locale#getDefault() |
1094 * the default locale (obtained from {@link java.util.Locale#getDefault() |
1088 * Locale.getDefault}) to generate a sequence of <a |
1095 * Locale.getDefault}) to generate a sequence of <a |
1089 * id="candidates"><em>candidate bundle names</em></a>. If the specified |
1096 * id="candidates"><em>candidate bundle names</em></a>. If the specified |
2363 * <code>ResourceBundle.Control</code> collaborates with the factory |
2371 * <code>ResourceBundle.Control</code> collaborates with the factory |
2364 * methods for loading resource bundles. The default implementation of |
2372 * methods for loading resource bundles. The default implementation of |
2365 * the callback methods provides the information necessary for the |
2373 * the callback methods provides the information necessary for the |
2366 * factory methods to perform the <a |
2374 * factory methods to perform the <a |
2367 * href="./ResourceBundle.html#default_behavior">default behavior</a>. |
2375 * href="./ResourceBundle.html#default_behavior">default behavior</a>. |
2368 * <a href="#note">Note that this class is not supported in named modules.</a> |
2376 * |
|
2377 * <p> {@link ResourceBundle.Control} is designed for an application deployed |
|
2378 * in an unnamed module, for example to support resource bundles in |
|
2379 * non-standard formats or package localized resources in a non-traditional |
|
2380 * convention. {@link ResourceBundleProvider} is the replacement for |
|
2381 * {@code ResourceBundle.Control} when migrating to modules. |
|
2382 * {@code UnsupportedOperationException} will be thrown when a factory |
|
2383 * method that takes the {@code ResourceBundle.Control} parameter is called. |
2369 * |
2384 * |
2370 * <p>In addition to the callback methods, the {@link |
2385 * <p>In addition to the callback methods, the {@link |
2371 * #toBundleName(String, Locale) toBundleName} and {@link |
2386 * #toBundleName(String, Locale) toBundleName} and {@link |
2372 * #toResourceName(String, String) toResourceName} methods are defined |
2387 * #toResourceName(String, String) toResourceName} methods are defined |
2373 * primarily for convenience in implementing the callback |
2388 * primarily for convenience in implementing the callback |