8157102: Avoid exceptional control flow in Configuration.getText

Reviewed-by: jjg
This commit is contained in:
Claes Redestad 2016-05-17 01:35:36 +02:00
parent 149821a78e
commit 6cc9359822
4 changed files with 100 additions and 62 deletions

View File

@ -714,43 +714,43 @@ public abstract class Configuration {
}
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();

View File

@ -83,6 +83,34 @@ public class MessageRetriever {
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 @@ public class MessageRetriever {
* 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);
}

View File

@ -894,43 +894,43 @@ public abstract class Configuration {
}
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();

View File

@ -86,15 +86,32 @@ public class MessageRetriever {
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 @@ public class MessageRetriever {
* 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);
}