Skip to content
Merged
Show file tree
Hide file tree
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 @@ -625,8 +625,9 @@ public static boolean defToboolean(final Object value) {
if (value instanceof Boolean) {
return (boolean)value;
} else {
throw new ClassCastException(
"cannot cast def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to boolean");
throw new ClassCastException("cannot cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
boolean.class.getCanonicalName());
}
}

Expand All @@ -635,7 +636,8 @@ public static byte defTobyteImplicit(final Object value) {
return (byte)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to byte");
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
byte.class.getCanonicalName());
}
}

Expand All @@ -646,7 +648,8 @@ public static short defToshortImplicit(final Object value) {
return (short)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to short");
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
short.class.getCanonicalName());
}
}

Expand All @@ -655,7 +658,8 @@ public static char defTocharImplicit(final Object value) {
return (char)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to char");
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
char.class.getCanonicalName());
}
}

Expand All @@ -670,7 +674,8 @@ public static int defTointImplicit(final Object value) {
return (int)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to int");
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
int.class.getCanonicalName());
}
}

Expand All @@ -686,9 +691,9 @@ public static long defTolongImplicit(final Object value) {
} else if (value instanceof Long) {
return (long)value;
} else {
throw new ClassCastException(
"cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to long");
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
long.class.getCanonicalName());
}
}

Expand All @@ -706,9 +711,9 @@ public static float defTofloatImplicit(final Object value) {
} else if (value instanceof Float) {
return (float)value;
} else {
throw new ClassCastException(
"cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to float");
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
float.class.getCanonicalName());
}
}

Expand All @@ -728,7 +733,9 @@ public static double defTodoubleImplicit(final Object value) {
} else if (value instanceof Double) {
return (double)value;
} else {
throw new ClassCastException("cannot implicitly cast def [" + value.getClass().getCanonicalName() + "] to double");
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
double.class.getCanonicalName());
}
}

Expand All @@ -745,7 +752,9 @@ public static byte defTobyteExplicit(final Object value) {
) {
return ((Number)value).byteValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to byte");
throw new ClassCastException("cannot explicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
byte.class.getCanonicalName());
}
}

Expand All @@ -762,12 +771,16 @@ public static short defToshortExplicit(final Object value) {
) {
return ((Number)value).shortValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to short");
throw new ClassCastException("cannot explicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
short.class.getCanonicalName());
}
}

public static char defTocharExplicit(final Object value) {
if (value instanceof Character) {
if (value instanceof String) {
return Utility.StringTochar((String)value);
} else if (value instanceof Character) {
return (char)value;
} else if (
value instanceof Byte ||
Expand All @@ -779,7 +792,9 @@ public static char defTocharExplicit(final Object value) {
) {
return (char)((Number)value).intValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to char");
throw new ClassCastException("cannot explicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
char.class.getCanonicalName());
}
}

Expand All @@ -796,7 +811,9 @@ public static int defTointExplicit(final Object value) {
) {
return ((Number)value).intValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to int");
throw new ClassCastException("cannot explicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
int.class.getCanonicalName());
}
}

Expand All @@ -813,7 +830,9 @@ public static long defTolongExplicit(final Object value) {
) {
return ((Number)value).longValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to long");
throw new ClassCastException("cannot explicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
long.class.getCanonicalName());
}
}

Expand All @@ -830,7 +849,9 @@ public static float defTofloatExplicit(final Object value) {
) {
return ((Number)value).floatValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to float");
throw new ClassCastException("cannot explicitly cast " +
"float [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
byte.class.getCanonicalName());
}
}

Expand All @@ -847,7 +868,9 @@ public static double defTodoubleExplicit(final Object value) {
) {
return ((Number)value).doubleValue();
} else {
throw new ClassCastException("cannot explicitly cast def [" + value.getClass().getCanonicalName() + "] to double");
throw new ClassCastException("cannot explicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
byte.class.getCanonicalName());
}
}

Expand Down Expand Up @@ -982,7 +1005,8 @@ public static Double defToDoubleImplicit(final Object value) {
return (Double)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + value.getClass().getCanonicalName() + "] to " + Double.class.getCanonicalName());
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
Double.class.getCanonicalName());
}
}

Expand All @@ -1002,7 +1026,8 @@ public static Byte defToByteExplicit(final Object value) {
return ((Number)value).byteValue();
} else {
throw new ClassCastException("cannot explicitly cast " +
"def [" + value.getClass().getCanonicalName() + "] to " + Byte.class.getCanonicalName());
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
Byte.class.getCanonicalName());
}
}

Expand All @@ -1022,13 +1047,16 @@ public static Short defToShortExplicit(final Object value) {
return ((Number)value).shortValue();
} else {
throw new ClassCastException("cannot explicitly cast " +
"def [" + value.getClass().getCanonicalName() + "] to " + Short.class.getCanonicalName());
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
Short.class.getCanonicalName());
}
}

public static Character defToCharacterExplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof String) {
return Utility.StringTochar((String)value);
} else if (value instanceof Character) {
return (Character)value;
} else if (
Expand All @@ -1042,7 +1070,8 @@ public static Character defToCharacterExplicit(final Object value) {
return (char)((Number)value).intValue();
} else {
throw new ClassCastException("cannot explicitly cast " +
"def [" + value.getClass().getCanonicalName() + "] to " + Character.class.getCanonicalName());
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
Character.class.getCanonicalName());
}
}

