Skip to content
Permalink
Browse files

Improved: Refactor UelFunctions.java to remove error management via E…

…xception

(OFBIZ-11213)

This refactoring was initiated since using UelFunction::getSize throws 
ClassCastException and log a consequent unneeded stack trace to test 
Object class type. The log is unnecessary, because it is the result of 
each supported class cast trial.

This issue show up that lot of possible NPE were not checked against,
but catched by 'Exception'.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1867635 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
gilPts committed Sep 27, 2019
1 parent 39f5dda commit 42c208ee0c5801d34d09f9fe3096a0c0f103d82c
Showing with 41 additions and 132 deletions.
  1. +41 −132 framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java
@@ -312,199 +312,108 @@ public static String timeString(Timestamp stamp, TimeZone timeZone, Locale local
return dateFormat.format(stamp);
}

@SuppressWarnings("rawtypes")
public static int getSize(Object obj) {
try {
Map map = (Map) obj;
return map.size();
} catch (Exception e) {
Debug.logError(e, module);
if (null == obj) return 0;
if (obj instanceof Map) {
return ((Map) obj).size();
}

try {
Collection coll = (Collection) obj;
return coll.size();
} catch (Exception e) {
Debug.logError(e, module);
if (obj instanceof Collection) {
return ((Collection) obj).size();
}

try {
String str = (String) obj;
return str.length();
} catch (Exception e) {
Debug.logError(e, module);
if (obj instanceof String) {
return ((String) obj).length();
}

return -1;
}

public static boolean endsWith(String str1, String str2) {
try {
return str1.endsWith(str2);
} catch (Exception e) {
Debug.logError(e, module);
}

return false;
if (null == str1) return false;
return str1.endsWith(str2);
}

public static int indexOf(String str1, String str2) {
try {
return str1.indexOf(str2);
} catch (Exception e) {
Debug.logError(e, module);
}

return -1;
if (null == str1) return -1;
return str1.indexOf(str2);
}

public static int lastIndexOf(String str1, String str2) {
try {
return str1.lastIndexOf(str2);
} catch (Exception e) {
Debug.logError(e, module);
}

return -1;
if (null == str1) return -1;
return str1.lastIndexOf(str2);
}

public static int length(String str1) {
try {
return str1.length();
} catch (Exception e) {
Debug.logError(e, module);
}

return -1;
if (null == str1) return 0;
return str1.length();
}

public static String replace(String str1, String str2, String str3) {
try {
return str1.replace(str2, str3);
} catch (Exception e) {
Debug.logError(e, module);
}

return null;
if (null == str1) return null;
return str1.replace(str2, str3);
}

public static String replaceAll(String str1, String str2, String str3) {
try {
return str1.replaceAll(str2, str3);
} catch (Exception e) {
Debug.logError(e, module);
}

return null;
if (null == str1) return null;
return str1.replaceAll(str2, str3);
}

public static String replaceFirst(String str1, String str2, String str3) {
try {
return str1.replaceFirst(str2, str3);
} catch (Exception e) {
Debug.logError(e, module);
}

return null;
if (null == str1) return null;
return str1.replaceFirst(str2, str3);
}

public static boolean startsWith(String str1, String str2) {
try {
return str1.startsWith(str2);
} catch (Exception e) {
Debug.logError(e, module);
}

return false;
if (null == str1) return false;
return str1.startsWith(str2);
}

public static String endString(String str, int index) {
try {
return str.substring(index);
} catch (Exception e) {
Debug.logError(e, module);
}

return null;
if (null == str) return null;
return str.substring(index);
}

public static String subString(String str, int beginIndex, int endIndex) {
try {
return str.substring(beginIndex, endIndex);
} catch (Exception e) {
Debug.logError(e, module);
}

return null;
if (null == str) return null;
return str.substring(beginIndex, endIndex);
}

public static String trim(String str) {
try {
return str.trim();
} catch (Exception e) {
Debug.logError(e, module);
}

return null;
if (null == str) return null;
return str.trim();
}

public static String toLowerCase(String str) {
try {
return str.toLowerCase(Locale.getDefault());
} catch (Exception e) {
Debug.logError(e, module);
}

return null;
if (null == str) return null;
return str.toLowerCase(Locale.getDefault());
}

public static String toUpperCase(String str) {
try {
return str.toUpperCase(Locale.getDefault());
} catch (Exception e) {
Debug.logError(e, module);
}

return null;
if (null == str) return null;
return str.toUpperCase(Locale.getDefault());
}

public static String toString(Object obj) {
if (null == obj) return null;
return obj.toString();
}

public static String sysGetEnv(String str) {
try {
return System.getenv(str);
} catch (Exception e) {
Debug.logError(e, module);
}

return null;
if (null == str) return null;
return System.getenv(str);
}

public static String sysGetProp(String str) {
try {
return System.getProperty(str);
} catch (Exception e) {
Debug.logError(e, module);
}

return null;
if (null == str) return null;
return System.getProperty(str);
}

public static String label(String ressource, String label, Locale locale) {
if (locale == null) {
locale = Locale.getDefault();
}
try {
String resolveLabel = UtilProperties.getMessage(ressource, label, locale);
if (resolveLabel != null) {
return resolveLabel;
}
} catch (Exception e) {
Debug.logError(e, module);
String resolveLabel = UtilProperties.getMessage(ressource, label, locale);
if (resolveLabel != null) {
return resolveLabel;
}

return label;
}

0 comments on commit 42c208e

Please sign in to comment.
You can’t perform that action at this time.