Author: adrianc
Date: Mon May 18 17:18:54 2015 New Revision: 1680058 URL: http://svn.apache.org/r1680058 Log: Replace ClassCastException with type check - https://issues.apache.org/jira/browse/OFBIZ-6291. Ignoring a thrown ClassCastException is a common pattern in the project (one I have used myself), but it should be avoided in the future because it makes debugging difficult, plus it creates objects unnecessarily. Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/MiniLangUtil.java ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Compare.java Modified: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java?rev=1680058&r1=1680057&r2=1680058&view=diff ============================================================================== --- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java (original) +++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/ObjectType.java Mon May 18 17:18:54 2015 @@ -530,11 +530,9 @@ public class ObjectType { converter = (Converter<Object, Object>) Converters.getConverter(sourceClass, targetClass); } catch (ClassNotFoundException e) {} if (converter != null) { - LocalizedConverter<Object, Object> localizedConverter = null; - try { - localizedConverter = (LocalizedConverter<Object, Object>) converter; - } catch (ClassCastException e) {} - if (localizedConverter != null) { + if (converter instanceof LocalizedConverter) { + @SuppressWarnings("rawtypes") + LocalizedConverter<Object, Object> localizedConverter = (LocalizedConverter) converter; if (timeZone == null) { timeZone = TimeZone.getDefault(); } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/MiniLangUtil.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/MiniLangUtil.java?rev=1680058&r1=1680057&r2=1680058&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/MiniLangUtil.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/MiniLangUtil.java Mon May 18 17:18:54 2015 @@ -169,7 +169,7 @@ public final class MiniLangUtil { } Converter<Object, Object> converter = (Converter<Object, Object>) Converters.getConverter(sourceClass, targetClass); LocalizedConverter<Object, Object> localizedConverter = null; - try { + if (converter instanceof LocalizedConverter) { localizedConverter = (LocalizedConverter) converter; if (locale == null) { locale = Locale.getDefault(); @@ -181,7 +181,7 @@ public final class MiniLangUtil { format = null; } return localizedConverter.convert(obj, locale, timeZone, format); - } catch (ClassCastException e) {} + } return converter.convert(obj); } Modified: ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Compare.java URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Compare.java?rev=1680058&r1=1680057&r2=1680058&view=diff ============================================================================== --- ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Compare.java (original) +++ ofbiz/trunk/framework/minilang/src/org/ofbiz/minilang/method/conditional/Compare.java Mon May 18 17:18:54 2015 @@ -109,10 +109,9 @@ public abstract class Compare { if (lValue == null || lValue == GenericEntity.NULL_FIELD) { return false; } - try { + if (lValue instanceof Collection) { Collection<Object> collection = UtilGenerics.checkCollection(lValue); return collection.contains(rValue); - } catch (ClassCastException e) { } if (lValue instanceof String && rValue instanceof String) { return ((String) lValue).contains((String) rValue); @@ -133,16 +132,16 @@ public abstract class Compare { if (convertedRvalue == null) { return false; } - try { + if (convertedLvalue instanceof BigDecimal && + convertedRvalue instanceof BigDecimal) { BigDecimal lBigDecimal = (BigDecimal) convertedLvalue; BigDecimal rBigDecimal = (BigDecimal) convertedRvalue; return compareBigDecimals(lBigDecimal, rBigDecimal) == 0; - } catch (ClassCastException e) { } - try { + if (convertedLvalue instanceof Comparable && + convertedRvalue instanceof Comparable) { Comparable<Object> comparable = UtilGenerics.cast(convertedLvalue); return comparable.compareTo(convertedRvalue) == 0; - } catch (ClassCastException e) { } return convertedLvalue.equals(convertedRvalue); } @@ -155,16 +154,16 @@ public abstract class Compare { Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format); Object convertedRvalue = MiniLangUtil.convertType(rValue, type, locale, timeZone, format); assertValuesNotNull(convertedLvalue, convertedRvalue); - try { + if (convertedLvalue instanceof BigDecimal && + convertedRvalue instanceof BigDecimal) { BigDecimal lBigDecimal = (BigDecimal) convertedLvalue; BigDecimal rBigDecimal = (BigDecimal) convertedRvalue; return compareBigDecimals(lBigDecimal, rBigDecimal) > 0; - } catch (ClassCastException e) { } - try { + if (convertedLvalue instanceof Comparable && + convertedRvalue instanceof Comparable) { Comparable<Object> comparable = UtilGenerics.cast(convertedLvalue); return comparable.compareTo(convertedRvalue) > 0; - } catch (ClassCastException e) { } throw new IllegalArgumentException("Cannot compare: l-value is not a comparable type"); } @@ -177,16 +176,16 @@ public abstract class Compare { Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format); Object convertedRvalue = MiniLangUtil.convertType(rValue, type, locale, timeZone, format); assertValuesNotNull(convertedLvalue, convertedRvalue); - try { + if (convertedLvalue instanceof BigDecimal && + convertedRvalue instanceof BigDecimal) { BigDecimal lBigDecimal = (BigDecimal) convertedLvalue; BigDecimal rBigDecimal = (BigDecimal) convertedRvalue; return compareBigDecimals(lBigDecimal, rBigDecimal) >= 0; - } catch (ClassCastException e) { } - try { + if (convertedLvalue instanceof Comparable && + convertedRvalue instanceof Comparable) { Comparable<Object> comparable = UtilGenerics.cast(convertedLvalue); return comparable.compareTo(convertedRvalue) >= 0; - } catch (ClassCastException e) { } throw new IllegalArgumentException("Cannot compare: l-value is not a comparable type"); } @@ -226,16 +225,16 @@ public abstract class Compare { Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format); Object convertedRvalue = MiniLangUtil.convertType(rValue, type, locale, timeZone, format); assertValuesNotNull(convertedLvalue, convertedRvalue); - try { + if (convertedLvalue instanceof BigDecimal && + convertedRvalue instanceof BigDecimal) { BigDecimal lBigDecimal = (BigDecimal) convertedLvalue; BigDecimal rBigDecimal = (BigDecimal) convertedRvalue; return compareBigDecimals(lBigDecimal, rBigDecimal) < 0; - } catch (ClassCastException e) { } - try { + if (convertedLvalue instanceof Comparable && + convertedRvalue instanceof Comparable) { Comparable<Object> comparable = UtilGenerics.cast(convertedLvalue); return comparable.compareTo(convertedRvalue) < 0; - } catch (ClassCastException e) { } throw new IllegalArgumentException("Cannot compare: l-value is not a comparable type"); } @@ -248,16 +247,16 @@ public abstract class Compare { Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format); Object convertedRvalue = MiniLangUtil.convertType(rValue, type, locale, timeZone, format); assertValuesNotNull(convertedLvalue, convertedRvalue); - try { + if (convertedLvalue instanceof BigDecimal && + convertedRvalue instanceof BigDecimal) { BigDecimal lBigDecimal = (BigDecimal) convertedLvalue; BigDecimal rBigDecimal = (BigDecimal) convertedRvalue; return compareBigDecimals(lBigDecimal, rBigDecimal) <= 0; - } catch (ClassCastException e) { } - try { + if (convertedLvalue instanceof Comparable && + convertedRvalue instanceof Comparable) { Comparable<Object> comparable = UtilGenerics.cast(convertedLvalue); return comparable.compareTo(convertedRvalue) <= 0; - } catch (ClassCastException e) { } throw new IllegalArgumentException("Cannot compare: l-value is not a comparable type"); } @@ -275,16 +274,16 @@ public abstract class Compare { if (convertedRvalue == null) { return true; } - try { + if (convertedLvalue instanceof BigDecimal && + convertedRvalue instanceof BigDecimal) { BigDecimal lBigDecimal = (BigDecimal) convertedLvalue; BigDecimal rBigDecimal = (BigDecimal) convertedRvalue; return compareBigDecimals(lBigDecimal, rBigDecimal) != 0; - } catch (ClassCastException e) { } - try { + if (convertedLvalue instanceof Comparable && + convertedRvalue instanceof Comparable) { Comparable<Object> comparable = UtilGenerics.cast(convertedLvalue); return comparable.compareTo(convertedRvalue) != 0; - } catch (ClassCastException e) { } return !convertedLvalue.equals(convertedRvalue); } |
Free forum by Nabble | Edit this page |