From 2c2bed2adbff39ec2508ba89af7a017bb57d3e95 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 30 Jul 2015 19:34:12 +0200 Subject: [PATCH] ResourceBundleMessageSource checks containsKey before calling getString Issue: SPR-13295 --- .../support/ResourceBundleMessageSource.java | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/support/ResourceBundleMessageSource.java b/spring-context/src/main/java/org/springframework/context/support/ResourceBundleMessageSource.java index 2100e44d3b..23ae8340aa 100644 --- a/spring-context/src/main/java/org/springframework/context/support/ResourceBundleMessageSource.java +++ b/spring-context/src/main/java/org/springframework/context/support/ResourceBundleMessageSource.java @@ -372,15 +372,31 @@ public class ResourceBundleMessageSource extends AbstractMessageSource implement } } - private String getStringOrNull(ResourceBundle bundle, String key) { - try { - return bundle.getString(key); - } - catch (MissingResourceException ex) { - // Assume key not found - // -> do NOT throw the exception to allow for checking parent message source. - return null; + /** + * Efficiently retrieve the String value for the specified key, + * or return {@code null} if not found. + *

As of 4.2, the default implementation checks {@code containsKey} + * before it attempts to call {@code getString} (which would require + * catching {@code MissingResourceException} for key not found). + *

Can be overridden in subclasses. + * @param bundle the ResourceBundle to perform the lookup in + * @param key the key to look up + * @return the associated value, or {@code null} if none + * @since 4.2 + * @see ResourceBundle#getString(String) + * @see ResourceBundle#containsKey(String) + */ + protected String getStringOrNull(ResourceBundle bundle, String key) { + if (bundle.containsKey(key)) { + try { + return bundle.getString(key); + } + catch (MissingResourceException ex){ + // Assume key not found for some other reason + // -> do NOT throw the exception to allow for checking parent message source. + } } + return null; } /**