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] Partially fixes def boxed types casting #35563

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/painless/painless-casting.asciidoc
Expand Up @@ -501,15 +501,15 @@ indicates whether a cast to the specified target type is implicit (I), explicit
|====
| | o | b | s | c | i | j | f | d | O | B | S | C | I | L | F | D | T | R | def
| def as boolean | I | - | - | - | - | - | - | - | I | - | - | - | - | - | - | - | - | - |
| def as byte | - | I | I | I | I | I | I | I | - | I | I | I | I | I | I | I | - | - |
| def as byte | - | I | I | E | I | I | I | I | - | I | I | E | I | I | I | I | - | - |
| def as short | - | E | I | E | I | I | I | I | - | E | I | E | I | I | I | I | - | - |
| def as char | - | E | E | I | I | I | I | I | - | E | E | I | I | I | I | I | E | - |
| def as int | - | E | E | E | I | I | I | I | - | E | E | E | I | I | I | I | - | - |
| def as long | - | E | E | E | E | I | I | I | - | E | E | E | E | I | I | I | - | - |
| def as float | - | E | E | E | E | E | I | I | - | E | E | E | E | E | I | I | - | - |
| def as double | - | E | E | E | E | E | E | I | - | E | E | E | E | E | E | I | - | - |
| def as Boolean | I | - | - | - | - | - | - | - | I | - | - | - | | - | - | - | - | - |
| def as Byte | - | I | I | I | I | I | I | I | - | I | I | I | I | I | I | I | - | - |
| def as Byte | - | I | I | E | I | I | I | I | - | I | I | E | I | I | I | I | - | - |
| def as Short | - | E | I | E | I | I | I | I | - | E | I | E | I | I | I | I | - | - |
| def as Character | - | E | E | I | I | I | I | I | - | E | E | I | I | I | I | I | - | - |
| def as Integer | - | E | E | E | I | I | I | I | - | E | E | E | I | I | I | I | - | - |
Expand Down
198 changes: 176 additions & 22 deletions modules/lang-painless/src/main/java/org/elasticsearch/painless/Def.java
Expand Up @@ -620,33 +620,29 @@ static MethodHandle lookupIterator(Class<?> receiverClass) {
}


// Conversion methods for Def to primitive types.
// Conversion methods for def to primitive types.

