You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by jl...@apache.org on 2019/09/14 17:11:59 UTC

svn commit: r1866952 - in /ofbiz/ofbiz-framework/trunk/framework: base/src/main/java/org/apache/ofbiz/base/conversion/ base/src/main/java/org/apache/ofbiz/base/metrics/ base/src/main/java/org/apache/ofbiz/base/util/ base/src/main/java/org/apache/ofbiz/...

Author: jleroux
Date: Sat Sep 14 17:11:59 2019
New Revision: 1866952

URL: http://svn.apache.org/viewvc?rev=1866952&view=rev
Log:
Fixed: Fix Default or Empty Catch block in Java and Groovy files
(OFBIZ-)

In many Java and Groovy files we have auto generated catch blocks or empty catch 
blocks. 
To avoid such exception swallowing this should be improved to at least log the 
error and also return error in case of service.

Here we use a try-with-ressource block and log some errors

Modified:
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/conversion/MiscConverters.java
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/metrics/MetricsFactory.java
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/TimeDuration.java
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java
    ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java
    ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericEntity.java
    ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java
    ofbiz/ofbiz-framework/trunk/framework/webtools/src/main/java/org/apache/ofbiz/webtools/UtilCacheEvents.java

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/conversion/MiscConverters.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/conversion/MiscConverters.java?rev=1866952&r1=1866951&r2=1866952&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/conversion/MiscConverters.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/conversion/MiscConverters.java Sat Sep 14 17:11:59 2019
@@ -18,7 +18,6 @@
  *******************************************************************************/
 package org.apache.ofbiz.base.conversion;
 
-import java.io.IOException;
 import java.io.Reader;
 import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
@@ -124,22 +123,13 @@ public class MiscConverters implements C
             StringBuilder strBuf = new StringBuilder();
             char[] inCharBuffer = new char[CHAR_BUFFER_SIZE];
             int charsRead = 0;
-            Reader clobReader = null;
-            try {
-                clobReader =  obj.getCharacterStream();
+            try (Reader clobReader =  obj.getCharacterStream()) {
                 while ((charsRead = clobReader.read(inCharBuffer, 0, CHAR_BUFFER_SIZE)) > 0) {
                     strBuf.append(inCharBuffer, 0, charsRead);
                 }
             } catch (Exception e) {
                 throw new ConversionException(e);
             }
-            finally {
-                if (clobReader != null) {
-                    try {
-                        clobReader.close();
-                    } catch (IOException e) {}
-                }
-            }
             return strBuf.toString();
         }
     }

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/metrics/MetricsFactory.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/metrics/MetricsFactory.java?rev=1866952&r1=1866951&r2=1866952&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/metrics/MetricsFactory.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/metrics/MetricsFactory.java Sat Sep 14 17:11:59 2019
@@ -33,6 +33,7 @@ import java.util.TreeSet;
 
 import org.apache.ofbiz.base.lang.ThreadSafe;
 import org.apache.ofbiz.base.util.Assert;
+import org.apache.ofbiz.base.util.Debug;
 import org.apache.ofbiz.base.util.UtilProperties;
 import org.apache.ofbiz.base.util.cache.UtilCache;
 import org.w3c.dom.Element;
