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

Boolean.parseBoolean() does not detect errors

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