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 def to boxed type casts #36506

Merged
merged 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,37 @@ public static PainlessCast getLegalCast(Location location, Class<?> actual, Clas

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) {
Expand Down
139 changes: 124 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 @@ -623,7 +623,7 @@ static MethodHandle lookupIterator(Class<?> receiverClass) {

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");
Expand Down Expand Up @@ -853,11 +853,27 @@ public static double defTodoubleExplicit(final Object value) {

// Conversion methods for def to boxed types.

public static Byte defToByteImplicit(final Object value) {
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 if (value instanceof Byte) {
return (Byte)value;
} else {
throw new ClassCastException("cannot implicitly cast " +
"def [" + PainlessLookupUtility.typeToUnboxedType(value.getClass()).getCanonicalName() + "] to " +
Byte.class.getCanonicalName());
}
}

Expand All @@ -866,16 +882,24 @@ public static Short defToShortImplicit(final Object value) {
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());
}
}

Expand All @@ -888,8 +912,12 @@ public static Integer defToIntegerImplicit(final Object value) {
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());
}
}

Expand All @@ -904,8 +932,12 @@ public static Long defToLongImplicit(final Object value) {
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());
}
}

Expand All @@ -922,8 +954,12 @@ public static Float defToFloatImplicit(final Object value) {
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());
}
}

Expand All @@ -942,8 +978,11 @@ public static Double defToDoubleImplicit(final Object value) {
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());
}
}

Expand All @@ -952,8 +991,18 @@ public static Byte defToByteExplicit(final Object value) {
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());
}
}

Expand All @@ -962,8 +1011,18 @@ public static Short defToShortExplicit(final Object value) {
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());
}
}

Expand All @@ -972,8 +1031,18 @@ public static Character defToCharacterExplicit(final Object value) {
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());
}
}

Expand All @@ -982,8 +1051,18 @@ public static Integer defToIntegerExplicit(final Object value) {
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());
}
}

Expand All @@ -992,8 +1071,18 @@ public static Long defToLongExplicit(final Object value) {
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());
}
}

Expand All @@ -1002,8 +1091,18 @@ public static Float defToFloatExplicit(final Object value) {
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());
}
}

Expand All @@ -1012,8 +1111,18 @@ public static Double defToDoubleExplicit(final Object value) {
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());
}
}

Expand Down
Loading