Skip to content

Commit 2c2bed2

Browse files
committed
ResourceBundleMessageSource checks containsKey before calling getString
Issue: SPR-13295
1 parent 27cd879 commit 2c2bed2

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

spring-context/src/main/java/org/springframework/context/support/ResourceBundleMessageSource.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -372,15 +372,31 @@ protected MessageFormat getMessageFormat(ResourceBundle bundle, String code, Loc
372372
}
373373
}
374374

375-
private String getStringOrNull(ResourceBundle bundle, String key) {
376-
try {
377-
return bundle.getString(key);
378-
}
379-
catch (MissingResourceException ex) {
380-
// Assume key not found
381-
// -> do NOT throw the exception to allow for checking parent message source.
382-
return null;
375+
/**
376+
* Efficiently retrieve the String value for the specified key,
377+
* or return {@code null} if not found.
378+
* <p>As of 4.2, the default implementation checks {@code containsKey}
379+
* before it attempts to call {@code getString} (which would require
380+
* catching {@code MissingResourceException} for key not found).
381+
* <p>Can be overridden in subclasses.
382+
* @param bundle the ResourceBundle to perform the lookup in
383+
* @param key the key to look up
384+
* @return the associated value, or {@code null} if none
385+
* @since 4.2
386+
* @see ResourceBundle#getString(String)
387+
* @see ResourceBundle#containsKey(String)
388+
*/
389+
protected String getStringOrNull(ResourceBundle bundle, String key) {
390+
if (bundle.containsKey(key)) {
391+
try {
392+
return bundle.getString(key);
393+
}
394+
catch (MissingResourceException ex){
395+
// Assume key not found for some other reason
396+
// -> do NOT throw the exception to allow for checking parent message source.
397+
}
383398
}
399+
return null;
384400
}
385401

386402
/**

0 commit comments

Comments
 (0)