Skip to content

Commit

Permalink
[Painless] Add extensive tests for def to primitive casts (#36455)
Browse files Browse the repository at this point in the history
This adds tests for each possible cast of def to a primitive type both implicit 
and explicit. This also fixes a minor bug where we were only checking the type 
of a def to be Number in some explicit casts. This does not work because it 
allows possible unintended casts from BigInteger and BigDecimal to primitive 
types since they both extend Number but are not included as part of the 
Painless casting model.
  • Loading branch information
jdconrad committed Dec 11, 2018
1 parent 8a866fe commit 4a43730
Show file tree
Hide file tree
Showing 3 changed files with 449 additions and 17 deletions.
124 changes: 109 additions & 15 deletions modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java
Original file line number Diff line number Diff line change
Expand Up @@ -622,23 +622,41 @@ static MethodHandle lookupIterator(Class<?> receiverClass) {
// Conversion methods for def to primitive types.

public static boolean defToboolean(final Object value) {
return (boolean)value;
if (value instanceof Boolean) {
return (boolean) value;
} else {
throw new ClassCastException(
"cannot cast def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to boolean");
}
}

public static byte defTobyteImplicit(final Object value) {
return (byte)value;
if (value instanceof Byte) {
return (byte)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to byte");
}
}

public static short defToshortImplicit(final Object value) {
if (value instanceof Byte) {
return (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");
}
}

public static char defTocharImplicit(final Object value) {
return (char)value;
if (value instanceof Character) {
return (char)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to char");
}
}

public static int defTointImplicit(final Object value) {
Expand All @@ -648,8 +666,11 @@ public static int defTointImplicit(final Object value) {
return (short)value;
} else if (value instanceof Character) {
return (char)value;
} else {
} else if (value instanceof Integer) {
return (int)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to int");
}
}

Expand All @@ -662,8 +683,12 @@ public static long defTolongImplicit(final Object value) {
return (char)value;
} else if (value instanceof Integer) {
return (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");
}
}

Expand All @@ -678,8 +703,12 @@ public static float defTofloatImplicit(final Object value) {
return (int)value;
} else if (value instanceof Long) {
return (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");
}
}

Expand All @@ -696,64 +725,129 @@ public static double defTodoubleImplicit(final Object value) {
return (long)value;
} else if (value instanceof Float) {
return (float)value;
} else {
} else if (value instanceof Double) {
return (double)value;
} else {
throw new ClassCastException("cannot implicitly cast def [" + value.getClass().getCanonicalName() + "] to double");
}
}

public static byte defTobyteExplicit(final Object value) {
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");
}
}

public static short defToshortExplicit(final Object value) {
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");
}
}

public static char defTocharExplicit(final Object value) {
if (value instanceof Character) {
return (char)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 char");
}
}

public static int defTointExplicit(final Object value) {
if (value instanceof Character) {
return (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 int");
}
}

public static long defTolongExplicit(final Object value) {
if (value instanceof Character) {
return (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");
}
}

public static float defTofloatExplicit(final Object value) {
if (value instanceof Character) {
return (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");
}
}

public static double defTodoubleExplicit(final Object value) {
if (value instanceof Character) {
return (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");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,12 @@ public void testIllegalDefCast() {
Exception exception = expectScriptThrows(ClassCastException.class, () -> {
exec("def x = 1.0; int y = x; return y;");
});
assertTrue(exception.getMessage().contains("cannot be cast"));
assertTrue(exception.getMessage().contains("cannot implicitly cast"));

exception = expectScriptThrows(ClassCastException.class, () -> {
exec("def x = (short)1; byte y = x; return y;");
});
assertTrue(exception.getMessage().contains("cannot be cast"));
assertTrue(exception.getMessage().contains("cannot implicitly cast"));
}

public void testCat() {
Expand Down
Loading

0 comments on commit 4a43730

Please sign in to comment.