@@ -193,7 +194,10 @@ public final class MetricsFactory {
             try {
                 MetricsImpl that = (MetricsImpl) obj;
                 return this.name.equals(that.name);
-            } catch (Exception e) {}
+            } catch (Exception e) {
+                Debug.logError(e, "MetricsFactory");
+            }
+
             return false;
         }
 

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/TimeDuration.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/TimeDuration.java?rev=1866952&r1=1866951&r2=1866952&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/TimeDuration.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/TimeDuration.java Sat Sep 14 17:11:59 2019
@@ -32,6 +32,7 @@ import com.ibm.icu.util.Calendar;
 @SuppressWarnings("serial")
 public class TimeDuration implements Serializable, Comparable<TimeDuration> {
     /** A <code>TimeDuration</code> instance that represents a zero time duration. */
+    private static final String module = TimeDuration.class.getName();
     public static final TimeDuration ZeroTimeDuration = new NullDuration();
 
     protected final int milliseconds;
@@ -184,7 +185,10 @@ public class TimeDuration implements Ser
                     && this.minutes == that.minutes
                     && this.seconds == that.seconds
                     && this.milliseconds == that.milliseconds;
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return false;
     }
 

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java?rev=1866952&r1=1866951&r2=1866952&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/UtilMisc.java Sat Sep 14 17:11:59 2019
@@ -434,7 +434,10 @@ public final class UtilMisc {
         Double result = null;
         try {
             result = Double.parseDouble(obj.toString());
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return result;
     }
 
@@ -466,7 +469,10 @@ public final class UtilMisc {
         Integer result = null;
         try {
             result = Integer.parseInt(obj.toString());
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return result;
     }
 
@@ -498,7 +504,10 @@ public final class UtilMisc {
         Long result = null;
         try {
             result = Long.parseLong(obj.toString());
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return result;
     }
 

Modified: ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java?rev=1866952&r1=1866951&r2=1866952&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/base/src/main/java/org/apache/ofbiz/base/util/string/UelFunctions.java Sat Sep 14 17:11:59 2019
@@ -161,7 +161,7 @@ import org.xml.sax.SAXException;
  */
 public class UelFunctions {
 
-    public static final String module = UelFunctions.class.getName();
+    private static final String module = UelFunctions.class.getName();
     protected static final Functions functionMapper = new Functions();
 
     /** Returns a <code>FunctionMapper</code> instance.
@@ -317,106 +317,154 @@ public class UelFunctions {
         try {
             Map map = (Map) obj;
             return map.size();
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         try {
             Collection coll = (Collection) obj;
             return coll.size();
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         try {
             String str = (String) obj;
             return str.length();
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return -1;
     }
 
     public static boolean endsWith(String str1, String str2) {
         try {
             return str1.endsWith(str2);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return false;
     }
 
     public static int indexOf(String str1, String str2) {
         try {
             return str1.indexOf(str2);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return -1;
     }
 
     public static int lastIndexOf(String str1, String str2) {
         try {
             return str1.lastIndexOf(str2);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return -1;
     }
 
     public static int length(String str1) {
         try {
             return str1.length();
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return -1;
     }
 
     public static String replace(String str1, String str2, String str3) {
         try {
             return str1.replace(str2, str3);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return null;
     }
 
     public static String replaceAll(String str1, String str2, String str3) {
         try {
             return str1.replaceAll(str2, str3);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return null;
     }
 
     public static String replaceFirst(String str1, String str2, String str3) {
         try {
             return str1.replaceFirst(str2, str3);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return null;
     }
 
     public static boolean startsWith(String str1, String str2) {
         try {
             return str1.startsWith(str2);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return false;
     }
 
     public static String endString(String str, int index) {
         try {
             return str.substring(index);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return null;
     }
 
     public static String subString(String str, int beginIndex, int endIndex) {
         try {
             return str.substring(beginIndex, endIndex);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return null;
     }
 
     public static String trim(String str) {
         try {
             return str.trim();
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return null;
     }
 
     public static String toLowerCase(String str) {
         try {
             return str.toLowerCase(Locale.getDefault());
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return null;
     }
 
     public static String toUpperCase(String str) {
         try {
             return str.toUpperCase(Locale.getDefault());
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return null;
     }
 
@@ -427,14 +475,20 @@ public class UelFunctions {
     public static String sysGetEnv(String str) {
         try {
             return System.getenv(str);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return null;
     }
 
     public static String sysGetProp(String str) {
         try {
             return System.getProperty(str);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return null;
     }
 
@@ -447,7 +501,10 @@ public class UelFunctions {
             if (resolveLabel != null) {
                 return resolveLabel;
             }
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         return label;
     }
 

Modified: ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericEntity.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericEntity.java?rev=1866952&r1=1866951&r2=1866952&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericEntity.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/entity/src/main/java/org/apache/ofbiz/entity/GenericEntity.java Sat Sep 14 17:11:59 2019
@@ -696,11 +696,17 @@ public class GenericEntity implements Ma
         try {
             Number number = (Number) obj;
             return TimeDuration.fromNumber(number);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         try {
             String duration = (String) obj;
             return TimeDuration.parseDuration(duration);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         throw new IllegalArgumentException("getDuration could not map the object '" + obj.toString() + "' to TimeDuration type, incompatible object type: " + obj.getClass().getName());
     }
 

Modified: ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java?rev=1866952&r1=1866951&r2=1866952&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/service/src/main/java/org/apache/ofbiz/service/mail/JavaMailContainer.java Sat Sep 14 17:11:59 2019
@@ -296,7 +296,9 @@ public class JavaMailContainer implement
                     if (store.isConnected()) {
                         try {
                             store.close();
-                        } catch (Exception e) {}
+                        } catch (Exception e) {
+                            Debug.logError(e, module);
+                        }
                     }
                 }
             }

Modified: ofbiz/ofbiz-framework/trunk/framework/webtools/src/main/java/org/apache/ofbiz/webtools/UtilCacheEvents.java
URL: http://svn.apache.org/viewvc/ofbiz/ofbiz-framework/trunk/framework/webtools/src/main/java/org/apache/ofbiz/webtools/UtilCacheEvents.java?rev=1866952&r1=1866951&r2=1866952&view=diff
==============================================================================
--- ofbiz/ofbiz-framework/trunk/framework/webtools/src/main/java/org/apache/ofbiz/webtools/UtilCacheEvents.java (original)
+++ ofbiz/ofbiz-framework/trunk/framework/webtools/src/main/java/org/apache/ofbiz/webtools/UtilCacheEvents.java Sat Sep 14 17:11:59 2019
@@ -19,13 +19,15 @@
 package org.apache.ofbiz.webtools;
 
 import java.util.Iterator;
-import java.util.Locale;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
-import java.util.LinkedList;
+
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 
+import org.apache.ofbiz.base.util.Debug;
 import org.apache.ofbiz.base.util.UtilDateTime;
 import org.apache.ofbiz.base.util.UtilHttp;
 import org.apache.ofbiz.base.util.UtilMisc;
@@ -39,6 +41,8 @@ import org.apache.ofbiz.security.Securit
 public final class UtilCacheEvents {
 
     private static final String err_resource = "WebtoolsErrorUiLabels";
+    private static final String module = UtilCacheEvents.class.getName();
+
 
     private UtilCacheEvents() {}
 
@@ -255,10 +259,16 @@ public final class UtilCacheEvents {
 
         try {
             maxInMemory = Integer.valueOf(maxInMemoryStr);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
         try {
             expireTime = Long.valueOf(expireTimeStr);
-        } catch (Exception e) {}
+        } catch (Exception e) {
+            Debug.logError(e, module);
+        }
+
 
         UtilCache<?, ?> utilCache = UtilCache.findCache(name);