提交 fb9ddcec authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Boolean.parseBoolean() does not detect errors

上级 993f6e73
......@@ -30,10 +30,10 @@ public class SettingsBase {
* @return the setting
*/
protected boolean get(String key, boolean defaultValue) {
String s = get(key, "" + defaultValue);
String s = get(key, null);
try {
return Boolean.parseBoolean(s);
} catch (NumberFormatException e) {
return Utils.parseBoolean(s, defaultValue, true);
} catch (IllegalArgumentException e) {
throw DbException.get(ErrorCode.DATA_CONVERSION_ERROR_1,
e, "key:" + key + " value:" + s);
}
......
......@@ -52,10 +52,9 @@ public class SortedProperties extends Properties {
*/
public static boolean getBooleanProperty(Properties prop, String key,
boolean def) {
String value = prop.getProperty(key, "" + def);
try {
return Boolean.parseBoolean(value);
} catch (Exception e) {
return Utils.parseBoolean(prop.getProperty(key, null), def, true);
} catch (IllegalArgumentException e) {
e.printStackTrace();
return def;
}
......
......@@ -642,6 +642,35 @@ public class Utils {
return clazz;
}
/**
* Parses the specified string to boolean value.
*
* @param value
* string to parse
* @param defaultValue
* value to return if value is null or on parsing error
* @param throwException
* throw exception on parsing error or return default value instead
* @return parsed or default value
* @throws IllegalArgumentException
* on parsing error if {@code throwException} is true
*/
public static boolean parseBoolean(String value, boolean defaultValue, boolean throwException) {
if (value == null) {
return defaultValue;
}
if (value.equalsIgnoreCase("true")) {
return true;
}
if (value.equalsIgnoreCase("false")) {
return false;
}
if (throwException) {
throw new IllegalArgumentException(value);
}
return defaultValue;
}
/**
* Get the system property. If the system property is not set, or if a
* security exception occurs, the default value is returned.
......@@ -687,15 +716,7 @@ public class Utils {
* @return the value
*/
public static boolean getProperty(String key, boolean defaultValue) {
String s = getProperty(key, null);
if (s != null) {
try {
return Boolean.parseBoolean(s);
} catch (NumberFormatException e) {
// ignore
}
}
return defaultValue;
return parseBoolean(getProperty(key, null), defaultValue, false);
}
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论