Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public class DefaultTypeTransformation {
* @throws GroovyCastException if the object cannot be converted to a number
*/
public static byte byteUnbox(final Object value) {
if (value instanceof Byte b) {
return b;
}
Number n = castToNumber(value, byte.class);
return n.byteValue();
}
Expand All @@ -111,9 +114,11 @@ public static byte byteUnbox(final Object value) {
* @throws GroovyCastException if the object cannot be converted to char
*/
public static char charUnbox(final Object value) {
if (value instanceof Character c) {
return c;
}
if (value == null) return '\u0000'; // GROOVY-11371
var ch = ShortTypeHandling.castToChar(value);
return ch;
return ShortTypeHandling.castToChar(value);
}

/**
Expand All @@ -126,6 +131,9 @@ public static char charUnbox(final Object value) {
* @throws GroovyCastException if the object cannot be converted to a number
*/
public static short shortUnbox(final Object value) {
if (value instanceof Short s) {
return s;
}
Number n = castToNumber(value, short.class);
return n.shortValue();
}
Expand All @@ -140,6 +148,9 @@ public static short shortUnbox(final Object value) {
* @throws GroovyCastException if the object cannot be converted to a number
*/
public static int intUnbox(final Object value) {
if (value instanceof Integer i) {
return i;
}
Number n = castToNumber(value, int.class);
return n.intValue();
}
Expand All @@ -153,6 +164,9 @@ public static int intUnbox(final Object value) {
* @return the boolean value
*/
public static boolean booleanUnbox(final Object value) {
if (value instanceof Boolean b) {
return b;
}
return castToBoolean(value);
}

Expand All @@ -166,6 +180,9 @@ public static boolean booleanUnbox(final Object value) {
* @throws GroovyCastException if the object cannot be converted to a number
*/
public static long longUnbox(final Object value) {
if (value instanceof Long l) {
return l;
}
Number n = castToNumber(value, long.class);
return n.longValue();
}
Expand All @@ -181,6 +198,9 @@ public static long longUnbox(final Object value) {
* @throws GroovyCastException if the object cannot be converted to a number
*/
public static float floatUnbox(final Object value) {
if (value instanceof Float f) {
return f;
}
if (value == null) return Float.NaN; // GROOVY-11371
Number n = castToNumber(value, float.class);
return n.floatValue();
Expand All @@ -197,6 +217,9 @@ public static float floatUnbox(final Object value) {
* @throws GroovyCastException if the object cannot be converted to a number
*/
public static double doubleUnbox(final Object value) {
if (value instanceof Double d) {
return d;
}
if (value == null) return Double.NaN; // GROOVY-11371
Number n = castToNumber(value, double.class);
return n.doubleValue();
Expand Down Expand Up @@ -333,11 +356,15 @@ public static Number castToNumber(Object object) {
* @throws GroovyCastException if the object cannot be converted to a number
*/
public static Number castToNumber(Object object, Class type) {
if (object instanceof Number) {
return (Number) object;
if (object instanceof Number number) {
return number;
}
if (object instanceof Character) {
char c = (Character) object;
return castToNumberFallback(object, type);
}

private static Number castToNumberFallback(Object object, Class<?> type) {
if (object instanceof Character cObj) {
char c = cObj;
return (int) c;
}
if (object instanceof GString) {
Expand Down Expand Up @@ -370,6 +397,10 @@ public static boolean castToBoolean(Object object) {
return (Boolean) object;
}

return castToBooleanFallback(object);
}

private static boolean castToBooleanFallback(Object object) {
// if the object isn't null and no Boolean, try to call an asBoolean() method on the object
return (Boolean) InvokerHelper.invokeMethod(object, "asBoolean", InvokerHelper.EMPTY_ARGS);
}
Expand Down
Loading