Skip to content

Commit

Permalink
Replace ClassCastException with type check - https://issues.apache.or…
Browse files Browse the repository at this point in the history
…g/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.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1680058 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
adrian-crum committed May 18, 2015
1 parent b6e90c0 commit c372a6c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 33 deletions.
8 changes: 3 additions & 5 deletions framework/base/src/org/ofbiz/base/util/ObjectType.java
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,9 @@ public static Object simpleTypeConvert(Object obj, String type, String format, T
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();
}
Expand Down
4 changes: 2 additions & 2 deletions framework/minilang/src/org/ofbiz/minilang/MiniLangUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public static Object convertType(Object obj, Class<?> targetClass, Locale locale
}
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();
Expand All @@ -181,7 +181,7 @@ public static Object convertType(Object obj, Class<?> targetClass, Locale locale
format = null;
}
return localizedConverter.convert(obj, locale, timeZone, format);
} catch (ClassCastException e) {}
}
return converter.convert(obj);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@ public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale loc
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);
Expand All @@ -133,16 +132,16 @@ public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale loc
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);
}
Expand All @@ -155,16 +154,16 @@ public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale loc
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");
}
Expand All @@ -177,16 +176,16 @@ public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale loc
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");
}
Expand Down Expand Up @@ -226,16 +225,16 @@ public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale loc
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");
}
Expand All @@ -248,16 +247,16 @@ public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale loc
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");
}
Expand All @@ -275,16 +274,16 @@ public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale loc
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);
}
Expand Down

0 comments on commit c372a6c

Please sign in to comment.