Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Painless] Add extensive tests for def to primitive casts #36455

Merged
merged 3 commits into from
Dec 11, 2018
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
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