Expand All @@ -1062,7 +1091,8 @@ public static Integer defToIntegerExplicit(final Object value) {
return ((Number)value).intValue();
} else {
throw new ClassCastException("cannot explicitly cast " +
"def [" + value.getClass().getCanonicalName() + "] to " + Integer.class.getCanonicalName());
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
Integer.class.getCanonicalName());
}
}

Expand All @@ -1082,7 +1112,8 @@ public static Long defToLongExplicit(final Object value) {
return ((Number)value).longValue();
} else {
throw new ClassCastException("cannot explicitly cast " +
"def [" + value.getClass().getCanonicalName() + "] to " + Long.class.getCanonicalName());
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
Long.class.getCanonicalName());
}
}

Expand All @@ -1102,7 +1133,8 @@ public static Float defToFloatExplicit(final Object value) {
return ((Number)value).floatValue();
} else {
throw new ClassCastException("cannot explicitly cast " +
"def [" + value.getClass().getCanonicalName() + "] to " + Float.class.getCanonicalName());
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
Float.class.getCanonicalName());
}
}

Expand All @@ -1122,7 +1154,34 @@ public static Double defToDoubleExplicit(final Object value) {
return ((Number)value).doubleValue();
} else {
throw new ClassCastException("cannot explicitly cast " +
"def [" + value.getClass().getCanonicalName() + "] to " + Double.class.getCanonicalName());
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
Double.class.getCanonicalName());
}
}

public static String defToStringImplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof String) {
return (String)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
String.class.getCanonicalName());
}
}

public static String defToStringExplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Character) {
return Utility.charToString((char)value);
} else if (value instanceof String) {
return (String)value;
} else {
throw new ClassCastException("cannot explicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
String.class.getCanonicalName());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
import static org.elasticsearch.painless.WriterConstants.DEF_TO_P_LONG_IMPLICIT;
import static org.elasticsearch.painless.WriterConstants.DEF_TO_P_SHORT_EXPLICIT;
import static org.elasticsearch.painless.WriterConstants.DEF_TO_P_SHORT_IMPLICIT;
import static org.elasticsearch.painless.WriterConstants.DEF_TO_STRING_EXPLICIT;
import static org.elasticsearch.painless.WriterConstants.DEF_TO_STRING_IMPLICIT;
import static org.elasticsearch.painless.WriterConstants.DEF_UTIL_TYPE;
import static org.elasticsearch.painless.WriterConstants.INDY_STRING_CONCAT_BOOTSTRAP_HANDLE;
import static org.elasticsearch.painless.WriterConstants.LAMBDA_BOOTSTRAP_HANDLE;
Expand Down Expand Up @@ -188,6 +190,7 @@ public void writeCast(PainlessCast cast) {
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 if (cast.targetType == String.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_STRING_EXPLICIT);
else {
writeCast(cast.originalType, cast.targetType);
}
Expand All @@ -208,6 +211,7 @@ public void writeCast(PainlessCast cast) {
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 if (cast.targetType == String.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_STRING_IMPLICIT);
else {
writeCast(cast.originalType, cast.targetType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ public final class WriterConstants {
public static final Method DEF_TO_B_FLOAT_EXPLICIT = getAsmMethod(Float.class , "defToFloatExplicit" , Object.class);
public static final Method DEF_TO_B_DOUBLE_EXPLICIT = getAsmMethod(Double.class , "defToDoubleExplicit" , Object.class);

public static final Method DEF_TO_STRING_IMPLICIT = getAsmMethod(String.class, "defToStringImplicit", Object.class);
public static final Method DEF_TO_STRING_EXPLICIT = getAsmMethod(String.class, "defToStringExplicit", Object.class);

public static final Type DEF_ARRAY_LENGTH_METHOD_TYPE = Type.getMethodType(Type.INT_TYPE, Type.getType(Object.class));

/** invokedynamic bootstrap for lambda expression/method references */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void testdefToshortImplicit() {
}

public void testdefTocharImplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 's'; char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; char b = d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = (byte)0; char b = d;"));
Expand Down Expand Up @@ -232,6 +233,7 @@ public void testdefToshortExplicit() {
}

public void testdefTocharExplicit() {
assertEquals('s', exec("def d = 's'; char b = (char)d; b"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = 'string'; char b = (char)d;"));
expectScriptThrows(ClassCastException.class, () -> exec("def d = true; char b = (char)d;"));
assertEquals((char)0, exec("def d = (byte)0; char b = (char)d; b"));
Expand Down Expand Up @@ -400,6 +402,7 @@ public void testdefToShortImplicit() {
}

public void testdefToCharacterImplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = 's'; Character b = d;"));
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;"));
Expand Down Expand Up @@ -568,6 +571,7 @@ public void testdefToShortExplicit() {
}

public void testdefToCharacterExplicit() {
assertEquals('s', exec("def d = 's'; Character b = (Character)d; b"));
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"));
Expand Down Expand Up @@ -671,4 +675,12 @@ public void testdefToDoubleExplicit() {
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 testdefToStringImplicit() {
expectScriptThrows(ClassCastException.class, () -> exec("def d = (char)'s'; String b = d;"));
}

public void testdefToStringExplicit() {
assertEquals("s", exec("def d = (char)'s'; String b = (String)d; b"));
}
}