From 8e988f6c06a4f55fbc52b910ef4f691a651f36f7 Mon Sep 17 00:00:00 2001 From: Jack Conradson Date: Tue, 11 Dec 2018 14:06:38 -0800 Subject: [PATCH] [Painless] Add def to boxed type casts (#36506) This adds casts for the def type to all standard boxed types. Prior to this certain casts such as def [long/Long] -> Double would fail which does not follow the goals of the Painless casting model to remove the need for explicit boxing. This also creates symmetry with the casts for the newly created bridge methods being called at run-time. --- .../painless/AnalyzerCaster.java | 32 +- .../java/org/elasticsearch/painless/Def.java | 139 +++++++- .../elasticsearch/painless/MethodWriter.java | 91 +++-- .../painless/WriterConstants.java | 4 +- .../elasticsearch/painless/DefCastTests.java | 336 ++++++++++++++++++ 5 files changed, 547 insertions(+), 55 deletions(-) diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java index 588fe8ef5f7..f00a30a62c4 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java @@ -41,21 +41,37 @@ public final class AnalyzerCaster { if (actual == def.class) { if (expected == boolean.class) { - return PainlessCast.unboxTargetType(def.class, Boolean.class, explicit, boolean.class); + return PainlessCast.originalTypetoTargetType(def.class, boolean.class, explicit); } else if (expected == byte.class) { - return PainlessCast.unboxTargetType(def.class, Byte.class, explicit, byte.class); + return PainlessCast.originalTypetoTargetType(def.class, byte.class, explicit); } else if (expected == short.class) { - return PainlessCast.unboxTargetType(def.class, Short.class, explicit, short.class); + return PainlessCast.originalTypetoTargetType(def.class, short.class, explicit); } else if (expected == char.class) { - return PainlessCast.unboxTargetType(def.class, Character.class, explicit, char.class); + return PainlessCast.originalTypetoTargetType(def.class, char.class, explicit); } else if (expected == int.class) { - return PainlessCast.unboxTargetType(def.class, Integer.class, explicit, int.class); + return PainlessCast.originalTypetoTargetType(def.class, int.class, explicit); } else if (expected == long.class) { - return PainlessCast.unboxTargetType(def.class, Long.class, explicit, long.class); + return PainlessCast.originalTypetoTargetType(def.class, long.class, explicit); } else if (expected == float.class) { - return PainlessCast.unboxTargetType(def.class, Float.class, explicit, float.class); + return PainlessCast.originalTypetoTargetType(def.class, float.class, explicit); } else if (expected == double.class) { - return PainlessCast.unboxTargetType(def.class, Double.class, explicit, double.class); + return PainlessCast.originalTypetoTargetType(def.class, double.class, explicit); + } else if (expected == Boolean.class) { + return PainlessCast.originalTypetoTargetType(def.class, Boolean.class, explicit); + } else if (expected == Byte.class) { + return PainlessCast.originalTypetoTargetType(def.class, Byte.class, explicit); + } else if (expected == Short.class) { + return PainlessCast.originalTypetoTargetType(def.class, Short.class, explicit); + } else if (expected == Character.class) { + return PainlessCast.originalTypetoTargetType(def.class, Character.class, explicit); + } else if (expected == Integer.class) { + return PainlessCast.originalTypetoTargetType(def.class, Integer.class, explicit); + } else if (expected == Long.class) { + return PainlessCast.originalTypetoTargetType(def.class, Long.class, explicit); + } else if (expected == Float.class) { + return PainlessCast.originalTypetoTargetType(def.class, Float.class, explicit); + } else if (expected == Double.class) { + return PainlessCast.originalTypetoTargetType(def.class, Double.class, explicit); } } else if (actual == Object.class) { if (expected == byte.class && explicit && internal) { diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java index 496c2702a8b..c672956cb07 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java @@ -623,7 +623,7 @@ public final class Def { public static boolean defToboolean(final Object value) { if (value instanceof Boolean) { - return (boolean) value; + return (boolean)value; } else { throw new ClassCastException( "cannot cast def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to boolean"); @@ -853,11 +853,27 @@ public final class Def { // Conversion methods for def to boxed types. + public static Boolean defToBoolean(final Object value) { + if (value == null) { + return null; + } else if (value instanceof Boolean) { + return (Boolean)value; + } else { + throw new ClassCastException("cannot implicitly cast " + + "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + + Boolean.class.getCanonicalName()); + } + } + public static Byte defToByteImplicit(final Object value) { if (value == null) { return null; - } else { + } else if (value instanceof Byte) { return (Byte)value; + } else { + throw new ClassCastException("cannot implicitly cast " + + "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + + Byte.class.getCanonicalName()); } } @@ -866,16 +882,24 @@ public final class Def { return null; } else if (value instanceof Byte) { return (short)(byte)value; - } else { + } else if (value instanceof Short) { return (Short)value; + } else { + throw new ClassCastException("cannot implicitly cast " + + "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + + Short.class.getCanonicalName()); } } public static Character defToCharacterImplicit(final Object value) { if (value == null) { return null; - } else { + } else if (value instanceof Character) { return (Character)value; + } else { + throw new ClassCastException("cannot implicitly cast " + + "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + + Character.class.getCanonicalName()); } } @@ -888,8 +912,12 @@ public final class Def { return (int)(short)value; } else if (value instanceof Character) { return (int)(char)value; - } else { + } else if (value instanceof Integer) { return (Integer)value; + } else { + throw new ClassCastException("cannot implicitly cast " + + "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + + Integer.class.getCanonicalName()); } } @@ -904,8 +932,12 @@ public final class Def { return (long)(char)value; } else if (value instanceof Integer) { return (long)(int)value; - } else { + } else if (value instanceof Long) { return (Long)value; + } else { + throw new ClassCastException("cannot implicitly cast " + + "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + + Long.class.getCanonicalName()); } } @@ -922,8 +954,12 @@ public final class Def { return (float)(int)value; } else if (value instanceof Long) { return (float)(long)value; - } else { + } else if (value instanceof Float) { return (Float)value; + } else { + throw new ClassCastException("cannot implicitly cast " + + "def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " + + Float.class.getCanonicalName()); } } @@ -942,8 +978,11 @@ public final class Def { return (double)(long)value; } else if (value instanceof Float) { return (double)(float)value; - } else { + } else if (value instanceof Double) { return (Double)value; + } else { + throw new ClassCastException("cannot implicitly cast " + + "def [" + value.getClass().getCanonicalName() + "] to " + Double.class.getCanonicalName()); } } @@ -952,8 +991,18 @@ public final class Def { return null; } else if (value instanceof Character) { return (byte)(char)value; - } else { + } else if ( + value instanceof Byte || + value instanceof Short || + value instanceof Integer || + value instanceof Long || + value instanceof Float || + value instanceof Double + ) { return ((Number)value).byteValue(); + } else { + throw new ClassCastException("cannot explicitly cast " + + "def [" + value.getClass().getCanonicalName() + "] to " + Byte.class.getCanonicalName()); } } @@ -962,8 +1011,18 @@ public final class Def { return null; } else if (value instanceof Character) { return (short)(char)value; - } else { + } else if ( + value instanceof Byte || + value instanceof Short || + value instanceof Integer || + value instanceof Long || + value instanceof Float || + value instanceof Double + ) { return ((Number)value).shortValue(); + } else { + throw new ClassCastException("cannot explicitly cast " + + "def [" + value.getClass().getCanonicalName() + "] to " + Short.class.getCanonicalName()); } } @@ -972,8 +1031,18 @@ public final class Def { return null; } else if (value instanceof Character) { return (Character)value; - } else { + } else if ( + value instanceof Byte || + value instanceof Short || + value instanceof Integer || + value instanceof Long || + value instanceof Float || + value instanceof Double + ) { return (char)((Number)value).intValue(); + } else { + throw new ClassCastException("cannot explicitly cast " + + "def [" + value.getClass().getCanonicalName() + "] to " + Character.class.getCanonicalName()); } } @@ -982,8 +1051,18 @@ public final class Def { return null; } else if (value instanceof Character) { return (int)(char)value; - } else { + } else if ( + value instanceof Byte || + value instanceof Short || + value instanceof Integer || + value instanceof Long || + value instanceof Float || + value instanceof Double + ) { return ((Number)value).intValue(); + } else { + throw new ClassCastException("cannot explicitly cast " + + "def [" + value.getClass().getCanonicalName() + "] to " + Integer.class.getCanonicalName()); } } @@ -992,8 +1071,18 @@ public final class Def { return null; } else if (value instanceof Character) { return (long)(char)value; - } else { + } else if ( + value instanceof Byte || + value instanceof Short || + value instanceof Integer || + value instanceof Long || + value instanceof Float || + value instanceof Double + ) { return ((Number)value).longValue(); + } else { + throw new ClassCastException("cannot explicitly cast " + + "def [" + value.getClass().getCanonicalName() + "] to " + Long.class.getCanonicalName()); } } @@ -1002,8 +1091,18 @@ public final class Def { return null; } else if (value instanceof Character) { return (float)(char)value; - } else { + } else if ( + value instanceof Byte || + value instanceof Short || + value instanceof Integer || + value instanceof Long || + value instanceof Float || + value instanceof Double + ) { return ((Number)value).floatValue(); + } else { + throw new ClassCastException("cannot explicitly cast " + + "def [" + value.getClass().getCanonicalName() + "] to " + Float.class.getCanonicalName()); } } @@ -1012,8 +1111,18 @@ public final class Def { return null; } else if (value instanceof Character) { return (double)(char)value; - } else { + } else if ( + value instanceof Byte || + value instanceof Short || + value instanceof Integer || + value instanceof Long || + value instanceof Float || + value instanceof Double + ) { return ((Number)value).doubleValue(); + } else { + throw new ClassCastException("cannot explicitly cast " + + "def [" + value.getClass().getCanonicalName() + "] to " + Double.class.getCanonicalName()); } } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java index b03587ba5e9..a2433689db3 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/MethodWriter.java @@ -39,7 +39,22 @@ import java.util.List; import static org.elasticsearch.painless.WriterConstants.CHAR_TO_STRING; import static org.elasticsearch.painless.WriterConstants.DEF_BOOTSTRAP_HANDLE; -import static org.elasticsearch.painless.WriterConstants.DEF_TO_BOOLEAN; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_BOOLEAN; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_BYTE_EXPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_BYTE_IMPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_CHARACTER_EXPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_CHARACTER_IMPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_DOUBLE_EXPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_DOUBLE_IMPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_FLOAT_EXPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_FLOAT_IMPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_INTEGER_EXPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_INTEGER_IMPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_LONG_EXPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_LONG_IMPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_SHORT_EXPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_B_SHORT_IMPLICIT; +import static org.elasticsearch.painless.WriterConstants.DEF_TO_P_BOOLEAN; import static org.elasticsearch.painless.WriterConstants.DEF_TO_P_BYTE_EXPLICIT; import static org.elasticsearch.painless.WriterConstants.DEF_TO_P_BYTE_IMPLICIT; import static org.elasticsearch.painless.WriterConstants.DEF_TO_P_CHAR_EXPLICIT; @@ -143,42 +158,56 @@ public final class MethodWriter extends GeneratorAdapter { unbox(getType(cast.unboxOriginalType)); writeCast(cast.originalType, cast.targetType); } else if (cast.unboxTargetType != null) { - if (cast.originalType == def.class) { - if (cast.explicitCast) { - if (cast.targetType == Boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_BOOLEAN); - else if (cast.targetType == Byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BYTE_EXPLICIT); - else if (cast.targetType == Short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_SHORT_EXPLICIT); - else if (cast.targetType == Character.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_CHAR_EXPLICIT); - else if (cast.targetType == Integer.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_INT_EXPLICIT); - else if (cast.targetType == Long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_LONG_EXPLICIT); - else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_FLOAT_EXPLICIT); - else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_DOUBLE_EXPLICIT); - else { - throw new IllegalStateException("Illegal tree structure."); - } - } else { - if (cast.targetType == Boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_BOOLEAN); - else if (cast.targetType == Byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BYTE_IMPLICIT); - else if (cast.targetType == Short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_SHORT_IMPLICIT); - else if (cast.targetType == Character.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_CHAR_IMPLICIT); - else if (cast.targetType == Integer.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_INT_IMPLICIT); - else if (cast.targetType == Long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_LONG_IMPLICIT); - else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_FLOAT_IMPLICIT); - else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_DOUBLE_IMPLICIT); - else { - throw new IllegalStateException("Illegal tree structure."); - } - } - } else { - writeCast(cast.originalType, cast.targetType); - unbox(getType(cast.unboxTargetType)); - } + writeCast(cast.originalType, cast.targetType); + unbox(getType(cast.unboxTargetType)); } else if (cast.boxOriginalType != null) { box(getType(cast.boxOriginalType)); writeCast(cast.originalType, cast.targetType); } else if (cast.boxTargetType != null) { writeCast(cast.originalType, cast.targetType); box(getType(cast.boxTargetType)); + } else if (cast.originalType == def.class) { + if (cast.explicitCast) { + if (cast.targetType == boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BOOLEAN); + else if (cast.targetType == byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BYTE_EXPLICIT); + else if (cast.targetType == short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_SHORT_EXPLICIT); + else if (cast.targetType == char.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_CHAR_EXPLICIT); + else if (cast.targetType == int.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_INT_EXPLICIT); + else if (cast.targetType == long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_LONG_EXPLICIT); + else if (cast.targetType == float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_FLOAT_EXPLICIT); + else if (cast.targetType == double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_DOUBLE_EXPLICIT); + else if (cast.targetType == Boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BOOLEAN); + else if (cast.targetType == Byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BYTE_EXPLICIT); + else if (cast.targetType == Short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_SHORT_EXPLICIT); + else if (cast.targetType == Character.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_CHARACTER_EXPLICIT); + else if (cast.targetType == Integer.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_INTEGER_EXPLICIT); + else if (cast.targetType == Long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_LONG_EXPLICIT); + else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_FLOAT_EXPLICIT); + else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_DOUBLE_EXPLICIT); + else { + writeCast(cast.originalType, cast.targetType); + } + } else { + if (cast.targetType == boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BOOLEAN); + else if (cast.targetType == byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_BYTE_IMPLICIT); + else if (cast.targetType == short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_SHORT_IMPLICIT); + else if (cast.targetType == char.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_CHAR_IMPLICIT); + else if (cast.targetType == int.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_INT_IMPLICIT); + else if (cast.targetType == long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_LONG_IMPLICIT); + else if (cast.targetType == float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_FLOAT_IMPLICIT); + else if (cast.targetType == double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_P_DOUBLE_IMPLICIT); + else if (cast.targetType == Boolean.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BOOLEAN); + else if (cast.targetType == Byte.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_BYTE_IMPLICIT); + else if (cast.targetType == Short.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_SHORT_IMPLICIT); + else if (cast.targetType == Character.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_CHARACTER_IMPLICIT); + else if (cast.targetType == Integer.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_INTEGER_IMPLICIT); + else if (cast.targetType == Long.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_LONG_IMPLICIT); + else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_FLOAT_IMPLICIT); + else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_DOUBLE_IMPLICIT); + else { + writeCast(cast.originalType, cast.targetType); + } + } } else { writeCast(cast.originalType, cast.targetType); } diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java index 78125d7e750..12112684f27 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/WriterConstants.java @@ -124,7 +124,7 @@ public final class WriterConstants { public static final Type DEF_UTIL_TYPE = Type.getType(Def.class); - public static final Method DEF_TO_BOOLEAN = getAsmMethod(boolean.class, "defToboolean", Object.class); + public static final Method DEF_TO_P_BOOLEAN = getAsmMethod(boolean.class, "defToboolean", Object.class); public static final Method DEF_TO_P_BYTE_IMPLICIT = getAsmMethod(byte.class , "defTobyteImplicit" , Object.class); public static final Method DEF_TO_P_SHORT_IMPLICIT = getAsmMethod(short.class , "defToshortImplicit" , Object.class); @@ -141,6 +141,8 @@ public final class WriterConstants { public static final Method DEF_TO_P_FLOAT_EXPLICIT = getAsmMethod(float.class , "defTofloatExplicit" , Object.class); public static final Method DEF_TO_P_DOUBLE_EXPLICIT = getAsmMethod(double.class , "defTodoubleExplicit" , Object.class); + public static final Method DEF_TO_B_BOOLEAN = getAsmMethod(Boolean.class, "defToBoolean", Object.class); + public static final Method DEF_TO_B_BYTE_IMPLICIT = getAsmMethod(Byte.class , "defToByteImplicit" , Object.class); public static final Method DEF_TO_B_SHORT_IMPLICIT = getAsmMethod(Short.class , "defToShortImplicit" , Object.class); public static final Method DEF_TO_B_CHARACTER_IMPLICIT = getAsmMethod(Character.class , "defToCharacterImplicit" , Object.class); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefCastTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefCastTests.java index 2add69989b1..015176a43c3 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefCastTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/DefCastTests.java @@ -335,4 +335,340 @@ public class DefCastTests extends ScriptTestCase { assertEquals((double)0, exec("def d = Double.valueOf(0); double b = (double)d; b")); expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); double b = (double)d;")); } + + public void testdefToBooleanImplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Boolean b = d;")); + assertEquals(true, exec("def d = true; Boolean b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (byte)0; Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (short)0; Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)0; Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; Boolean b = d;")); + assertEquals(false, exec("def d = Boolean.valueOf(false); Boolean b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Byte.valueOf(0); Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Short.valueOf(0); Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Character.valueOf(0); Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); Boolean b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Boolean b = d;")); + } + + public void testdefToByteImplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Byte b = d;")); + assertEquals((byte)0, exec("def d = (byte)0; Byte b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (short)0; Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)0; Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Byte b = d;")); + assertEquals((byte)0, exec("def d = Byte.valueOf(0); Byte b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Short.valueOf(0); Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Character.valueOf(0); Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); Byte b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Byte b = d;")); + } + + public void testdefToShortImplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Short b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Short b = d;")); + assertEquals((short)0, exec("def d = (byte)0; Short b = d; b")); + assertEquals((short)0, exec("def d = (short)0; Short b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)0; Short b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; Short b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; Short b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; Short b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; Short b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Short b = d;")); + assertEquals((short)0, exec("def d = Byte.valueOf(0); Short b = d; b")); + assertEquals((short)0, exec("def d = Short.valueOf(0); Short b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Character.valueOf(0); Short b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); Short b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); Short b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); Short b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); Short b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Short b = d;")); + } + + public void testdefToCharacterImplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (byte)0; Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (short)0; Character b = d;")); + assertEquals((char)0, exec("def d = (char)0; Character b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Byte.valueOf(0); Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Short.valueOf(0); Character b = d;")); + assertEquals((char)0, exec("def d = Character.valueOf(0); Character b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); Character b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Character b = d;")); + } + + public void testdefToIntegerImplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Integer b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Integer b = d;")); + assertEquals(0, exec("def d = (byte)0; Integer b = d; b")); + assertEquals(0, exec("def d = (short)0; Integer b = d; b")); + assertEquals(0, exec("def d = (char)0; Integer b = d; b")); + assertEquals(0, exec("def d = 0; Integer b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; Integer b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; Integer b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; Integer b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Integer b = d;")); + assertEquals(0, exec("def d = Byte.valueOf(0); Integer b = d; b")); + assertEquals(0, exec("def d = Short.valueOf(0); Integer b = d; b")); + assertEquals(0, exec("def d = Character.valueOf(0); Integer b = d; b")); + assertEquals(0, exec("def d = Integer.valueOf(0); Integer b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); Integer b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); Integer b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); Integer b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Integer b = d;")); + } + + public void testdefToLongImplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Long b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Long b = d;")); + assertEquals((long)0, exec("def d = (byte)0; Long b = d; b")); + assertEquals((long)0, exec("def d = (short)0; Long b = d; b")); + assertEquals((long)0, exec("def d = (char)0; Long b = d; b")); + assertEquals((long)0, exec("def d = 0; Long b = d; b")); + assertEquals((long)0, exec("def d = (long)0; Long b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; Long b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; Long b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Long b = d;")); + assertEquals((long)0, exec("def d = Byte.valueOf(0); Long b = d; b")); + assertEquals((long)0, exec("def d = Short.valueOf(0); Long b = d; b")); + assertEquals((long)0, exec("def d = Character.valueOf(0); Long b = d; b")); + assertEquals((long)0, exec("def d = Integer.valueOf(0); Long b = d; b")); + assertEquals((long)0, exec("def d = Long.valueOf(0); Long b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); Long b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); Long b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Long b = d;")); + } + + public void testdefToFloatImplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Float b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Float b = d;")); + assertEquals((float)0, exec("def d = (byte)0; Float b = d; b")); + assertEquals((float)0, exec("def d = (short)0; Float b = d; b")); + assertEquals((float)0, exec("def d = (char)0; Float b = d; b")); + assertEquals((float)0, exec("def d = 0; Float b = d; b")); + assertEquals((float)0, exec("def d = (long)0; Float b = d; b")); + assertEquals((float)0, exec("def d = (float)0; Float b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; Float b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Float b = d;")); + assertEquals((float)0, exec("def d = Byte.valueOf(0); Float b = d; b")); + assertEquals((float)0, exec("def d = Short.valueOf(0); Float b = d; b")); + assertEquals((float)0, exec("def d = Character.valueOf(0); Float b = d; b")); + assertEquals((float)0, exec("def d = Integer.valueOf(0); Float b = d; b")); + assertEquals((float)0, exec("def d = Long.valueOf(0); Float b = d; b")); + assertEquals((float)0, exec("def d = Float.valueOf(0); Float b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); Float b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Float b = d;")); + } + + public void testdefToDoubleImplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Double b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Double b = d;")); + assertEquals((double)0, exec("def d = (byte)0; Double b = d; b")); + assertEquals((double)0, exec("def d = (short)0; Double b = d; b")); + assertEquals((double)0, exec("def d = (char)0; Double b = d; b")); + assertEquals((double)0, exec("def d = 0; Double b = d; b")); + assertEquals((double)0, exec("def d = (long)0; Double b = d; b")); + assertEquals((double)0, exec("def d = (float)0; Double b = d; b")); + assertEquals((double)0, exec("def d = (double)0; Double b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Double b = d;")); + assertEquals((double)0, exec("def d = Byte.valueOf(0); Double b = d; b")); + assertEquals((double)0, exec("def d = Short.valueOf(0); Double b = d; b")); + assertEquals((double)0, exec("def d = Character.valueOf(0); Double b = d; b")); + assertEquals((double)0, exec("def d = Integer.valueOf(0); Double b = d; b")); + assertEquals((double)0, exec("def d = Long.valueOf(0); Double b = d; b")); + assertEquals((double)0, exec("def d = Float.valueOf(0); Double b = d; b")); + assertEquals((double)0, exec("def d = Double.valueOf(0); Double b = d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Double b = d;")); + } + + public void testdefToBooleanExplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Boolean b = (Boolean)d;")); + assertEquals(true, exec("def d = true; Boolean b = (Boolean)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (byte)0; Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (short)0; Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)0; Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (int)0; Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (long)0; Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (float)0; Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = (double)0; Boolean b = (Boolean)d;")); + assertEquals(false, exec("def d = Boolean.valueOf(false); Boolean b = (Boolean)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Byte.valueOf(0); Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Short.valueOf(0); Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Character.valueOf(0); Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Integer.valueOf(0); Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Long.valueOf(0); Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Float.valueOf(0); Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Double.valueOf(0); Boolean b = (Boolean)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Boolean b = (Boolean)d;")); + } + + public void testdefToByteExplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Byte b = (Byte)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Byte b = (Byte)d;")); + assertEquals((byte)0, exec("def d = (byte)0; Byte b = (Byte)d; b")); + assertEquals((byte)0, exec("def d = (short)0; Byte b = (Byte)d; b")); + assertEquals((byte)0, exec("def d = (char)0; Byte b = (Byte)d; b")); + assertEquals((byte)0, exec("def d = 0; Byte b = (Byte)d; b")); + assertEquals((byte)0, exec("def d = (long)0; Byte b = (Byte)d; b")); + assertEquals((byte)0, exec("def d = (float)0; Byte b = (Byte)d; b")); + assertEquals((byte)0, exec("def d = (double)0; Byte b = (Byte)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Byte b = d;")); + assertEquals((byte)0, exec("def d = Byte.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte)0, exec("def d = Short.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte)0, exec("def d = Character.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte)0, exec("def d = Integer.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte)0, exec("def d = Long.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte)0, exec("def d = Float.valueOf(0); Byte b = (Byte)d; b")); + assertEquals((byte)0, exec("def d = Double.valueOf(0); Byte b = (Byte)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Byte b = (Byte)d;")); + } + + public void testdefToShortExplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Short b = (Short)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Short b = (Short)d;")); + assertEquals((short)0, exec("def d = (byte)0; Short b = (Short)d; b")); + assertEquals((short)0, exec("def d = (short)0; Short b = (Short)d; b")); + assertEquals((short)0, exec("def d = (char)0; Short b = (Short)d; b")); + assertEquals((short)0, exec("def d = 0; Short b = (Short)d; b")); + assertEquals((short)0, exec("def d = (long)0; Short b = (Short)d; b")); + assertEquals((short)0, exec("def d = (float)0; Short b = (Short)d; b")); + assertEquals((short)0, exec("def d = (double)0; Short b = (Short)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Short b = d;")); + assertEquals((short)0, exec("def d = Byte.valueOf(0); Short b = (Short)d; b")); + assertEquals((short)0, exec("def d = Short.valueOf(0); Short b = (Short)d; b")); + assertEquals((short)0, exec("def d = Character.valueOf(0); Short b = (Short)d; b")); + assertEquals((short)0, exec("def d = Integer.valueOf(0); Short b = (Short)d; b")); + assertEquals((short)0, exec("def d = Long.valueOf(0); Short b = (Short)d; b")); + assertEquals((short)0, exec("def d = Float.valueOf(0); Short b = (Short)d; b")); + assertEquals((short)0, exec("def d = Double.valueOf(0); Short b = (Short)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Short b = (Short)d;")); + } + + public void testdefToCharacterExplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Character b = (Character)d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Character b = (Character)d;")); + assertEquals((char)0, exec("def d = (byte)0; Character b = (Character)d; b")); + assertEquals((char)0, exec("def d = (short)0; Character b = (Character)d; b")); + assertEquals((char)0, exec("def d = (char)0; Character b = (Character)d; b")); + assertEquals((char)0, exec("def d = 0; Character b = (Character)d; b")); + assertEquals((char)0, exec("def d = (long)0; Character b = (Character)d; b")); + assertEquals((char)0, exec("def d = (float)0; Character b = (Character)d; b")); + assertEquals((char)0, exec("def d = (double)0; Character b = (Character)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Character b = d;")); + assertEquals((char)0, exec("def d = Byte.valueOf(0); Character b = (Character)d; b")); + assertEquals((char)0, exec("def d = Short.valueOf(0); Character b = (Character)d; b")); + assertEquals((char)0, exec("def d = Character.valueOf(0); Character b = (Character)d; b")); + assertEquals((char)0, exec("def d = Integer.valueOf(0); Character b = (Character)d; b")); + assertEquals((char)0, exec("def d = Long.valueOf(0); Character b = (Character)d; b")); + assertEquals((char)0, exec("def d = Float.valueOf(0); Character b = (Character)d; b")); + assertEquals((char)0, exec("def d = Double.valueOf(0); Character b = (Character)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Character b = (Character)d;")); + } + + public void testdefToIntegerExplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Integer b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Integer b = (Integer)d;")); + assertEquals(0, exec("def d = (byte)0; Integer b = (Integer)d; b")); + assertEquals(0, exec("def d = (short)0; Integer b = (Integer)d; b")); + assertEquals(0, exec("def d = (char)0; Integer b = (Integer)d; b")); + assertEquals(0, exec("def d = 0; Integer b = (Integer)d; b")); + assertEquals(0, exec("def d = (long)0; Integer b = (Integer)d; b")); + assertEquals(0, exec("def d = (float)0; Integer b = (Integer)d; b")); + assertEquals(0, exec("def d = (double)0; Integer b = (Integer)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Integer b = d;")); + assertEquals(0, exec("def d = Byte.valueOf(0); Integer b = (Integer)d; b")); + assertEquals(0, exec("def d = Short.valueOf(0); Integer b = (Integer)d; b")); + assertEquals(0, exec("def d = Character.valueOf(0); Integer b = (Integer)d; b")); + assertEquals(0, exec("def d = Integer.valueOf(0); Integer b = (Integer)d; b")); + assertEquals(0, exec("def d = Long.valueOf(0); Integer b = (Integer)d; b")); + assertEquals(0, exec("def d = Float.valueOf(0); Integer b = (Integer)d; b")); + assertEquals(0, exec("def d = Double.valueOf(0); Integer b = (Integer)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Integer b = (Integer)d;")); + } + + public void testdefToLongExplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Long b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Long b = (Long)d;")); + assertEquals((long)0, exec("def d = (byte)0; Long b = (Long)d; b")); + assertEquals((long)0, exec("def d = (short)0; Long b = (Long)d; b")); + assertEquals((long)0, exec("def d = (char)0; Long b = (Long)d; b")); + assertEquals((long)0, exec("def d = 0; Long b = (Long)d; b")); + assertEquals((long)0, exec("def d = (long)0; Long b = (Long)d; b")); + assertEquals((long)0, exec("def d = (float)0; Long b = (Long)d; b")); + assertEquals((long)0, exec("def d = (double)0; Long b = (Long)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Long b = d;")); + assertEquals((long)0, exec("def d = Byte.valueOf(0); Long b = (Long)d; b")); + assertEquals((long)0, exec("def d = Short.valueOf(0); Long b = (Long)d; b")); + assertEquals((long)0, exec("def d = Character.valueOf(0); Long b = (Long)d; b")); + assertEquals((long)0, exec("def d = Integer.valueOf(0); Long b = (Long)d; b")); + assertEquals((long)0, exec("def d = Long.valueOf(0); Long b = (Long)d; b")); + assertEquals((long)0, exec("def d = Float.valueOf(0); Long b = (Long)d; b")); + assertEquals((long)0, exec("def d = Double.valueOf(0); Long b = (Long)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Long b = (Long)d;")); + } + + public void testdefToFloatExplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Float b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Float b = (Float)d;")); + assertEquals((float)0, exec("def d = (byte)0; Float b = (Float)d; b")); + assertEquals((float)0, exec("def d = (short)0; Float b = (Float)d; b")); + assertEquals((float)0, exec("def d = (char)0; Float b = (Float)d; b")); + assertEquals((float)0, exec("def d = 0; Float b = (Float)d; b")); + assertEquals((float)0, exec("def d = (long)0; Float b = (Float)d; b")); + assertEquals((float)0, exec("def d = (float)0; Float b = (Float)d; b")); + assertEquals((float)0, exec("def d = (double)0; Float b = (Float)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Float b = d;")); + assertEquals((float)0, exec("def d = Byte.valueOf(0); Float b = (Float)d; b")); + assertEquals((float)0, exec("def d = Short.valueOf(0); Float b = (Float)d; b")); + assertEquals((float)0, exec("def d = Character.valueOf(0); Float b = (Float)d; b")); + assertEquals((float)0, exec("def d = Integer.valueOf(0); Float b = (Float)d; b")); + assertEquals((float)0, exec("def d = Long.valueOf(0); Float b = (Float)d; b")); + assertEquals((float)0, exec("def d = Float.valueOf(0); Float b = (Float)d; b")); + assertEquals((float)0, exec("def d = Double.valueOf(0); Float b = (Float)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Float b = (Float)d;")); + } + + public void testdefToDoubleExplicit() { + expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; Double b = d;")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = true; Double b = (Double)d;")); + assertEquals((double)0, exec("def d = (byte)0; Double b = (Double)d; b")); + assertEquals((double)0, exec("def d = (short)0; Double b = (Double)d; b")); + assertEquals((double)0, exec("def d = (char)0; Double b = (Double)d; b")); + assertEquals((double)0, exec("def d = 0; Double b = (Double)d; b")); + assertEquals((double)0, exec("def d = (long)0; Double b = (Double)d; b")); + assertEquals((double)0, exec("def d = (float)0; Double b = (Double)d; b")); + assertEquals((double)0, exec("def d = (double)0; Double b = (Double)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = Boolean.valueOf(true); Double b = d;")); + assertEquals((double)0, exec("def d = Byte.valueOf(0); Double b = (Double)d; b")); + assertEquals((double)0, exec("def d = Short.valueOf(0); Double b = (Double)d; b")); + assertEquals((double)0, exec("def d = Character.valueOf(0); Double b = (Double)d; b")); + assertEquals((double)0, exec("def d = Integer.valueOf(0); Double b = (Double)d; b")); + assertEquals((double)0, exec("def d = Long.valueOf(0); Double b = (Double)d; b")); + assertEquals((double)0, exec("def d = Float.valueOf(0); Double b = (Double)d; b")); + assertEquals((double)0, exec("def d = Double.valueOf(0); Double b = (Double)d; b")); + expectScriptThrows(ClassCastException.class, () -> exec("def d = new ArrayList(); Double b = (Double)d;")); + } }