8157102: Avoid exceptional control flow in Configuration.getText
authorredestad
Tue, 17 May 2016 01:35:36 +0200 (2016-05-16)
changeset 38515 6ae7f7d9b44c
parent 38514 f7df9ab653b0
child 38516 b643c42e9d25
child 38517 51a0187f11f6
8157102: Avoid exceptional control flow in Configuration.getText Reviewed-by: jjg
langtools/src/jdk.javadoc/share/classes/com/sun/tools/doclets/internal/toolkit/Configuration.java
langtools/src/jdk.javadoc/share/classes/com/sun/tools/doclets/internal/toolkit/util/MessageRetriever.java
langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/Configuration.java
langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/MessageRetriever.java
--- a/langtools/src/jdk.javadoc/share/classes/com/sun/tools/doclets/internal/toolkit/Configuration.java	Mon May 16 21:46:32 2016 -0700
+++ b/langtools/src/jdk.javadoc/share/classes/com/sun/tools/doclets/internal/toolkit/Configuration.java	Tue May 17 01:35:36 2016 +0200
@@ -714,43 +714,43 @@
     }
 
     public String getText(String key) {
-        try {
-            //Check the doclet specific properties file.
-            return getDocletSpecificMsg().getText(key);
-        } catch (Exception e) {
-            //Check the shared properties file.
-            return message.getText(key);
+        // Check the doclet specific properties file.
+        MessageRetriever docletMessage = getDocletSpecificMsg();
+        if (docletMessage.containsKey(key)) {
+            return docletMessage.getText(key);
         }
+        // Check the shared properties file.
+        return message.getText(key);
     }
 
     public String getText(String key, String a1) {
-        try {
-            //Check the doclet specific properties file.
-            return getDocletSpecificMsg().getText(key, a1);
-        } catch (Exception e) {
-            //Check the shared properties file.
-            return message.getText(key, a1);
+        // Check the doclet specific properties file.
+        MessageRetriever docletMessage = getDocletSpecificMsg();
+        if (docletMessage.containsKey(key)) {
+            return docletMessage.getText(key, a1);
         }
+        // Check the shared properties file.
+        return message.getText(key, a1);
     }
 
     public String getText(String key, String a1, String a2) {
-        try {
-            //Check the doclet specific properties file.
-            return getDocletSpecificMsg().getText(key, a1, a2);
-        } catch (Exception e) {
-            //Check the shared properties file.
-            return message.getText(key, a1, a2);
+        // Check the doclet specific properties file.
+        MessageRetriever docletMessage = getDocletSpecificMsg();
+        if (docletMessage.containsKey(key)) {
+            return docletMessage.getText(key, a1, a2);
         }
+        // Check the shared properties file.
+        return message.getText(key, a1, a2);
     }
 
     public String getText(String key, String a1, String a2, String a3) {
-        try {
-            //Check the doclet specific properties file.
-            return getDocletSpecificMsg().getText(key, a1, a2, a3);
-        } catch (Exception e) {
-            //Check the shared properties file.
-            return message.getText(key, a1, a2, a3);
+        // Check the doclet specific properties file.
+        MessageRetriever docletMessage = getDocletSpecificMsg();
+        if (docletMessage.containsKey(key)) {
+            return docletMessage.getText(key, a1, a2, a3);
         }
+        // Check the shared properties file.
+        return message.getText(key, a1, a2, a3);
     }
 
     public abstract Content newContent();
--- a/langtools/src/jdk.javadoc/share/classes/com/sun/tools/doclets/internal/toolkit/util/MessageRetriever.java	Mon May 16 21:46:32 2016 -0700
+++ b/langtools/src/jdk.javadoc/share/classes/com/sun/tools/doclets/internal/toolkit/util/MessageRetriever.java	Tue May 17 01:35:36 2016 +0200
@@ -83,6 +83,34 @@
         this.resourcelocation = resourcelocation;
     }
 
+    private ResourceBundle initRB() {
+        ResourceBundle bundle = messageRB;
+        if (bundle == null) {
+            try {
+                messageRB = bundle =
+                        ResourceBundle.getBundle(resourcelocation, configuration.getLocale());
+            } catch (MissingResourceException e) {
+                throw new Error("Fatal: Resource (" + resourcelocation
+                        + ") for javadoc doclets is missing.");
+            }
+        }
+        return bundle;
+    }
+
+    /**
+     * Determines whether the given <code>key</code> can be retrieved
+     * from this <code>MessageRetriever</code>
+     *
+     * @param key
+     *        the resource <code>key</code>
+     * @return <code>true</code> if the given <code>key</code> is
+     *        contained in the underlying <code>ResourceBundle</code>.
+     */
+    public boolean containsKey(String key) {
+        ResourceBundle bundle = initRB();
+        return bundle.containsKey(key);
+    }
+
     /**
      * Get and format message string from resource
      *
@@ -92,15 +120,8 @@
      * exist in the properties file.
      */
     public String getText(String key, Object... args) throws MissingResourceException {
-        if (messageRB == null) {
-            try {
-                messageRB = ResourceBundle.getBundle(resourcelocation);
-            } catch (MissingResourceException e) {
-                throw new Error("Fatal: Resource (" + resourcelocation +
-                                    ") for javadoc doclets is missing.");
-            }
-        }
-        String message = messageRB.getString(key);
+        ResourceBundle bundle = initRB();
+        String message = bundle.getString(key);
         return MessageFormat.format(message, args);
     }
 
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/Configuration.java	Mon May 16 21:46:32 2016 -0700
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/Configuration.java	Tue May 17 01:35:36 2016 +0200
@@ -894,43 +894,43 @@
     }
 
     public String getText(String key) {
-        try {
-            //Check the doclet specific properties file.
-            return getDocletSpecificMsg().getText(key);
-        } catch (Exception e) {
-            //Check the shared properties file.
-            return message.getText(key);
+        // Check the doclet specific properties file.
+        MessageRetriever docletMessage = getDocletSpecificMsg();
+        if (docletMessage.containsKey(key)) {
+            return docletMessage.getText(key);
         }
+        // Check the shared properties file.
+        return message.getText(key);
     }
 
     public String getText(String key, String a1) {
-        try {
-            //Check the doclet specific properties file.
-            return getDocletSpecificMsg().getText(key, a1);
-        } catch (MissingResourceException e) {
-            //Check the shared properties file.
-            return message.getText(key, a1);
+        // Check the doclet specific properties file.
+        MessageRetriever docletMessage = getDocletSpecificMsg();
+        if (docletMessage.containsKey(key)) {
+            return docletMessage.getText(key, a1);
         }
+        // Check the shared properties file.
+        return message.getText(key, a1);
     }
 
     public String getText(String key, String a1, String a2) {
-        try {
-            //Check the doclet specific properties file.
-            return getDocletSpecificMsg().getText(key, a1, a2);
-        } catch (MissingResourceException e) {
-            //Check the shared properties file.
-            return message.getText(key, a1, a2);
+        // Check the doclet specific properties file.
+        MessageRetriever docletMessage = getDocletSpecificMsg();
+        if (docletMessage.containsKey(key)) {
+            return docletMessage.getText(key, a1, a2);
         }
+        // Check the shared properties file.
+        return message.getText(key, a1, a2);
     }
 
     public String getText(String key, String a1, String a2, String a3) {
-        try {
-            //Check the doclet specific properties file.
-            return getDocletSpecificMsg().getText(key, a1, a2, a3);
-        } catch (MissingResourceException e) {
-            //Check the shared properties file.
-            return message.getText(key, a1, a2, a3);
+        // Check the doclet specific properties file.
+        MessageRetriever docletMessage = getDocletSpecificMsg();
+        if (docletMessage.containsKey(key)) {
+            return docletMessage.getText(key, a1, a2, a3);
         }
+        // Check the shared properties file.
+        return message.getText(key, a1, a2, a3);
     }
 
     public abstract Content newContent();
--- a/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/MessageRetriever.java	Mon May 16 21:46:32 2016 -0700
+++ b/langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/MessageRetriever.java	Tue May 17 01:35:36 2016 +0200
@@ -86,15 +86,32 @@
         this.resourcelocation = resourcelocation;
     }
 
-    private void initRB() {
-        if (messageRB == null) {
+    private ResourceBundle initRB() {
+        ResourceBundle bundle = messageRB;
+        if (bundle == null) {
             try {
-                messageRB = ResourceBundle.getBundle(resourcelocation, configuration.getLocale());
+                messageRB = bundle =
+                        ResourceBundle.getBundle(resourcelocation, configuration.getLocale());
             } catch (MissingResourceException e) {
                 throw new Error("Fatal: Resource (" + resourcelocation
                         + ") for javadoc doclets is missing.");
             }
         }
+        return bundle;
+    }
+
+    /**
+     * Determines whether the given <code>key</code> can be retrieved
+     * from this <code>MessageRetriever</code>
+     *
+     * @param key
+     *        the resource <code>key</code>
+     * @return <code>true</code> if the given <code>key</code> is
+     *        contained in the underlying <code>ResourceBundle</code>.
+     */
+    public boolean containsKey(String key) {
+        ResourceBundle bundle = initRB();
+        return bundle.containsKey(key);
     }
 
     /**
@@ -107,8 +124,8 @@
      * exist in the properties file.
      */
     public String getText(String key, Object... args) throws MissingResourceException {
-        initRB();
-        String message = messageRB.getString(key);
+        ResourceBundle bundle = initRB();
+        String message = bundle.getString(key);
         return MessageFormat.format(message, args);
     }