Details
-
Bug
-
Status: Closed
-
Blocker
-
Resolution: Fixed
-
2.1.3
-
None
Description
This code started failing after adding the "return value" after the "if (value instanceof CharSequence)" line.
This fails:
static Object convertValueToType(Object value, Class targetType) { if(targetType != null && value != null && !(value in targetType)) { if (value instanceof CharSequence) { value = value.toString() if(value in targetType) { return value } } try { if (value instanceof Number && (targetType==Long || targetType==Integer)) { if(targetType == Long) { value = ((Number)value).toLong() } else { value = ((Number)value).toInteger() } } else if (value instanceof String && targetType in Number) { String strValue = value.trim() if(targetType == Long) { value = Long.parseLong(strValue) } else if (targetType == Integer) { value = Integer.parseInt(strValue) } else { value = StringGroovyMethods.asType(strValue, targetType) } } else { value = new SimpleTypeConverter().convertIfNecessary(value, targetType) } } catch (e) { // ignore } } return value }
This works:
static Object convertValueToType(Object passedValue, Class targetType) { Object value = passedValue if(targetType != null && value != null && !(value in targetType)) { if (value instanceof CharSequence) { value = value.toString() if(value in targetType) { return value } } try { if (value instanceof Number && (targetType==Long || targetType==Integer)) { if(targetType == Long) { value = ((Number)value).toLong() } else { value = ((Number)value).toInteger() } } else if (value instanceof String && targetType in Number) { String strValue = value.trim() if(targetType == Long) { value = Long.parseLong(strValue) } else if (targetType == Integer) { value = Integer.parseInt(strValue) } else { value = StringGroovyMethods.asType(strValue, targetType) } } else { value = new SimpleTypeConverter().convertIfNecessary(value, targetType) } } catch (e) { // ignore } } return value }
perhaps this has something to do with inference based return type selection.