public static boolean DefToboolean(final Object value) {
public static boolean defToboolean(final Object value) {
return (boolean)value;
}

public static byte DefTobyteImplicit(final Object value) {
public static byte defTobyteImplicit(final Object value) {
return (byte)value;
}

public static short DefToshortImplicit(final Object value) {
public static short defToshortImplicit(final Object value) {
if (value instanceof Byte) {
return (byte)value;
} else {
return (short)value;
}
}

public static char DefTocharImplicit(final Object value) {
if (value instanceof Byte) {
return (char)(byte)value;
} else {
return (char)value;
}
public static char defTocharImplicit(final Object value) {
return (char)value;
}

public static int DefTointImplicit(final Object value) {
public static int defTointImplicit(final Object value) {
if (value instanceof Byte) {
return (byte)value;
} else if (value instanceof Short) {
Expand All @@ -658,7 +654,7 @@ public static int DefTointImplicit(final Object value) {
}
}

public static long DefTolongImplicit(final Object value) {
public static long defTolongImplicit(final Object value) {
if (value instanceof Byte) {
return (byte)value;
} else if (value instanceof Short) {
Expand All @@ -672,7 +668,7 @@ public static long DefTolongImplicit(final Object value) {
}
}

public static float DefTofloatImplicit(final Object value) {
public static float defTofloatImplicit(final Object value) {
if (value instanceof Byte) {
return (byte)value;
} else if (value instanceof Short) {
Expand All @@ -688,7 +684,7 @@ public static float DefTofloatImplicit(final Object value) {
}
}

public static double DefTodoubleImplicit(final Object value) {
public static double defTodoubleImplicit(final Object value) {
if (value instanceof Byte) {
return (byte)value;
} else if (value instanceof Short) {
Expand All @@ -706,62 +702,220 @@ public static double DefTodoubleImplicit(final Object value) {
}
}

public static byte DefTobyteExplicit(final Object value) {
public static byte defTobyteExplicit(final Object value) {
if (value instanceof Character) {
return (byte)(char)value;
} else {
return ((Number)value).byteValue();
}
}

public static short DefToshortExplicit(final Object value) {
public static short defToshortExplicit(final Object value) {
if (value instanceof Character) {
return (short)(char)value;
} else {
return ((Number)value).shortValue();
}
}

public static char DefTocharExplicit(final Object value) {
public static char defTocharExplicit(final Object value) {
if (value instanceof Character) {
return ((Character)value);
return (char)value;
} else {
return (char)((Number)value).intValue();
}
}

public static int DefTointExplicit(final Object value) {
public static int defTointExplicit(final Object value) {
if (value instanceof Character) {
return (char)value;
} else {
return ((Number)value).intValue();
}
}

public static long DefTolongExplicit(final Object value) {
public static long defTolongExplicit(final Object value) {
if (value instanceof Character) {
return (char)value;
} else {
return ((Number)value).longValue();
}
}

public static float DefTofloatExplicit(final Object value) {
public static float defTofloatExplicit(final Object value) {
if (value instanceof Character) {
return (char)value;
} else {
return ((Number)value).floatValue();
}
}

public static double DefTodoubleExplicit(final Object value) {
public static double defTodoubleExplicit(final Object value) {
if (value instanceof Character) {
return (char)value;
} else {
return ((Number)value).doubleValue();
}
}

// Conversion methods for def to boxed types.

public static Short defToShortImplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Byte) {
return (short)(byte)value;
} else {
return (Short)value;
}
}

public static Character defToCharacterImplicit(final Object value) {
if (value == null) {
return null;
} else {
return (Character)value;
}
}

public static Integer defToIntegerImplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Byte) {
return (int)(byte)value;
} else if (value instanceof Short) {
return (int)(short)value;
} else if (value instanceof Character) {
return (int)(char)value;
} else {
return (Integer)value;
}
}

public static Long defToLongImplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Byte) {
return (long)(byte)value;
} else if (value instanceof Short) {
return (long)(short)value;
} else if (value instanceof Character) {
return (long)(char)value;
} else if (value instanceof Integer) {
return (long)(int)value;
} else {
return (Long)value;
}
}

public static Float defToFloatImplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Byte) {
return (float)(byte)value;
} else if (value instanceof Short) {
return (float)(short)value;
} else if (value instanceof Character) {
return (float)(char)value;
} else if (value instanceof Integer) {
return (float)(int)value;
} else if (value instanceof Long) {
return (float)(long)value;
} else {
return (Float)value;
}
}

public static Double defToDoubleImplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Byte) {
return (double)(byte)value;
} else if (value instanceof Short) {
return (double)(short)value;
} else if (value instanceof Character) {
return (double)(char)value;
} else if (value instanceof Integer) {
return (double)(int)value;
} else if (value instanceof Long) {
return (double)(long)value;
} else if (value instanceof Float) {
return (double)(float)value;
} else {
return (Double)value;
}
}

public static Byte defToByteExplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Character) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought byte -> char and char -> byte had to be explicit (in java). Does painless differ here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, you are correct. This is a bug that I'll fix now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this is the explicit version, though. I corrected the implicit versions.

return (byte)(char)value;
} else {
return ((Number)value).byteValue();
}
}

public static Short defToShortExplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Character) {
return (short)(char)value;
} else {
return ((Number)value).shortValue();
}
}

public static Character defToCharacterExplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Character) {
return (Character)value;
} else {
return (char)((Number)value).intValue();
}
}

public static Integer defToIntegerExplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Character) {
return (int)(char)value;
} else {
return ((Number)value).intValue();
}
}

public static Long defToLongExplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Character) {
return (long)(char)value;
} else {
return ((Number)value).longValue();
}
}

public static Float defToFloatExplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Character) {
return (float)(char)value;
} else {
return ((Number)value).floatValue();
}
}

public static Double defToDoubleExplicit(final Object value) {
if (value == null) {
return null;
} else if (value instanceof Character) {
return (double)(char)value;
} else {
return ((Number)value).doubleValue();
}
}

/**
* "Normalizes" the index into a {@code Map} by making no change to the index.
*/
Expand Down